Skip to content

Commit e5126f1

Browse files
authored
feat: add the --node-env flag (#2388)
1 parent 2841cd7 commit e5126f1

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

packages/webpack-cli/lib/webpack-cli.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ class WebpackCLI {
295295
multiple: true,
296296
description: 'Environment passed to the configuration when it is a function.',
297297
},
298+
{
299+
name: 'node-env',
300+
type: String,
301+
multiple: false,
302+
description: 'Sets process.env.NODE_ENV to the specified value',
303+
},
298304

299305
// Adding more plugins
300306
{
@@ -429,6 +435,12 @@ class WebpackCLI {
429435
return options;
430436
}
431437

438+
applyNodeEnv(options) {
439+
if (typeof options.nodeEnv === 'string') {
440+
process.env.NODE_ENV = options.nodeEnv;
441+
}
442+
}
443+
432444
async run(args, parseOptions) {
433445
// Built-in internal commands
434446
const buildCommandOptions = {
@@ -1517,7 +1529,7 @@ class WebpackCLI {
15171529
!configOptions.mode &&
15181530
process.env &&
15191531
process.env.NODE_ENV &&
1520-
(process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'node')
1532+
(process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'none')
15211533
) {
15221534
configOptions.mode = process.env.NODE_ENV;
15231535
}
@@ -1653,6 +1665,8 @@ class WebpackCLI {
16531665
}
16541666

16551667
async createCompiler(options, callback) {
1668+
this.applyNodeEnv(options);
1669+
16561670
let config = await this.resolveConfig(options);
16571671

16581672
config = await this.applyOptions(config, options);

test/node-env/auto-mode.config.js

+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/node-env/node-env.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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"', () => {
7+
const { exitCode, stderr, stdout } = 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"', () => {
15+
const { exitCode, stderr, stdout } = 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"', () => {
23+
const { exitCode, stderr, stdout } = 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"', () => {
31+
const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'development', '--config', './auto-mode.config.js']);
32+
33+
expect(exitCode).toBe(0);
34+
expect(stderr).toBeFalsy();
35+
expect(stdout).toContain("mode: 'development'");
36+
});
37+
38+
it('should set "process.env.NODE_ENV" and the "mode" option to "production"', () => {
39+
const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'production', '--config', './auto-mode.config.js']);
40+
41+
expect(exitCode).toBe(0);
42+
expect(stderr).toBeFalsy();
43+
expect(stdout).toContain("mode: 'production'");
44+
});
45+
46+
it('should set "process.env.NODE_ENV" and the "mode" option to "none"', () => {
47+
const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'none', '--config', './auto-mode.config.js']);
48+
49+
expect(exitCode).toBe(0);
50+
expect(stderr).toBeFalsy();
51+
expect(stdout).toContain("mode: 'none'");
52+
});
53+
});

test/node-env/src/index.js

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

test/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)