881: Support next.config.mjs #890
@ -3,11 +3,13 @@
|
||||
if [ -n "$CERC_SCRIPT_DEBUG" ]; then
|
||||
set -x
|
||||
fi
|
||||
set -x
|
||||
|
||||
CERC_MIN_NEXTVER=13.4.2
|
||||
CERC_DEFAULT_WEBPACK_VER="5.93.0"
|
||||
|
||||
CERC_NEXT_VERSION="${CERC_NEXT_VERSION:-keep}"
|
||||
CERC_WEBPACK_VERSION="${CERC_WEBPACK_VERSION:-keep}"
|
||||
|
||||
CERC_BUILD_TOOL="${CERC_BUILD_TOOL}"
|
||||
if [ -z "$CERC_BUILD_TOOL" ]; then
|
||||
if [ -f "pnpm-lock.yaml" ]; then
|
||||
@ -28,8 +30,10 @@ 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.
|
||||
@ -50,11 +54,27 @@ fi
|
||||
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 [ -z "$WEBPACK_REQ_LINE" ]; then
|
||||
cat > ${NEXT_CONFIG_JS}.0 <<EOF
|
||||
if [ "${IMPORT_OR_REQUIRE}" == "require" ]; then
|
||||
WEBPACK_REQ_LINE=$(grep -n "require([\'\"]webpack[\'\"])" ${NEXT_CONFIG_JS} | cut -d':' -f1)
|
||||
if [ -z "$WEBPACK_REQ_LINE" ]; then
|
||||
cat > ${NEXT_CONFIG_JS}.0 <<EOF
|
||||
const webpack = require('webpack');
|
||||
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
|
||||
|
||||
cat > ${NEXT_CONFIG_JS}.1 <<EOF
|
||||
@ -65,7 +85,8 @@ try {
|
||||
a[v] = \`"CERC_RUNTIME_ENV_\${v.split(/\./).pop()}"\`;
|
||||
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.
|
||||
envMap = Object.keys(process.env).reduce((a, v) => {
|
||||
if (v.startsWith('CERC_')) {
|
||||
@ -74,6 +95,7 @@ try {
|
||||
return a;
|
||||
}, {});
|
||||
}
|
||||
console.log(envMap);
|
||||
EOF
|
||||
|
||||
CONFIG_LINES=$(wc -l ${NEXT_CONFIG_JS} | awk '{ print $1 }')
|
||||
@ -104,9 +126,10 @@ else
|
||||
NEXT_SECTION_LINE=0
|
||||
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}
|
||||
cat ${NEXT_CONFIG_JS}.{0,1,2} > ${NEXT_CONFIG_JS}
|
||||
cat ${NEXT_CONFIG_JS}.{3,4} | sed 's/^ *//g' | js-beautify | grep -v 'process\.\env\.' | js-beautify >> ${NEXT_CONFIG_JS}
|
||||
rm ${NEXT_CONFIG_JS}.*
|
||||
|
||||
"${SCRIPT_DIR}/find-env.sh" "$(pwd)" > .env-list.json
|
||||
@ -123,6 +146,16 @@ if [ "$CERC_NEXT_VERSION" != "keep" ] && [ "$CUR_NEXT_VERSION" != "$CERC_NEXT_VE
|
||||
mv package.json.$$ package.json
|
||||
fi
|
||||
|
||||
CUR_WEBPACK_VERSION="`jq -r '.dependencies.webpack' package.json`"
|
||||
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. 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
|
||||
|
||||
CUR_NEXT_VERSION=`jq -r '.version' node_modules/next/package.json`
|
||||
|
Loading…
Reference in New Issue
Block a user