From 2d9dd01a1259e1a9a820c39684d81d99723b2b72 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Mon, 1 Feb 2021 11:27:09 +0530 Subject: [PATCH 1/3] feat: add --node-env flag --- packages/webpack-cli/lib/webpack-cli.js | 15 +++++++++++++ test/node-env/node-env.test.js | 29 +++++++++++++++++++++++++ test/node-env/src/index.js | 1 + test/node-env/webpack.config.js | 6 +++++ 4 files changed, 51 insertions(+) create mode 100644 test/node-env/node-env.test.js create mode 100644 test/node-env/src/index.js create mode 100644 test/node-env/webpack.config.js diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 79f7070534c..3f2eb404468 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -295,6 +295,12 @@ class WebpackCLI { multiple: true, description: 'Environment passed to the configuration when it is a function.', }, + { + name: 'node-env', + type: String, + multiple: false, + description: 'Sets process.env.NODE_ENV to the specified value', + }, // Adding more plugins { @@ -429,6 +435,12 @@ class WebpackCLI { return options; } + applyNodeEnv(options) { + if (typeof options.nodeEnv === 'string') { + process.env.NODE_ENV = options.nodeEnv; + } + } + async run(args, parseOptions) { // Built-in internal commands const buildCommandOptions = { @@ -1655,6 +1667,9 @@ class WebpackCLI { async createCompiler(options, callback) { let config = await this.resolveConfig(options); + // apply process.env.NODE_ENV with the help of --node-env + this.applyNodeEnv(options); + config = await this.applyOptions(config, options); config = await this.applyCLIPlugin(config, options); diff --git a/test/node-env/node-env.test.js b/test/node-env/node-env.test.js new file mode 100644 index 00000000000..cfa41db1e7b --- /dev/null +++ b/test/node-env/node-env.test.js @@ -0,0 +1,29 @@ +'use strict'; + +const { run } = require('../utils/test-utils'); + +describe('--node-env flag', () => { + it('should set process.env.NODE_ENV to "production"', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'production']); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toContain("mode: 'production'"); + }); + + it('should set process.env.NODE_ENV to "development"', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'development']); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toContain("mode: 'development'"); + }); + + it('should set process.env.NODE_ENV to "none"', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'none']); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toContain("mode: 'none'"); + }); +}); diff --git a/test/node-env/src/index.js b/test/node-env/src/index.js new file mode 100644 index 00000000000..c70a2899e94 --- /dev/null +++ b/test/node-env/src/index.js @@ -0,0 +1 @@ +console.log('--node-env test'); \ No newline at end of file diff --git a/test/node-env/webpack.config.js b/test/node-env/webpack.config.js new file mode 100644 index 00000000000..ca74229899d --- /dev/null +++ b/test/node-env/webpack.config.js @@ -0,0 +1,6 @@ +const WebpackCLITestPlugin = require('../utils/webpack-cli-test-plugin'); + +module.exports = { + mode: process.env.NODE_ENV, + plugins: [new WebpackCLITestPlugin()], +}; From a924bf474795af02eec54151308d6b7715172946 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 2 Feb 2021 16:32:01 +0300 Subject: [PATCH 2/3] fix: logic --- packages/webpack-cli/lib/webpack-cli.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 3f2eb404468..02d98539331 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -1665,11 +1665,10 @@ class WebpackCLI { } async createCompiler(options, callback) { - let config = await this.resolveConfig(options); - - // apply process.env.NODE_ENV with the help of --node-env this.applyNodeEnv(options); + let config = await this.resolveConfig(options); + config = await this.applyOptions(config, options); config = await this.applyCLIPlugin(config, options); From eaf042b54c3af72569dc52c59799d23585912528 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 2 Feb 2021 16:52:34 +0300 Subject: [PATCH 3/3] fix: `none` env --- packages/webpack-cli/lib/webpack-cli.js | 2 +- test/node-env/auto-mode.config.js | 5 ++++ test/node-env/node-env.test.js | 34 +++++++++++++++++++++---- 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 test/node-env/auto-mode.config.js diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 02d98539331..15be68263dc 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -1529,7 +1529,7 @@ class WebpackCLI { !configOptions.mode && process.env && process.env.NODE_ENV && - (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'node') + (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'none') ) { configOptions.mode = process.env.NODE_ENV; } diff --git a/test/node-env/auto-mode.config.js b/test/node-env/auto-mode.config.js new file mode 100644 index 00000000000..385cb2ac035 --- /dev/null +++ b/test/node-env/auto-mode.config.js @@ -0,0 +1,5 @@ +const WebpackCLITestPlugin = require('../utils/webpack-cli-test-plugin'); + +module.exports = { + plugins: [new WebpackCLITestPlugin()], +}; diff --git a/test/node-env/node-env.test.js b/test/node-env/node-env.test.js index cfa41db1e7b..4d8bda5d328 100644 --- a/test/node-env/node-env.test.js +++ b/test/node-env/node-env.test.js @@ -3,7 +3,15 @@ const { run } = require('../utils/test-utils'); describe('--node-env flag', () => { - it('should set process.env.NODE_ENV to "production"', () => { + it('should set "process.env.NODE_ENV" to "development"', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'development']); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toContain("mode: 'development'"); + }); + + it('should set "process.env.NODE_ENV" to "production"', () => { const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'production']); expect(exitCode).toBe(0); @@ -11,16 +19,32 @@ describe('--node-env flag', () => { expect(stdout).toContain("mode: 'production'"); }); - it('should set process.env.NODE_ENV to "development"', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'development']); + it('should set "process.env.NODE_ENV" to "none"', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'none']); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toContain("mode: 'none'"); + }); + + it('should set "process.env.NODE_ENV" and the "mode" option to "development"', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'development', '--config', './auto-mode.config.js']); expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); expect(stdout).toContain("mode: 'development'"); }); - it('should set process.env.NODE_ENV to "none"', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'none']); + it('should set "process.env.NODE_ENV" and the "mode" option to "production"', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'production', '--config', './auto-mode.config.js']); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toContain("mode: 'production'"); + }); + + it('should set "process.env.NODE_ENV" and the "mode" option to "none"', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--node-env', 'none', '--config', './auto-mode.config.js']); expect(exitCode).toBe(0); expect(stderr).toBeFalsy();