Support the case where webpack config is already present next.config.js
This commit is contained in:
parent
5e91c2224e
commit
0acec8551b
@ -10,21 +10,25 @@ WORK_DIR="${1:-/app}"
|
|||||||
|
|
||||||
cd "${WORK_DIR}" || exit 1
|
cd "${WORK_DIR}" || exit 1
|
||||||
|
|
||||||
cp next.config.js next.config.dist
|
if [ ! -f "next.config.dist" ]; then
|
||||||
|
cp next.config.js next.config.dist
|
||||||
|
fi
|
||||||
|
|
||||||
|
which js-beautify >/dev/null
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
npm i -g js-beautify
|
||||||
|
fi
|
||||||
|
|
||||||
npm i -g js-beautify
|
|
||||||
js-beautify next.config.dist > next.config.js
|
js-beautify next.config.dist > next.config.js
|
||||||
|
|
||||||
npm install
|
WEBPACK_REQ_LINE=$(grep -n "require([\'\"]webpack[\'\"])" next.config.js | cut -d':' -f1)
|
||||||
|
if [ -z "$WEBPACK_REQ_LINE" ]; then
|
||||||
CONFIG_LINES=$(wc -l next.config.js | awk '{ print $1 }')
|
cat > next.config.js.0 <<EOF
|
||||||
MOD_EXPORTS_LINE=$(grep -n 'module.exports' next.config.js | cut -d':' -f1)
|
const webpack = require('webpack');
|
||||||
|
EOF
|
||||||
head -$(( ${MOD_EXPORTS_LINE} - 1 )) next.config.js > next.config.js.1
|
fi
|
||||||
|
|
||||||
cat > next.config.js.2 <<EOF
|
|
||||||
const webpack = require('webpack');
|
|
||||||
|
|
||||||
|
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.
|
||||||
@ -43,21 +47,42 @@ try {
|
|||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
grep 'module.exports' next.config.js > next.config.js.3
|
CONFIG_LINES=$(wc -l next.config.js | awk '{ print $1 }')
|
||||||
|
ENV_LINE=$(grep -n 'env:' next.config.js | cut -d':' -f1)
|
||||||
|
WEBPACK_CONF_LINE=$(egrep -n 'webpack:\s+\([^,]+,' next.config.js | cut -d':' -f1)
|
||||||
|
NEXT_SECTION_ADJUSTMENT=0
|
||||||
|
|
||||||
cat > next.config.js.4 <<EOF
|
if [ -n "$WEBPACK_CONF_LINE" ]; then
|
||||||
webpack: (config) => {
|
WEBPACK_CONF_VAR=$(egrep -n 'webpack:\s+\([^,]+,' next.config.js | cut -d',' -f1 | cut -d'(' -f2)
|
||||||
config.plugins.push(new webpack.DefinePlugin(envMap));
|
head -$(( ${WEBPACK_CONF_LINE} )) next.config.js > next.config.js.2
|
||||||
return config;
|
cat > next.config.js.3 <<EOF
|
||||||
},
|
$WEBPACK_CONF_VAR.plugins.push(new webpack.DefinePlugin(envMap));
|
||||||
EOF
|
EOF
|
||||||
|
NEXT_SECTION_LINE=$((WEBPACK_CONF_LINE - 1))
|
||||||
|
elif [ -n "$ENV_LINE" ]; then
|
||||||
|
head -$(( ${ENV_LINE} - 1 )) next.config.js > next.config.js.2
|
||||||
|
cat > next.config.js.3 <<EOF
|
||||||
|
webpack: (config) => {
|
||||||
|
config.plugins.push(new webpack.DefinePlugin(envMap));
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
EOF
|
||||||
|
NEXT_SECTION_ADJUSTMENT=2
|
||||||
|
NEXT_SECTION_LINE=$ENV_LINE
|
||||||
|
else
|
||||||
|
echo "WARNING: Cannot find location to insert environment variable map in next.config.js" 1>&2
|
||||||
|
rm -f next.config.js.*
|
||||||
|
NEXT_SECTION_LINE=0
|
||||||
|
fi
|
||||||
|
|
||||||
tail -$(( ${CONFIG_LINES} - ${MOD_EXPORTS_LINE} + 1 )) next.config.js | grep -v 'process\.env\.' > next.config.js.5
|
tail -$(( ${CONFIG_LINES} - ${NEXT_SECTION_LINE} + ${NEXT_SECTION_ADJUSTMENT} )) next.config.js > next.config.js.5
|
||||||
|
|
||||||
cat next.config.js.* | js-beautify > next.config.js
|
cat next.config.js.* | sed 's/^ *//g' | js-beautify | grep -v 'process\.\env\.' | js-beautify | tee 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
|
||||||
|
|
||||||
npm run build
|
npm install || exit 1
|
||||||
rm .env-list.json
|
npm run build || exit 1
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
Loading…
Reference in New Issue
Block a user