Skip to content

Commit

Permalink
fix: make define-process-env-node-env alias node-env (#3514)
Browse files Browse the repository at this point in the history
* fix: make define-process-env-node-env alias node-env

* test: update snapshots

* test: update snapshots

* fix: add todo
  • Loading branch information
CreativeTechGuy authored Dec 4, 2022
1 parent 3ec7b16 commit 346a518
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 79 deletions.
3 changes: 2 additions & 1 deletion OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Options:
-m, --merge Merge two or more configurations using 'webpack-merge'.
--disable-interpret Disable interpret for loading the config file.
--env <value...> Environment passed to the configuration when it is a function.
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value.
--node-env <value> Sets process.env.NODE_ENV to the specified value.
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)
--analyze It invokes webpack-bundle-analyzer plugin to get bundle information.
--progress [value] Print compilation progress during build.
-j, --json [value] Prints result as JSON or store it in a file.
Expand Down
3 changes: 2 additions & 1 deletion SERVE-OPTIONS-v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Options:
-m, --merge Merge two or more configurations using 'webpack-merge'.
--disable-interpret Disable interpret for loading the config file.
--env <value...> Environment passed to the configuration when it is a function.
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value.
--node-env <value> Sets process.env.NODE_ENV to the specified value.
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)
--analyze It invokes webpack-bundle-analyzer plugin to get bundle information.
--progress [value] Print compilation progress during build.
-j, --json [value] Prints result as JSON or store it in a file.
Expand Down
4 changes: 2 additions & 2 deletions packages/generators/init-template/default/package.json.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = (isUsingDevServer) => {
const scripts = {
build: "webpack --mode=production --define-process-env-node-env=production",
build: "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --define-process-env-node-env=production",
"build:prod": "webpack --mode=production --node-env=production",
watch: "webpack --watch",
};
if (isUsingDevServer) {
Expand Down
4 changes: 2 additions & 2 deletions packages/generators/init-template/react/package.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"description": "My webpack project",
"name": "my-webpack-project",
"scripts": {
"build": "webpack --mode=production --define-process-env-node-env=production",
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --define-process-env-node-env=production",
"build:prod": "webpack --mode=production --node-env=production",
"watch": "webpack --watch",
"serve": "webpack serve"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/webpack-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ Options:
-m, --merge Merge two or more configurations using 'webpack-merge'.
--disable-interpret Disable interpret for loading the config file.
--env <value...> Environment passed to the configuration when it is a function.
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value.
--node-env <value> Sets process.env.NODE_ENV to the specified value.
--define-process-env-node-env <value> Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)
--analyze It invokes webpack-bundle-analyzer plugin to get bundle information.
--progress [value] Print compilation progress during build.
-j, --json [value] Prints result as JSON or store it in a file.
Expand Down
16 changes: 15 additions & 1 deletion packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ class WebpackCLI implements IWebpackCLI {
description: "Environment passed to the configuration when it is a function.",
},
{
name: "define-process-env-node-env",
name: "node-env",
configs: [
{
type: "string",
Expand All @@ -899,6 +899,17 @@ class WebpackCLI implements IWebpackCLI {
multiple: false,
description: "Sets process.env.NODE_ENV to the specified value.",
},
{
name: "define-process-env-node-env",
configs: [
{
type: "string",
},
],
multiple: false,
description:
"Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)",
},

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

let config = await this.loadConfig(options);
Expand Down
5 changes: 5 additions & 0 deletions test/build/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()],
};
68 changes: 68 additions & 0 deletions test/build/node-env/node-env.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"use strict";

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

describe("--node-env flag", () => {
it('should set "process.env.NODE_ENV" to "development"', async () => {
const { exitCode, stderr, stdout } = await 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"', async () => {
const { exitCode, stderr, stdout } = await 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"', async () => {
const { exitCode, stderr, stdout } = await 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"', async () => {
const { exitCode, stderr, stdout } = await 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"', async () => {
const { exitCode, stderr, stdout } = await 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"', async () => {
const { exitCode, stderr, stdout } = await 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/build/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/build/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()],
};
Loading

0 comments on commit 346a518

Please sign in to comment.