WIP: Support next.config.mjs
All checks were successful
Lint Checks / Run linter (pull_request) Successful in 35s
Smoke Test / Run basic test suite (pull_request) Successful in 4m18s
Webapp Test / Run webapp test suite (pull_request) Successful in 4m47s
Deploy Test / Run deploy test suite (pull_request) Successful in 5m6s
K8s Deploy Test / Run deploy test suite on kind/k8s (pull_request) Successful in 8m30s

This commit is contained in:
Thomas E Lackey 2024-07-24 15:36:57 -05:00
parent 01deac78c4
commit 48cc96875e
2 changed files with 32 additions and 23 deletions

View File

@ -3,6 +3,7 @@
if [ -n "$CERC_SCRIPT_DEBUG" ]; then if [ -n "$CERC_SCRIPT_DEBUG" ]; then
set -x set -x
fi fi
set -x
CERC_MIN_NEXTVER=13.4.2 CERC_MIN_NEXTVER=13.4.2
@ -25,13 +26,19 @@ 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"
else
NEXT_CONFIG_JS="next.config.js"
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 +46,17 @@ 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 next.config.dist > ${NEXT_CONFIG_JS}
echo "" >> next.config.js echo "" >> ${NEXT_CONFIG_JS}
WEBPACK_REQ_LINE=$(grep -n "require([\'\"]webpack[\'\"])" next.config.js | cut -d':' -f1) WEBPACK_REQ_LINE=$(grep -n "require([\'\"]webpack[\'\"])" ${NEXT_CONFIG_JS} | cut -d':' -f1)
if [ -z "$WEBPACK_REQ_LINE" ]; then if [ -z "$WEBPACK_REQ_LINE" ]; then
cat > next.config.js.0 <<EOF cat > ${NEXT_CONFIG_JS}.0 <<EOF
const webpack = require('webpack'); const webpack = require('webpack');
EOF EOF
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.
@ -68,21 +75,21 @@ try {
} }
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 +98,15 @@ 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}.5
cat next.config.js.* | sed 's/^ *//g' | js-beautify | grep -v 'process\.\env\.' | js-beautify > next.config.js cat ${NEXT_CONFIG_JS}.* | sed 's/^ *//g' | js-beautify | grep -v 'process\.\env\.' | js-beautify > ${NEXT_CONFIG_JS}
rm next.config.js.* rm ${NEXT_CONFIG_JS}.*
"${SCRIPT_DIR}/find-env.sh" "$(pwd)" > .env-list.json "${SCRIPT_DIR}/find-env.sh" "$(pwd)" > .env-list.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 .