From e68d207c43333735c8ce1303fdbbb32ba23e0add Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 6 Nov 2023 22:29:48 -0600 Subject: [PATCH] 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" },