-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Clarify interaction between --node-env
and mode
#7080
Conversation
The interpretation of the current description is ambiguous, as it pertains to how a configuration function can or cannot benefit from the use of `--node-env` as a way to set `mode`. One might interpret it as if that happens before the configuration function is invoked, and therefore should be available to use for logic like switching in the function. That would be wrong. This update makes it clear that the assignment of a default `mode` based on `--node-env` happens after the configuration function is executed. Related: webpack#4009 Fixes webpack#3992
|
@aoberoi is attempting to deploy a commit to the Webpack Docs Team on Vercel. A member of the Team first needs to authorize it. |
@@ -620,7 +620,7 @@ You can use `--node-env` option to set `process.env.NODE_ENV`, which is availabl | |||
npx webpack --node-env production # process.env.NODE_ENV = 'production' | |||
``` | |||
|
|||
T> The `mode` option would respect the `--node-env` option if you don't set it explicitly, i.e. `--node-env production` would set both `process.env.NODE_ENV` and `mode` to `'production'` | |||
T> When the `mode` option is not set in the configuration, the `--node-env` option can be used to set `mode`. For example, `--node-env production` would set both `process.env.NODE_ENV` and `mode` to `'production'`. If the configuration [exports a function](/configuration/configuration-types/#exporting-a-function), the `--node-env` value is assigned to `mode` after the function returns, so it will not be set in the function arguments (`env` and `argv`). |
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 you clarify
so it will not be set in the function arguments (
env
andargv
).
Because
const path = require("path");
module.exports = function test(env, argv) {
console.log(argv);
return {
entry: "./src/index.js",
devtool: false,
cache: {
type: "filesystem",
},
output: {
path: path.resolve(__dirname, "./dist"),
filename: "main.js",
},
};
};
Run
webpack --node-env production
Output:
{
nodeEnv: 'production',
env: { WEBPACK_BUNDLE: true, WEBPACK_BUILD: true }
}
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.
Or let's do it better:
so the
mode
option will not be set in the function arguments (env
andargv
). You need to use theargv.nodeEnv
option
The interpretation of the current description is ambiguous, as it pertains to how a configuration function can or cannot benefit from the use of
--node-env
as a way to setmode
. One might interpret it as if that happens before the configuration function is invoked, and therefore should be available to use for logic like switching in the function. That would be wrong. This update makes it clear that the assignment of a defaultmode
based on--node-env
happens after the configuration function is executed.Related: webpack/webpack-cli#4009
Fixes webpack/webpack-cli#3992