Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

aoberoi
Copy link

@aoberoi aoberoi commented Dec 15, 2023

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/webpack-cli#4009

Fixes webpack/webpack-cli#3992

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
Copy link

linux-foundation-easycla bot commented Dec 15, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: aoberoi / name: Ankur Oberoi (d00cbec)

Copy link

vercel bot commented Dec 15, 2023

@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`).
Copy link
Member

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 and argv).

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 }
}

Copy link
Member

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 and argv). You need to use the argv.nodeEnv option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--node-env doesn't set the mode as expected
2 participants