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

feat: add --node-env flag #2388

Merged
merged 3 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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'");
});
});
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions test/node-env/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('--node-env test');
6 changes: 6 additions & 0 deletions test/node-env/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const WebpackCLITestPlugin = require('../utils/webpack-cli-test-plugin');

module.exports = {
mode: process.env.NODE_ENV,
plugins: [new WebpackCLITestPlugin()],
};
snitin315 marked this conversation as resolved.
Show resolved Hide resolved