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

fix: make define-process-env-node-env alias node-env #3514

Merged
merged 4 commits into from Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion OPTIONS.md
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
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
@@ -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
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
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
15 changes: 14 additions & 1 deletion packages/webpack-cli/src/webpack-cli.ts
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 @@ -2177,6 +2188,8 @@ class WebpackCLI implements IWebpackCLI {
): Promise<WebpackCompiler> {
if (typeof options.defineProcessEnvNodeEnv === "string") {
process.env.NODE_ENV = options.defineProcessEnvNodeEnv;
CreativeTechGuy marked this conversation as resolved.
Show resolved Hide resolved
} 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
@@ -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
@@ -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
@@ -0,0 +1 @@
console.log('--node-env test');
6 changes: 6 additions & 0 deletions test/build/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()],
};