-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add JS config handling to webpack and jest configs #515
Conversation
* Jest config can now handle both JS and JSX env.configs * Webpack dev config will be able to assign devserver port based on the PORT in env.config.js
* Require env.config object after copying it into MFE
5cab127
to
d664423
Compare
const appEnvConfigPathJs = path.resolve(process.cwd(), './env.config.js'); | ||
const appEnvConfigPathJsx = path.resolve(process.cwd(), './env.config.jsx'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[curious] Is there a chance someone may want to use .ts
or .tsx
with the env.config
file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would! ;)
Or at the least, let me put // @ts-check
at the top of env.config.jsx
to turn on VS Code's TypeScript checking, and let me write the configuration with auto-completion and type checking in the IDE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use https://www.npmjs.com/package/glob to find the file with regex instead? It is easier to improve in the future.
config/webpack.dev.config.js
Outdated
const envConfigPathJs = path.resolve(process.cwd(), './env.config.js'); | ||
const envConfigPathJsx = path.resolve(process.cwd(), './env.config.jsx'); | ||
|
||
if (fs.existsSync(envConfigPathJs)) { | ||
envConfig = require(envConfigPathJs); | ||
} else if (fs.existsSync(envConfigPathJsx)) { | ||
envConfig = require(envConfigPathJsx); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] I wonder if it might make sense to create a helper/utility function (e.g., getEnvConfigPath
) that can be used both here and in jest.config.js
? E.g.
// jest.config.js
const envConfigPath = getEnvConfigPath();
// returns nothing if `env.config` doesn't exist; otherwise, returns path to appropriate `env.config`.
const envConfigPath = getEnvConfigPath();
if (envConfigPath) {
envConfig = require(envConfigPath);
}
// webpack.dev.config.js
let envConfig = {};
// returns nothing if `env.config` doesn't exist; otherwise, returns path to appropriate `env.config`.
const envConfigPath = getEnvConfigPath();
if (envConfigPath) {
envConfig = require(envConfigPath);
}
Related, I might also recommend adding support for envConfig
within webpack.dev-stage.config.js
file as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What Adam said.
Plus, we need to document somewhere in the README that the .js
file takes precedence if both exist.
config/webpack.prod.config.js
Outdated
@@ -45,12 +65,12 @@ if (process.env.ENABLE_NEW_RELIC !== 'false') { | |||
agentID: process.env.NEW_RELIC_AGENT_ID || 'undefined_agent_id', | |||
trustKey: process.env.NEW_RELIC_TRUST_KEY || 'undefined_trust_key', | |||
licenseKey: process.env.NEW_RELIC_LICENSE_KEY || 'undefined_license_key', | |||
applicationID: process.env.NEW_RELIC_APP_ID || 'undefined_application_id', | |||
applicationID: envConfig.NEW_RELIC_APP_ID || process.env.NEW_RELIC_APP_ID || 'undefined_application_id', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[curious] Would we expect the other process.env.NEW_RELIC_*
env vars might be usable with envConfig
as well (e.g., agentID
, trustKey
, licenseKey
, etc.)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are y'all looking into pluggifying the NEW_RELIC stuff, yet? It would be great for the project if we didn't have to deal with it so specifically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually went ahead and removed these changes as this would've meant using a require
statement in this config to fetch the environment variables, and that got in the way of using import
in the env.config.js
file.
This will mean that there's no clear solution yet for if we ever want to replace the process.env.ENV_VAR
s in this file or webpack.dev.config.js
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish I had a specific community use-case in mind for JS config beyond the Tubular usage at edx.org, but I can't object in principle: it offers significant additional flexibility, at least in theory. I also take it means it'll make pluggability more practical.
Nevertheless, we need to make sure it's not the primary use case, at least not until Tutor can make use of it.
README.md
Outdated
To use a private `env.config.js` file during the production build, the Webpack Production config will look for an env | ||
variable `process.env.JS_CONFIG_FILEPATH`, which should represent a file path to the desired `env.config.js`. | ||
|
||
The only requirement is that the filepath end with `env.config.*`, where either `.js` or `.jsx` as the extension |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only requirement is that the filepath end with `env.config.*`, where either `.js` or `.jsx` as the extension | |
The only requirement is that the filepath end with `env.config.*`, with either `.js` or `.jsx` as the extension. |
|
||
JS_CONFIG_FILEPATH="{HOME}/frontends/frontend-app-profile/stage.env.config.jsx" | ||
|
||
## Requiring Jest to reference env.config.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean that an env.config.js
will now be required for tests to run successfully?
Maybe it already is and I just missed it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, env.config.js
isn't required to run tests, but if there are any env variables or configuration in the env.config.js file that should be used for tests, this is what will be needed in the setupTest.js
file.
README.md
Outdated
@@ -218,6 +217,31 @@ locally. To serve a production build locally: | |||
attempt to run the build on the same port specified in the | |||
`env.config.js` file. | |||
|
|||
## Creating a Production Build with env.config.js (using Tubular) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only place Tubular is mentioned. I feel like we should either remove the reference entirely, or add a section that explains how to use it. I lean towards the former.
config/webpack.dev.config.js
Outdated
const envConfigPathJs = path.resolve(process.cwd(), './env.config.js'); | ||
const envConfigPathJsx = path.resolve(process.cwd(), './env.config.jsx'); | ||
|
||
if (fs.existsSync(envConfigPathJs)) { | ||
envConfig = require(envConfigPathJs); | ||
} else if (fs.existsSync(envConfigPathJsx)) { | ||
envConfig = require(envConfigPathJsx); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What Adam said.
Plus, we need to document somewhere in the README that the .js
file takes precedence if both exist.
config/webpack.prod.config.js
Outdated
@@ -45,12 +65,12 @@ if (process.env.ENABLE_NEW_RELIC !== 'false') { | |||
agentID: process.env.NEW_RELIC_AGENT_ID || 'undefined_agent_id', | |||
trustKey: process.env.NEW_RELIC_TRUST_KEY || 'undefined_trust_key', | |||
licenseKey: process.env.NEW_RELIC_LICENSE_KEY || 'undefined_license_key', | |||
applicationID: process.env.NEW_RELIC_APP_ID || 'undefined_application_id', | |||
applicationID: envConfig.NEW_RELIC_APP_ID || process.env.NEW_RELIC_APP_ID || 'undefined_application_id', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are y'all looking into pluggifying the NEW_RELIC stuff, yet? It would be great for the project if we didn't have to deal with it so specifically.
* this is due to the fact that if any 'import' was desired in env.config it would fail due to the 'require' of env.config
if (fs.existsSync(appEnvConfigPathJs)) { | ||
envConfigPath = appEnvConfigPathJs; | ||
} else if (fs.existsSync(appEnvConfigPathJsx)) { | ||
envConfigPath = appEnvConfigPathJsx; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specific reason this logic prioritizes .js
over .jsx
? I don't have strong feelings either way, but the decision around the prioritization should be documented.
Hello all! I have taken over this PR as Jason is out for the next couple weeks. I have read through comments/code and have some questions to help get me up to speed.
cc: @adamstankiewicz @arbrandes @jsnwesson @brian-smith-tcril @leangseu-edx |
As discussed in the last FWG meeting, I don't see the point of adding an |
Closed as we aren't going forward with changes to the |
This PR includes the following changes:
PORT
assigned inenv.config.js
. Otherwise checksprocess.env.PORT
or falls back to 8080.env.config.js
orenv.config.jsx
for environment variables.Resolves issues #513 and #514