Skip to content

Commit 346a518

Browse files
fix: make define-process-env-node-env alias node-env (#3514)
* fix: make define-process-env-node-env alias node-env * test: update snapshots * test: update snapshots * fix: add todo
1 parent 3ec7b16 commit 346a518

File tree

12 files changed

+195
-79
lines changed

12 files changed

+195
-79
lines changed

OPTIONS.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Options:
1010
-m, --merge Merge two or more configurations using 'webpack-merge'.
1111
--disable-interpret Disable interpret for loading the config file.
1212
--env <value...> Environment passed to the configuration when it is a function.
13-
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value.
13+
--node-env <value> Sets process.env.NODE_ENV to the specified value.
14+
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)
1415
--analyze It invokes webpack-bundle-analyzer plugin to get bundle information.
1516
--progress [value] Print compilation progress during build.
1617
-j, --json [value] Prints result as JSON or store it in a file.

SERVE-OPTIONS-v4.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Options:
99
-m, --merge Merge two or more configurations using 'webpack-merge'.
1010
--disable-interpret Disable interpret for loading the config file.
1111
--env <value...> Environment passed to the configuration when it is a function.
12-
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value.
12+
--node-env <value> Sets process.env.NODE_ENV to the specified value.
13+
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)
1314
--analyze It invokes webpack-bundle-analyzer plugin to get bundle information.
1415
--progress [value] Print compilation progress during build.
1516
-j, --json [value] Prints result as JSON or store it in a file.

packages/generators/init-template/default/package.json.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = (isUsingDevServer) => {
22
const scripts = {
3-
build: "webpack --mode=production --define-process-env-node-env=production",
3+
build: "webpack --mode=production --node-env=production",
44
"build:dev": "webpack --mode=development",
5-
"build:prod": "webpack --mode=production --define-process-env-node-env=production",
5+
"build:prod": "webpack --mode=production --node-env=production",
66
watch: "webpack --watch",
77
};
88
if (isUsingDevServer) {

packages/generators/init-template/react/package.json.tpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"description": "My webpack project",
44
"name": "my-webpack-project",
55
"scripts": {
6-
"build": "webpack --mode=production --define-process-env-node-env=production",
6+
"build": "webpack --mode=production --node-env=production",
77
"build:dev": "webpack --mode=development",
8-
"build:prod": "webpack --mode=production --define-process-env-node-env=production",
8+
"build:prod": "webpack --mode=production --node-env=production",
99
"watch": "webpack --watch",
1010
"serve": "webpack serve"
1111
}

packages/webpack-cli/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ Options:
8484
-m, --merge Merge two or more configurations using 'webpack-merge'.
8585
--disable-interpret Disable interpret for loading the config file.
8686
--env <value...> Environment passed to the configuration when it is a function.
87-
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value.
87+
--node-env <value> Sets process.env.NODE_ENV to the specified value.
88+
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)
8889
--analyze It invokes webpack-bundle-analyzer plugin to get bundle information.
8990
--progress [value] Print compilation progress during build.
9091
-j, --json [value] Prints result as JSON or store it in a file.

packages/webpack-cli/src/webpack-cli.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ class WebpackCLI implements IWebpackCLI {
890890
description: "Environment passed to the configuration when it is a function.",
891891
},
892892
{
893-
name: "define-process-env-node-env",
893+
name: "node-env",
894894
configs: [
895895
{
896896
type: "string",
@@ -899,6 +899,17 @@ class WebpackCLI implements IWebpackCLI {
899899
multiple: false,
900900
description: "Sets process.env.NODE_ENV to the specified value.",
901901
},
902+
{
903+
name: "define-process-env-node-env",
904+
configs: [
905+
{
906+
type: "string",
907+
},
908+
],
909+
multiple: false,
910+
description:
911+
"Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)",
912+
},
902913

903914
// Adding more plugins
904915
{
@@ -2176,7 +2187,10 @@ class WebpackCLI implements IWebpackCLI {
21762187
callback?: Callback<[Error | undefined, WebpackCLIStats | undefined]>,
21772188
): Promise<WebpackCompiler> {
21782189
if (typeof options.defineProcessEnvNodeEnv === "string") {
2190+
// TODO: This should only set NODE_ENV for the runtime not for the config too. Change this during next breaking change.
21792191
process.env.NODE_ENV = options.defineProcessEnvNodeEnv;
2192+
} else if (typeof options.nodeEnv === "string") {
2193+
process.env.NODE_ENV = options.nodeEnv;
21802194
}
21812195

21822196
let config = await this.loadConfig(options);
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const WebpackCLITestPlugin = require("../../utils/webpack-cli-test-plugin");
2+
3+
module.exports = {
4+
plugins: [new WebpackCLITestPlugin()],
5+
};

test/build/node-env/node-env.test.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"use strict";
2+
3+
const { run } = require("../../utils/test-utils");
4+
5+
describe("--node-env flag", () => {
6+
it('should set "process.env.NODE_ENV" to "development"', async () => {
7+
const { exitCode, stderr, stdout } = await run(__dirname, ["--node-env", "development"]);
8+
9+
expect(exitCode).toBe(0);
10+
expect(stderr).toBeFalsy();
11+
expect(stdout).toContain("mode: 'development'");
12+
});
13+
14+
it('should set "process.env.NODE_ENV" to "production"', async () => {
15+
const { exitCode, stderr, stdout } = await run(__dirname, ["--node-env", "production"]);
16+
17+
expect(exitCode).toBe(0);
18+
expect(stderr).toBeFalsy();
19+
expect(stdout).toContain("mode: 'production'");
20+
});
21+
22+
it('should set "process.env.NODE_ENV" to "none"', async () => {
23+
const { exitCode, stderr, stdout } = await run(__dirname, ["--node-env", "none"]);
24+
25+
expect(exitCode).toBe(0);
26+
expect(stderr).toBeFalsy();
27+
expect(stdout).toContain("mode: 'none'");
28+
});
29+
30+
it('should set "process.env.NODE_ENV" and the "mode" option to "development"', async () => {
31+
const { exitCode, stderr, stdout } = await run(__dirname, [
32+
"--node-env",
33+
"development",
34+
"--config",
35+
"./auto-mode.config.js",
36+
]);
37+
38+
expect(exitCode).toBe(0);
39+
expect(stderr).toBeFalsy();
40+
expect(stdout).toContain("mode: 'development'");
41+
});
42+
43+
it('should set "process.env.NODE_ENV" and the "mode" option to "production"', async () => {
44+
const { exitCode, stderr, stdout } = await run(__dirname, [
45+
"--node-env",
46+
"production",
47+
"--config",
48+
"./auto-mode.config.js",
49+
]);
50+
51+
expect(exitCode).toBe(0);
52+
expect(stderr).toBeFalsy();
53+
expect(stdout).toContain("mode: 'production'");
54+
});
55+
56+
it('should set "process.env.NODE_ENV" and the "mode" option to "none"', async () => {
57+
const { exitCode, stderr, stdout } = await run(__dirname, [
58+
"--node-env",
59+
"none",
60+
"--config",
61+
"./auto-mode.config.js",
62+
]);
63+
64+
expect(exitCode).toBe(0);
65+
expect(stderr).toBeFalsy();
66+
expect(stdout).toContain("mode: 'none'");
67+
});
68+
});

test/build/node-env/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('--node-env test');

test/build/node-env/webpack.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const WebpackCLITestPlugin = require("../../utils/webpack-cli-test-plugin");
2+
3+
module.exports = {
4+
mode: process.env.NODE_ENV,
5+
plugins: [new WebpackCLITestPlugin()],
6+
};

0 commit comments

Comments
 (0)