-
Notifications
You must be signed in to change notification settings - Fork 64
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: JavaScript file configuration of apps #474
Merged
davidjoy
merged 7 commits into
openedx:master
from
davidjoy:davidjoy/feat_js_file_config
May 26, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f626eff
feat: JavaScript file configuration of apps
davidjoy a67784d
docs: Update docs/decisions/0007-javascript-file-configuration.rst
davidjoy bda306a
docs: JS file config ADR review feedback
davidjoy 1458037
docs: improving documentation around when to use config methods
davidjoy 7cc7155
test: improving test coverage for js file config
davidjoy e44f959
test: we actually need the env.config.js file for the example app
davidjoy 84adafd
docs: accepting the js file config ADR
davidjoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
Promote JavaScript file configuration and deprecate environment variable configuration | ||
====================================================================================== | ||
|
||
Status | ||
------ | ||
|
||
Accepted | ||
|
||
Context | ||
------- | ||
|
||
Our webpack build process allows us to set environment variables on the command | ||
line or via .env files. These environment variables are available in the | ||
application via ``process.env``. | ||
|
||
The implementation of this uses templatization and string interpolation to | ||
replace any instance of ``process.env.XXXX`` with the value of the environment | ||
variable named ``XXXX``. As an example, in our source code we may write:: | ||
|
||
const LMS_BASE_URL = process.env.LMS_BASE_URL; | ||
|
||
After the build process runs, the compiled source code will instead read:: | ||
|
||
const LMS_BASE_URL = 'http://localhost:18000'; | ||
|
||
Put another way, `process.env` is not actually an object available at runtime, | ||
it's a templatization token that helps the build replace it with a string | ||
literal. | ||
|
||
This approach has several important limitations: | ||
|
||
- There's no way to add variables without hard-coding process.env.XXXX | ||
somewhere in the file, complicating our ability to add additional | ||
application-specific configuration without explicitly merging it into the | ||
configuration document after it's been created in frontend-platform. | ||
- The method can *only* handle strings. | ||
|
||
Other data types are converted to strings:: | ||
|
||
# Build command: | ||
BOOLEAN_VAR=false NULL_VAR=null NUMBER_VAR=123 npm run build | ||
|
||
... | ||
|
||
// Source code: | ||
const BOOLEAN_VAR = process.env.BOOLEAN_VAR; | ||
const NULL_VAR = process.env.NULL_VAR; | ||
const NUMBER_VAR = process.env.NUMBER_VAR; | ||
|
||
... | ||
|
||
// Compiled source after the build runs: | ||
const BOOLEAN_VAR = "false"; | ||
const NULL_VAR = "null"; | ||
const NUMBER_VAR = "123"; | ||
|
||
This is not good! | ||
|
||
- It makes it very difficult to supply array and object configuration | ||
variables, and unreasonable to supply class or function config since we'd | ||
have to ``eval()`` them. | ||
|
||
Related to all this, frontend-platform has long had the ability to replace the | ||
implementations of its analytics, auth, and logging services, but no way to | ||
actually *configure* the app with a new implementation. Because of the above | ||
limitations, there's no reasonable way to configure a JavaScript class via | ||
environment variables. | ||
|
||
Decision | ||
-------- | ||
|
||
For the above reasons, we will deprecate environment variable configuration in | ||
favor of JavaScript file configuration. | ||
|
||
This method makes use of an ``env.config.js`` file to supply configuration | ||
variables to an application:: | ||
|
||
const config = { | ||
LMS_BASE_URL: 'http://localhost:18000', | ||
BOOLEAN_VAR: false, | ||
NULL_VAR: null, | ||
NUMBER_VAR: 123 | ||
}; | ||
|
||
export default config; | ||
|
||
This file is imported by the frontend-build webpack build process if it exists, | ||
and expected by frontend-platform as part of its initialization process. If the | ||
file doesn't exist, frontend-build falls back to importing an empty object for | ||
backwards compatibility. This functionality already exists today in | ||
frontend-build in preparation for using it here in frontend-platform. | ||
|
||
This interdependency creates a peerDependency for frontend-platform on `frontend-build v8.1.0 <frontend_build_810_>`_ or | ||
later. | ||
|
||
Using a JavaScript file for configuration is standard practice in the | ||
JavaScript/node community. Babel, webpack, eslint, Jest, etc., all accept | ||
configuration via JavaScript files (which we take advantage of in | ||
frontend-build), so there is ample precedent for using a .js file for | ||
configuration. | ||
|
||
In order to achieve deprecation of environment variable configuration, we will | ||
follow the deprecation process described in | ||
`OEP-21: Deprecation and Removal <oep21_>`_. In addition, we will add | ||
build-time warnings to frontend-build indicating the deprecation of environment | ||
variable configuration. Practically speaking, this will mean adjusting build | ||
processes throughout the community and in common tools like Tutor. | ||
|
||
Relationship to runtime configuration | ||
************************************* | ||
|
||
JavaScript file configuration is compatible with runtime MFE configuration. | ||
frontend-platform loads configuration in a predictable order: | ||
|
||
- environment variable config | ||
- optional handlers (commonly used to merge MFE-specific config in via additional | ||
process.env variables) | ||
- JS file config | ||
- runtime config | ||
|
||
In the end, runtime config wins. That said, JS file config solves some use | ||
cases that runtime config can't solve around extensibility and customization. | ||
|
||
In the future if we deprecate environment variable config, it's likely that | ||
we keep both JS file config and runtime configuration around. JS file config | ||
primarily to handle extensibility, and runtime config for everything else. | ||
|
||
Rejected Alternatives | ||
--------------------- | ||
|
||
Another option was to use JSON files for this purpose. This solves some of our | ||
issues (limited use of non-string primitive data types) but is otherwise not | ||
nearly as expressive or flexible as using a JavaScript file directly. | ||
Anecdotally, in the past frontend-build used JSON versions of many of | ||
its configuration files (Babel, eslint, jest) but over time they were all | ||
converted to JavaScript files so we could express more complicated | ||
configuration needs. Since one of the primary use cases and reasons we need a | ||
new configuration method is to allow developers to supply alternate | ||
implementations of frontend-platform's core services (analytics, logging), JSON | ||
was effectively a non-starter. | ||
|
||
.. _oep21: https://docs.openedx.org/projects/openedx-proposals/en/latest/processes/oep-0021-proc-deprecation.html | ||
.. _frontend_build_810: https://github.com/openedx/frontend-build/releases/tag/v8.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// NOTE: This file is used by the example app. frontend-build expects the file | ||
// to be in the root of the repository. This is not used by the actual frontend-platform library. | ||
// Also note that in an actual application this file would be added to .gitignore. | ||
const config = { | ||
JS_FILE_VAR: 'JS_FILE_VAR_VALUE_FOR_EXAMPLE_APP', | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 version of frontend-build that enables
env.config
as a magic import. So we didn't have a peer dependency here before, but we technically do now. It's unlikely to bite anyone since 8.1.0 was quite a while ago.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/thinking aloud] Is there a way we could treat
@edx/frontend-build
as an optional dependency instead (docs)? In theory, someone may have created or could create an MFE using@edx/frontend-platform
without@edx/frontend-build
; it feels like introducing the peer dependency is potentially a breaking change as a result, even though I agree it's probably fair to assume this won't bite anyone.I believe the challenge with treating
@edx/frontend-build
as anoptionalDependency
would be conditionally importingenv.config.js
and only using it, if the import was successful.FWIW, I don't feel too strongly about this suggestion; just thinking of potential alternatives that don't require a coupling with
@edx/frontend-build
for the Webpack implementation should consumers of@edx/frontend-platform
ever want to use a different implementation of Webpack than@edx/frontend-build
😃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.
For the record (Adam and I talked about this a bit offline) - the only way I could get
env.config
to work as an import was to use specialized webpack config (in frontend-build) to fallback to an empty import if the file couldn't be found. Since the entire mechanism is predicated on that behavior, and it's implemented in frontend-build, it feels a bit unavoidable to add the peer dependency. There's just not a way I've ever found to do this without that added magic (which was merged into frontend-build some time ago in openedx/frontend-build#200)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.
FWIW, we may have ended up needing to do something similar anyways given the direction this PR is headed to be able to fallback to a local Paragon version in the externally hosted Paragon CSS urls or if a externally hosted Paragon CSS urls fails to load, we need to expose the paths to the locally installed Paragon theme via frontend-build such that frontend-platform has access to them (otherwise, each MFE would have to pass args to
initialize
which feels like could be avoided).