From 0cd9b4280c110a639e13c69f4e82724d9423c26e Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 6 Nov 2023 21:55:55 -0600 Subject: [PATCH 1/6] Allow for runtime env. --- .gitignore | 3 ++- next.config.js | 15 ++++++++++----- package.json | 11 +++++++---- scripts/find-env.sh | 18 ++++++++++++++++++ scripts/replace-env.sh | 25 +++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 10 deletions(-) create mode 100755 scripts/find-env.sh create mode 100755 scripts/replace-env.sh diff --git a/.gitignore b/.gitignore index b538ae7..c239be4 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ # next.js /.next/ +/.next-r/ /out/ # production @@ -45,4 +46,4 @@ next-env.d.ts /public/sw.js.map /public/workbox-*.js.map /public/worker-*.js.map -/public/fallback-*.js \ No newline at end of file +/public/fallback-*.js diff --git a/next.config.js b/next.config.js index bc3bd17..257ef93 100644 --- a/next.config.js +++ b/next.config.js @@ -3,10 +3,15 @@ const withPWA = require('next-pwa')({ dest: 'public', }) +const webpack = require('webpack'); +const envjson = require('./.env-list.json'); + module.exports = withPWA({ - env: { - CERC_TEST_WEBAPP_CONFIG1: process.env.CERC_TEST_WEBAPP_CONFIG1, - CERC_TEST_WEBAPP_CONFIG2: process.env.CERC_TEST_WEBAPP_CONFIG2, - CERC_WEBAPP_DEBUG: process.env.CERC_WEBAPP_DEBUG, - }, + webpack: (config) => { + config.plugins.push(new webpack.DefinePlugin( + envjson.reduce((a, v) => { a[v] = `"CERC_RUNTIME_ENV_${v.split(/\./).pop()}"`; return a; }, {}) + )); + return config; + } }) + diff --git a/package.json b/package.json index 6335967..97d21e2 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,11 @@ { "private": true, "scripts": { - "dev": "next dev", - "build": "next build", - "start": "next start" + "find-env": "scripts/find-env.sh > .env-list.json", + "dev": "yarn find-env && next dev", + "build": "yarn find-env && next build", + "load-env": "scripts/replace-env.sh .next .next-r", + "start": "yarn load-env && next start .next-r" }, "dependencies": { "next": "latest", @@ -14,6 +16,7 @@ "devDependencies": { "@types/node": "17.0.4", "@types/react": "17.0.38", - "typescript": "4.5.4" + "typescript": "4.5.4", + "webpack": "^5.89.0" } } diff --git a/scripts/find-env.sh b/scripts/find-env.sh new file mode 100755 index 0000000..2394e06 --- /dev/null +++ b/scripts/find-env.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +TMPF=`mktemp` + + +for d in $(find . -maxdepth 1 -type d | grep -v '\./\.' | grep '/' | cut -d'/' -f2); do + egrep "/$d[/$]?" .gitignore >/dev/null 2>/dev/null + if [ $? -eq 0 ]; then + continue + fi + + for f in $(find $d -regex ".*.[tj]sx?$" -type f); do + cat $f | tr -s '[:blank:]' '\n' | tr -s '[{},()]' '\n' | egrep -o 'process.env.[A-Za-z0-9_]+' $f >> $TMPF + done +done + +cat $TMPF | sort -u | jq --raw-input . | jq --slurp . +rm -f $TMPF diff --git a/scripts/replace-env.sh b/scripts/replace-env.sh new file mode 100755 index 0000000..4413778 --- /dev/null +++ b/scripts/replace-env.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +SRC_DIR=$1 +TRG_DIR=$2 + +rm -rf $TRG_DIR +mkdir -p $TRG_DIR +cp -rp $SRC_DIR $TRG_DIR/ + +if [ -f ".env" ]; then + set -a + source .env + set +a +fi + +for f in $(find $TRG_DIR -regex ".*.[tj]sx?$" -type f | grep -v 'node_modules'); do + for e in $(cat $f | tr -s '[:blank:]' '\n' | tr -s '[{},()]' '\n' | egrep -o '^"CERC_RUNTIME_ENV[^\"]+"$'); do + orig_name=$(echo -n $e | sed 's/"//g') + cur_name=$(echo -n $orig_name | sed 's/CERC_RUNTIME_ENV_//g') + cur_val=$(echo -n "\$${cur_name}" | envsubst) + esc_val=$(sed 's/[&/\]/\\&/g' <<<"$cur_val") + echo "$cur_name=$cur_val" + sed -i "s/$orig_name/$esc_val/g" $f + done +done -- 2.45.2 From c8942a1d84aefd1e14acd13b9d5e3b01499e33e1 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 6 Nov 2023 21:57:29 -0600 Subject: [PATCH 2/6] ; --- next.config.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/next.config.js b/next.config.js index 257ef93..9f9810b 100644 --- a/next.config.js +++ b/next.config.js @@ -13,5 +13,4 @@ module.exports = withPWA({ )); return config; } -}) - +}); -- 2.45.2 From e68d207c43333735c8ce1303fdbbb32ba23e0add Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 6 Nov 2023 22:29:48 -0600 Subject: [PATCH 3/6] Better support for dev --- next.config.js | 28 +++++++++++++++++++++------- package.json | 5 +++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/next.config.js b/next.config.js index 9f9810b..0f578c7 100644 --- a/next.config.js +++ b/next.config.js @@ -4,13 +4,27 @@ const withPWA = require('next-pwa')({ }) const webpack = require('webpack'); -const envjson = require('./.env-list.json'); + +let envMap; +try { + // .env-list.json provides us a list of identifiers which should be replaced at runtime. + envMap = require('./.env-list.json').reduce((a, v) => { + a[v] = `"CERC_RUNTIME_ENV_${v.split(/\./).pop()}"`; + return a; + }, {}); +} catch { + // 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_')) { + a[`process.env.${v}`] = JSON.stringify(process.env[v]); + } + return a; + }, {}); +} module.exports = withPWA({ - webpack: (config) => { - config.plugins.push(new webpack.DefinePlugin( - envjson.reduce((a, v) => { a[v] = `"CERC_RUNTIME_ENV_${v.split(/\./).pop()}"`; return a; }, {}) - )); - return config; - } + webpack: (config) => { + config.plugins.push(new webpack.DefinePlugin(envMap)); + return config; + } }); diff --git a/package.json b/package.json index 97d21e2..58a05fd 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "private": true, "scripts": { - "find-env": "scripts/find-env.sh > .env-list.json", - "dev": "yarn find-env && next dev", "build": "yarn find-env && next build", + "clean": "rm -rf .next* .env-list.json", + "dev": "rm -f .env-list.json && next dev", + "find-env": "scripts/find-env.sh > .env-list.json", "load-env": "scripts/replace-env.sh .next .next-r", "start": "yarn load-env && next start .next-r" }, -- 2.45.2 From bd4d564a0d9914ea4a6228414a4605528f89695e Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 6 Nov 2023 22:37:31 -0600 Subject: [PATCH 4/6] .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c239be4..a41f98b 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ yarn-error.log* # local env files .env*.local +.env-list.json # vercel .vercel -- 2.45.2 From fd0b4185ddbe0eced33865651cda2eb31e0193ea Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 6 Nov 2023 22:46:33 -0600 Subject: [PATCH 5/6] Don't clobber env --- scripts/replace-env.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/replace-env.sh b/scripts/replace-env.sh index 4413778..de5e00d 100755 --- a/scripts/replace-env.sh +++ b/scripts/replace-env.sh @@ -8,9 +8,13 @@ mkdir -p $TRG_DIR cp -rp $SRC_DIR $TRG_DIR/ if [ -f ".env" ]; then + TMPENV=`mktemp` + declare -px > $TMPENV set -a source .env + source $TMPENV set +a + rm -f $TMPENV fi for f in $(find $TRG_DIR -regex ".*.[tj]sx?$" -type f | grep -v 'node_modules'); do -- 2.45.2 From f56d1c11261eb18b97a0a75cc77e7fc200a3672c Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 6 Nov 2023 22:48:53 -0600 Subject: [PATCH 6/6] space --- scripts/find-env.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/find-env.sh b/scripts/find-env.sh index 2394e06..2449d88 100755 --- a/scripts/find-env.sh +++ b/scripts/find-env.sh @@ -2,7 +2,6 @@ TMPF=`mktemp` - for d in $(find . -maxdepth 1 -type d | grep -v '\./\.' | grep '/' | cut -d'/' -f2); do egrep "/$d[/$]?" .gitignore >/dev/null 2>/dev/null if [ $? -eq 0 ]; then -- 2.45.2