@@ -5,8 +5,11 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then
5
5
fi
6
6
7
7
CERC_MIN_NEXTVER=13.4.2
8
+ CERC_DEFAULT_WEBPACK_VER=" 5.93.0"
8
9
9
10
CERC_NEXT_VERSION=" ${CERC_NEXT_VERSION:- keep} "
11
+ CERC_WEBPACK_VERSION=" ${CERC_WEBPACK_VERSION:- keep} "
12
+
10
13
CERC_BUILD_TOOL=" ${CERC_BUILD_TOOL} "
11
14
if [ -z " $CERC_BUILD_TOOL " ]; then
12
15
if [ -f " pnpm-lock.yaml" ]; then
@@ -25,39 +28,65 @@ WORK_DIR="${1:-/app}"
25
28
26
29
cd " ${WORK_DIR} " || exit 1
27
30
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
+
28
39
# 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}
31
42
fi
32
43
33
44
if [ ! -f " next.config.dist" ]; then
34
- cp next.config.js next.config.dist
45
+ cp $NEXT_CONFIG_JS next.config.dist
35
46
fi
36
47
37
48
which js-beautify > /dev/null
38
49
if [ $? -ne 0 ]; then
39
50
npm i -g js-beautify
40
51
fi
41
52
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}
44
56
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
48
61
const webpack = require('webpack');
49
62
EOF
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
50
78
fi
51
79
52
- cat > next.config.js .1 << EOF
80
+ cat > ${NEXT_CONFIG_JS} .1 << EOF
53
81
let envMap;
54
82
try {
55
83
// .env-list.json provides us a list of identifiers which should be replaced at runtime.
56
84
envMap = require('./.env-list.json').reduce((a, v) => {
57
85
a[v] = \` "CERC_RUNTIME_ENV_\$ {v.split(/\./).pop()}"\` ;
58
86
return a;
59
87
}, {});
60
- } catch {
88
+ } catch (e) {
89
+ console.error(e);
61
90
// If .env-list.json cannot be loaded, we are probably running in dev mode, so use process.env instead.
62
91
envMap = Object.keys(process.env).reduce((a, v) => {
63
92
if (v.startsWith('CERC_')) {
@@ -66,23 +95,24 @@ try {
66
95
return a;
67
96
}, {});
68
97
}
98
+ console.log(envMap);
69
99
EOF
70
100
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)
74
104
NEXT_SECTION_ADJUSTMENT=0
75
105
76
106
if [ -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
80
110
$WEBPACK_CONF_VAR .plugins.push(new webpack.DefinePlugin(envMap));
81
111
EOF
82
112
NEXT_SECTION_LINE=$(( WEBPACK_CONF_LINE))
83
113
elif [ -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
86
116
webpack: (config) => {
87
117
config.plugins.push(new webpack.DefinePlugin(envMap));
88
118
return config;
91
121
NEXT_SECTION_ADJUSTMENT=1
92
122
NEXT_SECTION_LINE=$ENV_LINE
93
123
else
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} .*
96
126
NEXT_SECTION_LINE=0
97
127
fi
98
128
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
100
130
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} .*
103
142
104
143
" ${SCRIPT_DIR} /find-env.sh" " $( pwd) " > .env-list.json
105
144
@@ -115,6 +154,19 @@ if [ "$CERC_NEXT_VERSION" != "keep" ] && [ "$CUR_NEXT_VERSION" != "$CERC_NEXT_VE
115
154
mv package.json.$$ package.json
116
155
fi
117
156
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
+
118
170
time $CERC_BUILD_TOOL install || exit 1
119
171
120
172
CUR_NEXT_VERSION=` jq -r ' .version' node_modules/next/package.json`
0 commit comments