881: Support next.config.mjs (#890)

Reviewed-on: cerc-io/stack-orchestrator#890
This commit is contained in:
Thomas E Lackey 2024-07-25 16:47:17 +00:00
parent b26698b756
commit 432bd4113d
2 changed files with 79 additions and 25 deletions

View File

@ -5,8 +5,11 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then
fi fi
CERC_MIN_NEXTVER=13.4.2 CERC_MIN_NEXTVER=13.4.2
CERC_DEFAULT_WEBPACK_VER="5.93.0"
CERC_NEXT_VERSION="${CERC_NEXT_VERSION:-keep}" CERC_NEXT_VERSION="${CERC_NEXT_VERSION:-keep}"
CERC_WEBPACK_VERSION="${CERC_WEBPACK_VERSION:-keep}"
CERC_BUILD_TOOL="${CERC_BUILD_TOOL}" CERC_BUILD_TOOL="${CERC_BUILD_TOOL}"
if [ -z "$CERC_BUILD_TOOL" ]; then if [ -z "$CERC_BUILD_TOOL" ]; then
if [ -f "pnpm-lock.yaml" ]; then if [ -f "pnpm-lock.yaml" ]; then
@ -25,13 +28,21 @@ WORK_DIR="${1:-/app}"
cd "${WORK_DIR}" || exit 1 cd "${WORK_DIR}" || exit 1
if [ -f "next.config.mjs" ]; then
NEXT_CONFIG_JS="next.config.mjs"
IMPORT_OR_REQUIRE="import"
else
NEXT_CONFIG_JS="next.config.js"
IMPORT_OR_REQUIRE="require"
fi
# If this file doesn't exist at all, we'll get errors below. # If this file doesn't exist at all, we'll get errors below.
if [ ! -f "next.config.js" ]; then if [ ! -f "${NEXT_CONFIG_JS}" ]; then
touch next.config.js touch ${NEXT_CONFIG_JS}
fi fi
if [ ! -f "next.config.dist" ]; then if [ ! -f "next.config.dist" ]; then
cp next.config.js next.config.dist cp $NEXT_CONFIG_JS next.config.dist
fi fi
which js-beautify >/dev/null which js-beautify >/dev/null
@ -39,17 +50,34 @@ if [ $? -ne 0 ]; then
npm i -g js-beautify npm i -g js-beautify
fi fi
js-beautify next.config.dist > next.config.js # js-beautify formats NEXTJS_CONFIG_FILE (ie next.config.js / next.config.mjs) so we can reliably transformable later
echo "" >> next.config.js js-beautify next.config.dist > ${NEXT_CONFIG_JS}
echo "" >> ${NEXT_CONFIG_JS}
WEBPACK_REQ_LINE=$(grep -n "require([\'\"]webpack[\'\"])" next.config.js | cut -d':' -f1) if [ "${IMPORT_OR_REQUIRE}" == "require" ]; then
if [ -z "$WEBPACK_REQ_LINE" ]; then WEBPACK_REQ_LINE=$(grep -n "require([\'\"]webpack[\'\"])" ${NEXT_CONFIG_JS} | cut -d':' -f1)
cat > next.config.js.0 <<EOF if [ -z "$WEBPACK_REQ_LINE" ]; then
cat > ${NEXT_CONFIG_JS}.0 <<EOF
const webpack = require('webpack'); const webpack = require('webpack');
EOF EOF
fi
else
WEBPACK_IMPORT_LINE=$(grep -n "^import .*[\'\"]webpack[\'\"];?$" ${NEXT_CONFIG_JS} | cut -d':' -f1)
if [ -z "$WEBPACK_IMPORT_LINE" ]; then
cat > ${NEXT_CONFIG_JS}.0 <<EOF
import webpack from 'webpack';
EOF
fi
CREATE_REQUIRE_LINE=$(grep -n "require = createRequire" ${NEXT_CONFIG_JS} | cut -d':' -f1)
if [ -z "$CREATE_REQUIRE_LINE" ]; then
cat >> ${NEXT_CONFIG_JS}.0 <<EOF
import { createRequire } from "module";
const require = createRequire(import.meta.url);
EOF
fi
fi fi
cat > next.config.js.1 <<EOF cat > ${NEXT_CONFIG_JS}.1 <<EOF
let envMap; let envMap;
try { try {
// .env-list.json provides us a list of identifiers which should be replaced at runtime. // .env-list.json provides us a list of identifiers which should be replaced at runtime.
@ -57,7 +85,8 @@ try {
a[v] = \`"CERC_RUNTIME_ENV_\${v.split(/\./).pop()}"\`; a[v] = \`"CERC_RUNTIME_ENV_\${v.split(/\./).pop()}"\`;
return a; return a;
}, {}); }, {});
} catch { } catch (e) {
console.error(e);
// If .env-list.json cannot be loaded, we are probably running in dev mode, so use process.env instead. // If .env-list.json cannot be loaded, we are probably running in dev mode, so use process.env instead.
envMap = Object.keys(process.env).reduce((a, v) => { envMap = Object.keys(process.env).reduce((a, v) => {
if (v.startsWith('CERC_')) { if (v.startsWith('CERC_')) {
@ -66,23 +95,24 @@ try {
return a; return a;
}, {}); }, {});
} }
console.log(envMap);
EOF EOF
CONFIG_LINES=$(wc -l next.config.js | awk '{ print $1 }') CONFIG_LINES=$(wc -l ${NEXT_CONFIG_JS} | awk '{ print $1 }')
ENV_LINE=$(grep -n 'env:' next.config.js | cut -d':' -f1) ENV_LINE=$(grep -n 'env:' ${NEXT_CONFIG_JS} | cut -d':' -f1)
WEBPACK_CONF_LINE=$(egrep -n 'webpack:\s+\([^,]+,' next.config.js | cut -d':' -f1) WEBPACK_CONF_LINE=$(egrep -n 'webpack:\s+\([^,]+,' ${NEXT_CONFIG_JS} | cut -d':' -f1)
NEXT_SECTION_ADJUSTMENT=0 NEXT_SECTION_ADJUSTMENT=0
if [ -n "$WEBPACK_CONF_LINE" ]; then if [ -n "$WEBPACK_CONF_LINE" ]; then
WEBPACK_CONF_VAR=$(egrep -n 'webpack:\s+\([^,]+,' next.config.js | cut -d',' -f1 | cut -d'(' -f2) WEBPACK_CONF_VAR=$(egrep -n 'webpack:\s+\([^,]+,' ${NEXT_CONFIG_JS} | cut -d',' -f1 | cut -d'(' -f2)
head -$(( ${WEBPACK_CONF_LINE} )) next.config.js > next.config.js.2 head -$(( ${WEBPACK_CONF_LINE} )) ${NEXT_CONFIG_JS} > ${NEXT_CONFIG_JS}.2
cat > next.config.js.3 <<EOF cat > ${NEXT_CONFIG_JS}.3 <<EOF
$WEBPACK_CONF_VAR.plugins.push(new webpack.DefinePlugin(envMap)); $WEBPACK_CONF_VAR.plugins.push(new webpack.DefinePlugin(envMap));
EOF EOF
NEXT_SECTION_LINE=$((WEBPACK_CONF_LINE)) NEXT_SECTION_LINE=$((WEBPACK_CONF_LINE))
elif [ -n "$ENV_LINE" ]; then elif [ -n "$ENV_LINE" ]; then
head -$(( ${ENV_LINE} - 1 )) next.config.js > next.config.js.2 head -$(( ${ENV_LINE} - 1 )) ${NEXT_CONFIG_JS} > ${NEXT_CONFIG_JS}.2
cat > next.config.js.3 <<EOF cat > ${NEXT_CONFIG_JS}.3 <<EOF
webpack: (config) => { webpack: (config) => {
config.plugins.push(new webpack.DefinePlugin(envMap)); config.plugins.push(new webpack.DefinePlugin(envMap));
return config; return config;
@ -91,15 +121,24 @@ EOF
NEXT_SECTION_ADJUSTMENT=1 NEXT_SECTION_ADJUSTMENT=1
NEXT_SECTION_LINE=$ENV_LINE NEXT_SECTION_LINE=$ENV_LINE
else else
echo "WARNING: Cannot find location to insert environment variable map in next.config.js" 1>&2 echo "WARNING: Cannot find location to insert environment variable map in ${NEXT_CONFIG_JS}" 1>&2
rm -f next.config.js.* rm -f ${NEXT_CONFIG_JS}.*
NEXT_SECTION_LINE=0 NEXT_SECTION_LINE=0
fi fi
tail -$(( ${CONFIG_LINES} - ${NEXT_SECTION_LINE} + ${NEXT_SECTION_ADJUSTMENT} )) next.config.js > next.config.js.5 tail -$(( ${CONFIG_LINES} - ${NEXT_SECTION_LINE} + ${NEXT_SECTION_ADJUSTMENT} )) ${NEXT_CONFIG_JS} > ${NEXT_CONFIG_JS}.4
cat next.config.js.* | sed 's/^ *//g' | js-beautify | grep -v 'process\.\env\.' | js-beautify > next.config.js rm -f ${NEXT_CONFIG_JS}
rm next.config.js.* for ((i=0; i <=5; i++)); do
if [ -f "${NEXT_CONFIG_JS}.${i}" ]; then
if [ $i -le 2 ] ; then
cat ${NEXT_CONFIG_JS}.${i} >> ${NEXT_CONFIG_JS}
else
cat ${NEXT_CONFIG_JS}.${i} | sed 's/^ *//g' | js-beautify | grep -v 'process\.\env\.' | js-beautify >> ${NEXT_CONFIG_JS}
fi
fi
done
rm ${NEXT_CONFIG_JS}.*
"${SCRIPT_DIR}/find-env.sh" "$(pwd)" > .env-list.json "${SCRIPT_DIR}/find-env.sh" "$(pwd)" > .env-list.json
@ -115,6 +154,19 @@ if [ "$CERC_NEXT_VERSION" != "keep" ] && [ "$CUR_NEXT_VERSION" != "$CERC_NEXT_VE
mv package.json.$$ package.json mv package.json.$$ package.json
fi fi
CUR_WEBPACK_VERSION="`jq -r '.dependencies.webpack' package.json`"
if [ -z "$CUR_WEBPACK_VERSION" ]; then
CUR_WEBPACK_VERSION="`jq -r '.devDependencies.webpack' package.json`"
fi
if [ "${CERC_WEBPACK_VERSION}" != "keep" ] || [ "${CUR_WEBPACK_VERSION}" == "null" ]; then
if [ -z "$CERC_WEBPACK_VERSION" ] || [ "$CERC_WEBPACK_VERSION" == "keep" ]; then
CERC_WEBPACK_VERSION="${CERC_DEFAULT_WEBPACK_VER}"
fi
echo "Webpack is required for env variable substitution. Adding to webpack@$CERC_WEBPACK_VERSION to dependencies..." 1>&2
cat package.json | jq ".dependencies.webpack = \"$CERC_WEBPACK_VERSION\"" > package.json.$$
mv package.json.$$ package.json
fi
time $CERC_BUILD_TOOL install || exit 1 time $CERC_BUILD_TOOL install || exit 1
CUR_NEXT_VERSION=`jq -r '.version' node_modules/next/package.json` CUR_NEXT_VERSION=`jq -r '.version' node_modules/next/package.json`

View File

@ -20,9 +20,11 @@ for d in $(find . -maxdepth 1 -type d | grep -v '\./\.' | grep '/' | cut -d'/' -
done done
done done
NEXT_CONF="next.config.js next.config.dist" NEXT_CONF="next.config.mjs next.config.js next.config.dist"
for f in $NEXT_CONF; do for f in $NEXT_CONF; do
if [ -f "$f" ]; then
cat "$f" | tr -s '[:blank:]' '\n' | tr -s '[{},()]' '\n' | egrep -o 'process.env.[A-Za-z0-9_]+' >> $TMPF cat "$f" | tr -s '[:blank:]' '\n' | tr -s '[{},()]' '\n' | egrep -o 'process.env.[A-Za-z0-9_]+' >> $TMPF
fi
done done
cat $TMPF | sort -u | jq --raw-input . | jq --slurp . cat $TMPF | sort -u | jq --raw-input . | jq --slurp .