fix(webpack): interpolate process.env more verbosely to reduce bundle size with DefinePlugin#30826
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
b6eabb8 to
903c22a
Compare
|
View your CI Pipeline Execution ↗ for commit 6622631
☁️ Nx Cloud last updated this comment at |
|
@Coly010 could you help me. Should I try to fix this failed test, or it is just something flaky? |
cdc625e to
26aab59
Compare
|
@Coly010 I can't reproduce this bug in the job on my MacOS laptop: |
|
@claude can you take a look at a test? I can't reproduce it on my macOS machine |
73bf313 to
d4fc5bd
Compare
👷 Deploy request for nx-docs pending review.Visit the deploys page to approve it
|
d4fc5bd to
936f99a
Compare
The change to define environment variables individually
(e.g., 'process.env.KEY') broke environments where the process
global doesn't exist, such as Cypress component testing.
This adds a fallback 'process.env': '{}' to handle cases where
code accesses process.env directly while maintaining the bundle
size optimization from individual key definitions.
29e4d6b to
6622631
Compare
There was a problem hiding this comment.
Nx Cloud is proposing a fix for your failed CI:
These changes fix the webpack dev server timeout by ensuring process.env property access occurs on a single line. The PR's DefinePlugin format change (defining individual keys instead of a process.env object) broke multi-line property access patterns, causing DefinePlugin to replace process.env with "{}" and then attempt property access on a string. By consolidating the property access to single lines in the runtime plugin and utility files, webpack can now correctly match and replace the full expression.
We could not verify this fix.
Suggested Fix changes
diff --git a/packages/module-federation/src/utils/plugins/runtime-library-control.plugin.ts b/packages/module-federation/src/utils/plugins/runtime-library-control.plugin.ts
index 2f34a68603..6910394ee4 100644
--- a/packages/module-federation/src/utils/plugins/runtime-library-control.plugin.ts
+++ b/packages/module-federation/src/utils/plugins/runtime-library-control.plugin.ts
@@ -10,8 +10,9 @@ const runtimeStore: {
if (process.env.NX_MF_DEV_REMOTES) {
// process.env.NX_MF_DEV_REMOTES is replaced by an array value via DefinePlugin, even though the original value is a stringified array.
- runtimeStore.devRemotes = process.env
- .NX_MF_DEV_REMOTES as unknown as string[];
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore - Must be on single line for DefinePlugin to replace correctly
+ runtimeStore.devRemotes = process.env.NX_MF_DEV_REMOTES as unknown as string[];
}
const nxRuntimeLibraryControlPlugin: () => ModuleFederationRuntimePlugin =
diff --git a/packages/module-federation/src/with-module-federation/angular/utils.ts b/packages/module-federation/src/with-module-federation/angular/utils.ts
index 34c8b84695..ad17918414 100644
--- a/packages/module-federation/src/with-module-federation/angular/utils.ts
+++ b/packages/module-federation/src/with-module-federation/angular/utils.ts
@@ -71,8 +71,9 @@ export function getFunctionDeterminateRemoteUrl(
: 'remoteEntry.mjs';
return function (remote: string) {
- const mappedStaticRemotesFromEnv = process.env
- .NX_MF_DEV_SERVER_STATIC_REMOTES
+ // Must access process.env.NX_MF_DEV_SERVER_STATIC_REMOTES on single line for DefinePlugin to replace correctly
+ // prettier-ignore
+ const mappedStaticRemotesFromEnv = process.env.NX_MF_DEV_SERVER_STATIC_REMOTES
? JSON.parse(process.env.NX_MF_DEV_SERVER_STATIC_REMOTES)
: undefined;
if (mappedStaticRemotesFromEnv && mappedStaticRemotesFromEnv[remote]) {
diff --git a/packages/module-federation/src/with-module-federation/rspack/utils.ts b/packages/module-federation/src/with-module-federation/rspack/utils.ts
index b9a141eaf4..bf9752f048 100644
--- a/packages/module-federation/src/with-module-federation/rspack/utils.ts
+++ b/packages/module-federation/src/with-module-federation/rspack/utils.ts
@@ -22,8 +22,9 @@ export function getFunctionDeterminateRemoteUrl(isServer = false) {
const remoteEntry = isServer ? 'server/remoteEntry.js' : 'remoteEntry.js';
return function (remote: string) {
- const mappedStaticRemotesFromEnv = process.env
- .NX_MF_DEV_SERVER_STATIC_REMOTES
+ // Must access process.env.NX_MF_DEV_SERVER_STATIC_REMOTES on single line for DefinePlugin to replace correctly
+ // prettier-ignore
+ const mappedStaticRemotesFromEnv = process.env.NX_MF_DEV_SERVER_STATIC_REMOTES
? JSON.parse(process.env.NX_MF_DEV_SERVER_STATIC_REMOTES)
: undefined;
if (mappedStaticRemotesFromEnv && mappedStaticRemotesFromEnv[remote]) {
diff --git a/packages/module-federation/src/with-module-federation/webpack/utils.ts b/packages/module-federation/src/with-module-federation/webpack/utils.ts
index ae53ab1dfe..029ee8bf37 100644
--- a/packages/module-federation/src/with-module-federation/webpack/utils.ts
+++ b/packages/module-federation/src/with-module-federation/webpack/utils.ts
@@ -23,8 +23,9 @@ export function getFunctionDeterminateRemoteUrl(isServer: boolean = false) {
const remoteEntry = isServer ? 'server/remoteEntry.js' : 'remoteEntry.js';
return function (remote: string) {
- const mappedStaticRemotesFromEnv = process.env
- .NX_MF_DEV_SERVER_STATIC_REMOTES
+ // Must access process.env.NX_MF_DEV_SERVER_STATIC_REMOTES on single line for DefinePlugin to replace correctly
+ // prettier-ignore
+ const mappedStaticRemotesFromEnv = process.env.NX_MF_DEV_SERVER_STATIC_REMOTES
? JSON.parse(process.env.NX_MF_DEV_SERVER_STATIC_REMOTES)
: undefined;
if (mappedStaticRemotesFromEnv && mappedStaticRemotesFromEnv[remote]) {
Because this branch comes from a fork, it is not possible for us to apply fixes directly, but you can apply the changes locally using the available options below.
Apply changes locally with:
npx nx-cloud apply-locally QJYE-FuvS
Apply fix locally with your editor ↗ View interactive diff ↗
🎓 Learn more about Self-Healing CI on nx.dev
… size with DefinePlugin (#30826) ## Current Behavior When we prepare ENVs for the DefinePlugin, we are creating the `process.env` object. For example: ``` // .env NX_PUBLIC_VALUE1=1 NX_PUBLIC_VALUE2=2 NX_PUBLIC_VALUE3=3 ``` As result we will have: ```js { 'process.env': { "NX_PUBLIC_VALUE1": "1", "NX_PUBLIC_VALUE2": "2", "NX_PUBLIC_VALUE3": "3" } } ``` As a result, in the final bundle, we will replace process.env with this object. The issue: If I use all 3 values in my application DefinePlugin will inject this object 3 times, instead of injecting it once. It will look like that: ```js const a = { "NX_PUBLIC_VALUE1": "1", "NX_PUBLIC_VALUE2": "2", "NX_PUBLIC_VALUE3": "3" }.NX_PUBLIC_VALUE1 const b = { "NX_PUBLIC_VALUE1": "1", "NX_PUBLIC_VALUE2": "2", "NX_PUBLIC_VALUE3": "3" }.NX_PUBLIC_VALUE2 const c = { "NX_PUBLIC_VALUE1": "1", "NX_PUBLIC_VALUE2": "2", "NX_PUBLIC_VALUE3": "3" }.NX_PUBLIC_VALUE3 ``` ## Expected Behavior DefinePlugin injects values instead of env object in each place ```js const a = "1" const b = "2" const c = "3" ``` ## Fixes - fixed this issue for webpack - fixed this issue for storybook - fixed this issue for rspack TLDR: now we have object like so: ```js { "process.env.NX_PUBLIC_VALUE1": "1", "process.env.NX_PUBLIC_VALUE2": "2", "process.env.NX_PUBLIC_VALUE3": "3" } ``` --------- Co-authored-by: Colum Ferry <[email protected]>
|
This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request. |


Current Behavior
When we prepare ENVs for the DefinePlugin, we are creating the
process.envobject.For example:
As result we will have:
As a result, in the final bundle, we will replace process.env with this object.
The issue:
If I use all 3 values in my application DefinePlugin will inject this object 3 times, instead of injecting it once.
It will look like that:
Expected Behavior
DefinePlugin injects values instead of env object in each place
Fixes
TLDR:
now we have object like so: