Better support for dev

This commit is contained in:
Thomas E Lackey 2023-11-06 22:29:48 -06:00
parent c8942a1d84
commit e68d207c43
2 changed files with 24 additions and 9 deletions

View File

@ -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;
}
});

View File

@ -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"
},