Merge pull request #252 from UniversityRadioYork/marks/build-baps

Rework BAPS build to avoid NODE_ENV problems
This commit is contained in:
Matthew Stratford 2021-11-03 00:15:19 +00:00 committed by GitHub
commit 7cfd48b003
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 11 deletions

View file

@ -14,15 +14,19 @@ if (!NODE_ENV) {
); );
} }
const BAPSICLE =
(process.env.REACT_APP_BAPSICLE_INTERFACE || "false") == "true";
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
const dotenvFiles = [ const dotenvFiles = [
`${paths.dotenv}.${NODE_ENV}.local`, `${paths.dotenv}.${NODE_ENV}.local`,
BAPSICLE && `${paths.dotenv}.baps-${NODE_ENV}`,
`${paths.dotenv}.${NODE_ENV}`, `${paths.dotenv}.${NODE_ENV}`,
// Don't include `.env.local` for `test` environment // Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same // since normally you expect tests to produce the same
// results for everyone // results for everyone
NODE_ENV !== "test" && `${paths.dotenv}.local`, NODE_ENV !== "test" && `${paths.dotenv}.local`,
paths.dotenv paths.dotenv,
].filter(Boolean); ].filter(Boolean);
// Load environment variables from .env* files. Suppress warnings using silent // Load environment variables from .env* files. Suppress warnings using silent
@ -30,11 +34,11 @@ const dotenvFiles = [
// that have already been set. Variable expansion is supported in .env files. // that have already been set. Variable expansion is supported in .env files.
// https://github.com/motdotla/dotenv // https://github.com/motdotla/dotenv
// https://github.com/motdotla/dotenv-expand // https://github.com/motdotla/dotenv-expand
dotenvFiles.forEach(dotenvFile => { dotenvFiles.forEach((dotenvFile) => {
if (fs.existsSync(dotenvFile)) { if (fs.existsSync(dotenvFile)) {
require("dotenv-expand")( require("dotenv-expand")(
require("dotenv").config({ require("dotenv").config({
path: dotenvFile path: dotenvFile,
}) })
); );
} }
@ -52,8 +56,8 @@ dotenvFiles.forEach(dotenvFile => {
const appDirectory = fs.realpathSync(process.cwd()); const appDirectory = fs.realpathSync(process.cwd());
process.env.NODE_PATH = (process.env.NODE_PATH || "") process.env.NODE_PATH = (process.env.NODE_PATH || "")
.split(path.delimiter) .split(path.delimiter)
.filter(folder => folder && !path.isAbsolute(folder)) .filter((folder) => folder && !path.isAbsolute(folder))
.map(folder => path.resolve(appDirectory, folder)) .map((folder) => path.resolve(appDirectory, folder))
.join(path.delimiter); .join(path.delimiter);
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
@ -62,7 +66,7 @@ const REACT_APP = /^REACT_APP_/i;
function getClientEnvironment(publicUrl) { function getClientEnvironment(publicUrl) {
const raw = Object.keys(process.env) const raw = Object.keys(process.env)
.filter(key => REACT_APP.test(key)) .filter((key) => REACT_APP.test(key))
.reduce( .reduce(
(env, key) => { (env, key) => {
env[key] = process.env[key]; env[key] = process.env[key];
@ -79,7 +83,7 @@ function getClientEnvironment(publicUrl) {
PUBLIC_URL: publicUrl, PUBLIC_URL: publicUrl,
// This is a bodge. // This is a bodge.
// See https://github.com/bunkat/later/issues/155 // See https://github.com/bunkat/later/issues/155
LATER_COV: false LATER_COV: false,
} }
); );
// Stringify all values so we can feed into Webpack DefinePlugin // Stringify all values so we can feed into Webpack DefinePlugin
@ -87,7 +91,7 @@ function getClientEnvironment(publicUrl) {
"process.env": Object.keys(raw).reduce((env, key) => { "process.env": Object.keys(raw).reduce((env, key) => {
env[key] = JSON.stringify(raw[key]); env[key] = JSON.stringify(raw[key]);
return env; return env;
}, {}) }, {}),
}; };
return { raw, stringified }; return { raw, stringified };

View file

@ -7,7 +7,9 @@ process.env.NODE_ENV = "production";
// If we want BAPS, specify it as the first command line argument. // If we want BAPS, specify it as the first command line argument.
var args = process.argv.slice(2); // Remove node start.js var args = process.argv.slice(2); // Remove node start.js
if (args.length > 0 && args[0] === "baps") { if (args.length > 0 && args[0] === "baps") {
process.env.NODE_ENV = "baps-production"; // We set this here first; later on, in env.js, we'll reference it to load in
// the other variables from .env.baps-${NODE_ENV}
process.env.REACT_APP_BAPSICLE_INTERFACE = "true";
} }
// Makes the script crash on unhandled rejections instead of silently // Makes the script crash on unhandled rejections instead of silently
@ -147,7 +149,11 @@ function build(previousFileSizes) {
console.log(); console.log();
} }
console.log("Creating an optimized production build..."); console.log(
`Creating an optimized production ${
process.env.REACT_APP_BAPSICLE_INTERFACE === "true" ? "BAPS" : "WebStudio"
} build...`
);
const compiler = webpack(config); const compiler = webpack(config);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View file

@ -7,7 +7,9 @@ process.env.NODE_ENV = "development";
// If we want BAPS, specify it as the first command line argument. // If we want BAPS, specify it as the first command line argument.
var args = process.argv.slice(2); // Remove node start.js var args = process.argv.slice(2); // Remove node start.js
if (args.length > 0 && args[0] === "baps") { if (args.length > 0 && args[0] === "baps") {
process.env.NODE_ENV = "baps-development"; // We set this here first; later on, in env.js, we'll reference it to load in
// the other variables from .env.baps-${NODE_ENV}
process.env.REACT_APP_BAPSICLE_INTERFACE = "true";
} }
// Makes the script crash on unhandled rejections instead of silently // Makes the script crash on unhandled rejections instead of silently