core
core copied to clipboard
Entire process.env compiled into application code (webpack)
Describe the bug
With Webpack process.env the following syntax will result in a build's entire process.env map being converted to an object and embedded into the compiled code.
The following lines make three references to process.env in a way that will embed that object as an object literal three times in the compiled code.
https://github.com/module-federation/core/blob/33e1719fe8968e31589f58b3150c0a5e3fb5d8e9/packages/sdk/src/env.ts#L12-L16
Repro case
run npx nx run-many --target=build:webpack --projects=react_ts_remote
run grep -Rn apps/*/dist/remoteEntry.js -e NX_WORKSPACE_ROOT | wc -l
Expect: 0 string is not in the compiled source Actual: 3 string will be in three times (a manual search will show the extent of the issue with many values that have no place in compiled code)
Recommendation
The best solution here is to not perform null checks and only check for a string value which can be directly replaced by a compiler; unfortunately, I'm not sure if you can safely do that in all of the build environments the sdk runs in be it rspack, webpack or something else.
Alternatively, and probably the most cross-compilation compatible approach is to forbid process.env substitution and inject the debug or other options through other means.
Perhaps this needs a discussion thread.
Reproduction
n/a
Used Package Manager
npm
System Info
System:
OS: macOS 13.2.1
CPU: (8) x64 Intel(R) Core(TM) i7-1060NG7 CPU @ 1.20GHz
Memory: 1.92 GB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 20.9.0 - ~/.volta/tools/image/node/20.9.0/bin/node
Yarn: 1.22.21 - ~/.volta/bin/yarn
npm: 10.1.0 - ~/.volta/tools/image/node/20.9.0/bin/npm
Browsers:
Brave Browser: 123.1.64.113
Chrome: 124.0.6367.62
Safari: 16.3
Validations
- [X] Read the docs.
- [X] Read the common issues list.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a Module federation issue and not a framework-specific issue.
- [X] The provided reproduction is a minimal reproducible example of the bug.