Use a more flexible mechanism to inject config into next.config.js for runtime env. (#901)

Instead of attempting to rewriting the nextConfig file directly, inject a helper function to add the config we need.

Reviewed-on: cerc-io/stack-orchestrator#901
Reviewed-by: David Boreham <dboreham@noreply.git.vdb.to>
Co-authored-by: Thomas E Lackey <telackey@bozemanpass.com>
Co-committed-by: Thomas E Lackey <telackey@bozemanpass.com>
This commit is contained in:
Thomas E Lackey 2024-07-31 03:22:23 +00:00 committed by Thomas E Lackey
parent 913c3a8557
commit f64683f03b

View File

@ -98,39 +98,67 @@ try {
console.log(envMap); console.log(envMap);
EOF EOF
CONFIG_LINES=$(wc -l ${NEXT_CONFIG_JS} | awk '{ print $1 }') grep 'withPWA' ${NEXT_CONFIG_JS} >/dev/null && HAS_WITHPWA=true || HAS_WITHPWA=false
ENV_LINE=$(grep -n 'env:' ${NEXT_CONFIG_JS} | cut -d':' -f1)
WEBPACK_CONF_LINE=$(egrep -n 'webpack:\s+\([^,]+,' ${NEXT_CONFIG_JS} | cut -d':' -f1)
NEXT_SECTION_ADJUSTMENT=0
if [ -n "$WEBPACK_CONF_LINE" ]; then if [ "$HAS_WITHPWA" == "true" ]; then
WEBPACK_CONF_VAR=$(egrep -n 'webpack:\s+\([^,]+,' ${NEXT_CONFIG_JS} | cut -d',' -f1 | cut -d'(' -f2) if [ "$IMPORT_OR_REQUIRE" == "import" ]; then
head -$(( ${WEBPACK_CONF_LINE} )) ${NEXT_CONFIG_JS} > ${NEXT_CONFIG_JS}.2 cat > ${NEXT_CONFIG_JS}.2 <<EOF
cat > ${NEXT_CONFIG_JS}.3 <<EOF const __xPWA__ = (p) => {
$WEBPACK_CONF_VAR.plugins.push(new webpack.DefinePlugin(envMap)); const realPWA = withPWA(p);
EOF return (nextConfig) => {
NEXT_SECTION_LINE=$((WEBPACK_CONF_LINE)) const modConfig = {...nextConfig};
elif [ -n "$ENV_LINE" ]; then
head -$(( ${ENV_LINE} - 1 )) ${NEXT_CONFIG_JS} > ${NEXT_CONFIG_JS}.2 modConfig.webpack = (config) => {
cat > ${NEXT_CONFIG_JS}.3 <<EOF
webpack: (config) => {
config.plugins.push(new webpack.DefinePlugin(envMap)); config.plugins.push(new webpack.DefinePlugin(envMap));
return config; return nextConfig.webpack ? nextConfig.webpack(config) : config;
}, };
return realPWA(modConfig);
};
};
EOF EOF
NEXT_SECTION_ADJUSTMENT=1 else
NEXT_SECTION_LINE=$ENV_LINE cat > ${NEXT_CONFIG_JS}.3 <<EOF
const __xPWA__ = (nextConfig) => {
const modConfig = {...nextConfig};
modConfig.webpack = (config) => {
config.plugins.push(new webpack.DefinePlugin(envMap));
return nextConfig.webpack ? nextConfig.webpack(config) : config;
};
return withPWA(modConfig);
};
EOF
fi
cat ${NEXT_CONFIG_JS} | js-beautify | sed 's/withPWA(/__xPWA__(/g' > ${NEXT_CONFIG_JS}.4
else else
echo "WARNING: Cannot find location to insert environment variable map in ${NEXT_CONFIG_JS}" 1>&2 cat > ${NEXT_CONFIG_JS}.3 <<EOF
rm -f ${NEXT_CONFIG_JS}.* const __xCfg__ = (nextConfig) => {
NEXT_SECTION_LINE=0 const modConfig = {...nextConfig};
modConfig.webpack = (config) => {
config.plugins.push(new webpack.DefinePlugin(envMap));
return nextConfig.webpack ? nextConfig.webpack(config) : config;
};
return modConfig;
};
EOF
if [ "$IMPORT_OR_REQUIRE" == "import" ]; then
cat ${NEXT_CONFIG_JS} | js-beautify | sed 's/export\s\+default\s\+/const __orig_cfg__ = /g' > ${NEXT_CONFIG_JS}.4
echo "export default __xCfg__(__orig_cfg__);" > ${NEXT_CONFIG_JS}.5
else
cat ${NEXT_CONFIG_JS} | js-beautify | sed 's/module.exports\s\+=\s\+/const __orig_cfg__ = /g' > ${NEXT_CONFIG_JS}.4
echo "module.exports = __xCfg__(__orig_cfg__);" > ${NEXT_CONFIG_JS}.5
fi
fi fi
tail -$(( ${CONFIG_LINES} - ${NEXT_SECTION_LINE} + ${NEXT_SECTION_ADJUSTMENT} )) ${NEXT_CONFIG_JS} > ${NEXT_CONFIG_JS}.4
rm -f ${NEXT_CONFIG_JS} rm -f ${NEXT_CONFIG_JS}
for ((i=0; i <=5; i++)); do for ((i=0; i <= 10; i++)); do
if [ -f "${NEXT_CONFIG_JS}.${i}" ]; then if [ -s "${NEXT_CONFIG_JS}.${i}" ]; then
if [ $i -le 2 ] ; then if [ $i -le 2 ] ; then
cat ${NEXT_CONFIG_JS}.${i} >> ${NEXT_CONFIG_JS} cat ${NEXT_CONFIG_JS}.${i} >> ${NEXT_CONFIG_JS}
else else
@ -139,6 +167,8 @@ for ((i=0; i <=5; i++)); do
fi fi
done done
rm ${NEXT_CONFIG_JS}.* rm ${NEXT_CONFIG_JS}.*
cat ${NEXT_CONFIG_JS} | js-beautify > ${NEXT_CONFIG_JS}.pretty
mv ${NEXT_CONFIG_JS}.pretty ${NEXT_CONFIG_JS}
"${SCRIPT_DIR}/find-env.sh" "$(pwd)" > .env-list.json "${SCRIPT_DIR}/find-env.sh" "$(pwd)" > .env-list.json