Skip to content

Commit

Permalink
feat: add the --node-env flag (#2388)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Feb 2, 2021
1 parent 2841cd7 commit e5126f1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -1517,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;
}
Expand Down Expand Up @@ -1653,6 +1665,8 @@ class WebpackCLI {
}

async createCompiler(options, callback) {
this.applyNodeEnv(options);

let config = await this.resolveConfig(options);

config = await this.applyOptions(config, options);
Expand Down
5 changes: 5 additions & 0 deletions test/node-env/auto-mode.config.js
@@ -0,0 +1,5 @@
const WebpackCLITestPlugin = require('../utils/webpack-cli-test-plugin');

module.exports = {
plugins: [new WebpackCLITestPlugin()],
};
53 changes: 53 additions & 0 deletions test/node-env/node-env.test.js
@@ -0,0 +1,53 @@
'use strict';

const { run } = require('../utils/test-utils');

describe('--node-env flag', () => {
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);
expect(stderr).toBeFalsy();
expect(stdout).toContain("mode: 'production'");
});

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" 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();
expect(stdout).toContain("mode: 'none'");
});
});
1 change: 1 addition & 0 deletions test/node-env/src/index.js
@@ -0,0 +1 @@
console.log('--node-env test');
6 changes: 6 additions & 0 deletions 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()],
};

0 comments on commit e5126f1

Please sign in to comment.