-
-
Notifications
You must be signed in to change notification settings - Fork 27k
fix: improve DefinePlugin config #9171
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
fix: improve DefinePlugin config #9171
Conversation
This configuration is used in webpack's DefinePlugin which prefers keys that look like `process.env.REACT_APP_MY_ENV_VAR` instead of a nested object: https://webpack.js.org/plugins/define-plugin/ Prior to this change, webpack would replace all occurrences of `process.env` with a JS object containing every key in the environment that begins with `REACT_APP`. The expected behavior is that a reference to `process.env.REACT_APP_MY_ENV_VAR` is replaced with the value of just that environment variable.
I'm not sure i understand this change. Was there a bug you were experiencing? |
In the build output, webpack's DefinePlugin was replacing
instead of just replacing The fix is to build the config to |
Ah i'm with you now. Thanks for clarifying. |
Technically this is a breaking change since some apps might be directly referencing the full |
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'm not sure we want to make this change. Next.js used to use the process.env.${key}
approach instead of replacing the entirety of process.env
, and we recently switched to replacing the full process.env
object.
There's two primary reasons:
- Replacing individual properties breaks Object Destructuring. That means users won't be able to write
const { REACT_APP_MY_VAR } = process.env
and have it work anymore. - When instrumenting Create React App to perform server-side rendering, it's dangerous to not replace the entire
process.env
object as you'd accidentally allow variables without theREACT_APP_
prefix to leak into your server-rendered markup.
The latter point is the most serious.
The JavaScript minification step (Terser) should be properly handling replacing the object access and removing all of the unrelated values, so this should not result in a net bundle win.
If this change is actually making the bundled code smaller, it means Terser is misconfigured and we should fix that instead.
Based on @Timer's comments above I think it's best that we leave this behaviour as is and close this PR. |
@sdemjanenko Thanks for spending the time to send this PR! If you open an issue describing the specific problem you're trying to solve in detail we'd be more than happy to suggest a better approach or implement a patch. If the only problem is what you said above (#9171 (comment)), be sure you're not customizing any webpack configuration using |
我支持这个改动。 |
This configuration is used in webpack's DefinePlugin which prefers keys
that look like
process.env.REACT_APP_MY_ENV_VAR
instead of a nestedobject: https://webpack.js.org/plugins/define-plugin/
Prior to this change, webpack would replace all occurrences of
process.env
with a JS object containing every key in the environmentthat begins with
REACT_APP
.The expected behavior is that a reference to
process.env.REACT_APP_MY_ENV_VAR
is replaced with the value of justthat environment variable.