@@ -5,8 +5,11 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then
55fi
66
77CERC_MIN_NEXTVER=13.4.2
8+ CERC_DEFAULT_WEBPACK_VER=" 5.93.0"
89
910CERC_NEXT_VERSION=" ${CERC_NEXT_VERSION:- keep} "
11+ CERC_WEBPACK_VERSION=" ${CERC_WEBPACK_VERSION:- keep} "
12+
1013CERC_BUILD_TOOL=" ${CERC_BUILD_TOOL} "
1114if [ -z " $CERC_BUILD_TOOL " ]; then
1215 if [ -f " pnpm-lock.yaml" ]; then
@@ -25,39 +28,65 @@ WORK_DIR="${1:-/app}"
2528
2629cd " ${WORK_DIR} " || exit 1
2730
31+ if [ -f " next.config.mjs" ]; then
32+ NEXT_CONFIG_JS=" next.config.mjs"
33+ IMPORT_OR_REQUIRE=" import"
34+ else
35+ NEXT_CONFIG_JS=" next.config.js"
36+ IMPORT_OR_REQUIRE=" require"
37+ fi
38+
2839# If this file doesn't exist at all, we'll get errors below.
29- if [ ! -f " next.config.js " ]; then
30- touch next.config.js
40+ if [ ! -f " ${NEXT_CONFIG_JS} " ]; then
41+ touch ${NEXT_CONFIG_JS}
3142fi
3243
3344if [ ! -f " next.config.dist" ]; then
34- cp next.config.js next.config.dist
45+ cp $NEXT_CONFIG_JS next.config.dist
3546fi
3647
3748which js-beautify > /dev/null
3849if [ $? -ne 0 ]; then
3950 npm i -g js-beautify
4051fi
4152
42- js-beautify next.config.dist > next.config.js
43- echo " " >> next.config.js
53+ # js-beautify formats NEXTJS_CONFIG_FILE (ie next.config.js / next.config.mjs) so we can reliably transformable later
54+ js-beautify next.config.dist > ${NEXT_CONFIG_JS}
55+ echo " " >> ${NEXT_CONFIG_JS}
4456
45- WEBPACK_REQ_LINE=$( grep -n " require([\'\" ]webpack[\'\" ])" next.config.js | cut -d' :' -f1)
46- if [ -z " $WEBPACK_REQ_LINE " ]; then
47- cat > next.config.js.0 << EOF
57+ if [ " ${IMPORT_OR_REQUIRE} " == " require" ]; then
58+ WEBPACK_REQ_LINE=$( grep -n " require([\'\" ]webpack[\'\" ])" ${NEXT_CONFIG_JS} | cut -d' :' -f1)
59+ if [ -z " $WEBPACK_REQ_LINE " ]; then
60+ cat > ${NEXT_CONFIG_JS} .0 << EOF
4861 const webpack = require('webpack');
4962EOF
63+ fi
64+ else
65+ WEBPACK_IMPORT_LINE=$( grep -n " ^import .*[\'\" ]webpack[\'\" ];?$" ${NEXT_CONFIG_JS} | cut -d' :' -f1)
66+ if [ -z " $WEBPACK_IMPORT_LINE " ]; then
67+ cat > ${NEXT_CONFIG_JS} .0 << EOF
68+ import webpack from 'webpack';
69+ EOF
70+ fi
71+ CREATE_REQUIRE_LINE=$( grep -n " require = createRequire" ${NEXT_CONFIG_JS} | cut -d' :' -f1)
72+ if [ -z " $CREATE_REQUIRE_LINE " ]; then
73+ cat >> ${NEXT_CONFIG_JS} .0 << EOF
74+ import { createRequire } from "module";
75+ const require = createRequire(import.meta.url);
76+ EOF
77+ fi
5078fi
5179
52- cat > next.config.js .1 << EOF
80+ cat > ${NEXT_CONFIG_JS} .1 << EOF
5381let envMap;
5482try {
5583 // .env-list.json provides us a list of identifiers which should be replaced at runtime.
5684 envMap = require('./.env-list.json').reduce((a, v) => {
5785 a[v] = \` "CERC_RUNTIME_ENV_\$ {v.split(/\./).pop()}"\` ;
5886 return a;
5987 }, {});
60- } catch {
88+ } catch (e) {
89+ console.error(e);
6190 // If .env-list.json cannot be loaded, we are probably running in dev mode, so use process.env instead.
6291 envMap = Object.keys(process.env).reduce((a, v) => {
6392 if (v.startsWith('CERC_')) {
@@ -66,23 +95,24 @@ try {
6695 return a;
6796 }, {});
6897}
98+ console.log(envMap);
6999EOF
70100
71- CONFIG_LINES=$( wc -l next.config.js | awk ' { print $1 }' )
72- ENV_LINE=$( grep -n ' env:' next.config.js | cut -d' :' -f1)
73- WEBPACK_CONF_LINE=$( egrep -n ' webpack:\s+\([^,]+,' next.config.js | cut -d' :' -f1)
101+ CONFIG_LINES=$( wc -l ${NEXT_CONFIG_JS} | awk ' { print $1 }' )
102+ ENV_LINE=$( grep -n ' env:' ${NEXT_CONFIG_JS} | cut -d' :' -f1)
103+ WEBPACK_CONF_LINE=$( egrep -n ' webpack:\s+\([^,]+,' ${NEXT_CONFIG_JS} | cut -d' :' -f1)
74104NEXT_SECTION_ADJUSTMENT=0
75105
76106if [ -n " $WEBPACK_CONF_LINE " ]; then
77- WEBPACK_CONF_VAR=$( egrep -n ' webpack:\s+\([^,]+,' next.config.js | cut -d' ,' -f1 | cut -d' (' -f2)
78- head -$(( ${WEBPACK_CONF_LINE} )) next.config.js > next.config.js .2
79- cat > next.config.js .3 << EOF
107+ WEBPACK_CONF_VAR=$( egrep -n ' webpack:\s+\([^,]+,' ${NEXT_CONFIG_JS} | cut -d' ,' -f1 | cut -d' (' -f2)
108+ head -$(( ${WEBPACK_CONF_LINE} )) ${NEXT_CONFIG_JS} > ${NEXT_CONFIG_JS} .2
109+ cat > ${NEXT_CONFIG_JS} .3 << EOF
80110 $WEBPACK_CONF_VAR .plugins.push(new webpack.DefinePlugin(envMap));
81111EOF
82112 NEXT_SECTION_LINE=$(( WEBPACK_CONF_LINE))
83113elif [ -n " $ENV_LINE " ]; then
84- head -$(( ${ENV_LINE} - 1 )) next.config.js > next.config.js .2
85- cat > next.config.js .3 << EOF
114+ head -$(( ${ENV_LINE} - 1 )) ${NEXT_CONFIG_JS} > ${NEXT_CONFIG_JS} .2
115+ cat > ${NEXT_CONFIG_JS} .3 << EOF
86116 webpack: (config) => {
87117 config.plugins.push(new webpack.DefinePlugin(envMap));
88118 return config;
91121 NEXT_SECTION_ADJUSTMENT=1
92122 NEXT_SECTION_LINE=$ENV_LINE
93123else
94- echo " WARNING: Cannot find location to insert environment variable map in next.config.js " 1>&2
95- rm -f next.config.js .*
124+ echo " WARNING: Cannot find location to insert environment variable map in ${NEXT_CONFIG_JS} " 1>&2
125+ rm -f ${NEXT_CONFIG_JS} .*
96126 NEXT_SECTION_LINE=0
97127fi
98128
99- tail -$(( ${CONFIG_LINES} - ${NEXT_SECTION_LINE} + ${NEXT_SECTION_ADJUSTMENT} )) next.config.js > next.config.js.5
129+ tail -$(( ${CONFIG_LINES} - ${NEXT_SECTION_LINE} + ${NEXT_SECTION_ADJUSTMENT} )) ${NEXT_CONFIG_JS} > ${NEXT_CONFIG_JS} .4
100130
101- cat next.config.js.* | sed ' s/^ *//g' | js-beautify | grep -v ' process\.\env\.' | js-beautify > next.config.js
102- rm next.config.js.*
131+ rm -f ${NEXT_CONFIG_JS}
132+ for (( i= 0 ; i <= 5 ; i++ )) ; do
133+ if [ -f " ${NEXT_CONFIG_JS} .${i} " ]; then
134+ if [ $i -le 2 ] ; then
135+ cat ${NEXT_CONFIG_JS} .${i} >> ${NEXT_CONFIG_JS}
136+ else
137+ cat ${NEXT_CONFIG_JS} .${i} | sed ' s/^ *//g' | js-beautify | grep -v ' process\.\env\.' | js-beautify >> ${NEXT_CONFIG_JS}
138+ fi
139+ fi
140+ done
141+ rm ${NEXT_CONFIG_JS} .*
103142
104143" ${SCRIPT_DIR} /find-env.sh" " $( pwd) " > .env-list.json
105144
@@ -115,6 +154,19 @@ if [ "$CERC_NEXT_VERSION" != "keep" ] && [ "$CUR_NEXT_VERSION" != "$CERC_NEXT_VE
115154 mv package.json.$$ package.json
116155fi
117156
157+ CUR_WEBPACK_VERSION=" ` jq -r ' .dependencies.webpack' package.json` "
158+ if [ -z " $CUR_WEBPACK_VERSION " ]; then
159+ CUR_WEBPACK_VERSION=" ` jq -r ' .devDependencies.webpack' package.json` "
160+ fi
161+ if [ " ${CERC_WEBPACK_VERSION} " != " keep" ] || [ " ${CUR_WEBPACK_VERSION} " == " null" ]; then
162+ if [ -z " $CERC_WEBPACK_VERSION " ] || [ " $CERC_WEBPACK_VERSION " == " keep" ]; then
163+ CERC_WEBPACK_VERSION=" ${CERC_DEFAULT_WEBPACK_VER} "
164+ fi
165+ echo " Webpack is required for env variable substitution. Adding to webpack@$CERC_WEBPACK_VERSION to dependencies..." 1>&2
166+ cat package.json | jq " .dependencies.webpack = \" $CERC_WEBPACK_VERSION \" " > package.json.$$
167+ mv package.json.$$ package.json
168+ fi
169+
118170time $CERC_BUILD_TOOL install || exit 1
119171
120172CUR_NEXT_VERSION=` jq -r ' .version' node_modules/next/package.json`
0 commit comments