diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 5fdb737542a..254351e8117 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -971,7 +971,8 @@ class WebpackCLI implements IWebpackCLI { }, ], multiple: false, - description: "Sets process.env.NODE_ENV to the specified value.", + description: + "Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration.", helpLevel: "minimum", }, { @@ -983,7 +984,7 @@ class WebpackCLI implements IWebpackCLI { ], multiple: false, description: - "Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`).", + "Sets process.env.NODE_ENV after loading the config, so it’s only available at runtime, not in the configuration.", helpLevel: "verbose", }, @@ -1645,9 +1646,14 @@ class WebpackCLI implements IWebpackCLI { ); if (possibleValues.length > 0) { - this.logger.raw( - `${bold("Possible values:")} ${JSON.stringify(possibleValues.join(" | "))}`, - ); + // Convert the possible values to a union type string + // ['mode', 'development', 'production'] => "'mode' | 'development' | 'production'" + // [false, 'eval'] => "false | 'eval'" + const possibleValuesUnionTypeString = possibleValues + .map((value) => (typeof value === "string" ? `'${value}'` : value)) + .join(" | "); + + this.logger.raw(`${bold("Possible values:")} ${possibleValuesUnionTypeString}`); } } @@ -2358,16 +2364,30 @@ class WebpackCLI implements IWebpackCLI { options: Partial, callback?: Callback<[Error | undefined, WebpackCLIStats | undefined]>, ): Promise { - 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") { + const isNodeEnvOptionDefined = typeof options.nodeEnv === "string"; + const isDefineProcessEnvNodeEnvOptionDefined = + typeof options.defineProcessEnvNodeEnv === "string"; + + if (isNodeEnvOptionDefined && isDefineProcessEnvNodeEnvOptionDefined) { + this.logger.error( + "You can't use both '--node-env' and '--define-process-env-node-env' options at the same time.", + ); + process.exit(2); + } + + // Setting before loading the config + if (isNodeEnvOptionDefined) { process.env.NODE_ENV = options.nodeEnv; } let config = await this.loadConfig(options); config = await this.buildConfig(config, options); + // Setting after loading the config + if (isDefineProcessEnvNodeEnvOptionDefined) { + process.env.NODE_ENV = options.defineProcessEnvNodeEnv; + } + let compiler: WebpackCompiler; try { diff --git a/test/build/define-process-env-node-env/auto-mode.config.js b/test/build/define-process-env-node-env/auto-mode.config.js deleted file mode 100644 index f68b5414666..00000000000 --- a/test/build/define-process-env-node-env/auto-mode.config.js +++ /dev/null @@ -1,5 +0,0 @@ -const WebpackCLITestPlugin = require("../../utils/webpack-cli-test-plugin"); - -module.exports = { - plugins: [new WebpackCLITestPlugin()], -}; diff --git a/test/build/define-process-env-node-env/define-process-env-node-env.test.js b/test/build/define-process-env-node-env/define-process-env-node-env.test.js index 814adf19991..e28c111b82a 100644 --- a/test/build/define-process-env-node-env/define-process-env-node-env.test.js +++ b/test/build/define-process-env-node-env/define-process-env-node-env.test.js @@ -3,6 +3,21 @@ const { run } = require("../../utils/test-utils"); describe("--define-process-env-node-env flag", () => { + const OLD_ENV = process.env; + + beforeEach(() => { + jest.resetModules(); + process.env = { ...OLD_ENV }; + // Ensure NODE_ENV is undefined before running the test + if (process.env.NODE_ENV) { + delete process.env.NODE_ENV; + } + }); + + afterAll(() => { + process.env = OLD_ENV; + }); + it('should set "process.env.NODE_ENV" to "development"', async () => { const { exitCode, stderr, stdout } = await run(__dirname, [ "--define-process-env-node-env", @@ -11,7 +26,8 @@ describe("--define-process-env-node-env flag", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - expect(stdout).toContain("mode: 'development'"); + expect(stdout).toContain("[webpack.config.js] process.env.NODE_ENV: undefined"); + expect(stdout).toContain("[WebpackCLITestPlugin] process.env.NODE_ENV: development"); }); it('should set "process.env.NODE_ENV" to "production"', async () => { @@ -22,7 +38,8 @@ describe("--define-process-env-node-env flag", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - expect(stdout).toContain("mode: 'production'"); + expect(stdout).toContain("[webpack.config.js] process.env.NODE_ENV: undefined"); + expect(stdout).toContain("[WebpackCLITestPlugin] process.env.NODE_ENV: production"); }); it('should set "process.env.NODE_ENV" to "none"', async () => { @@ -33,45 +50,22 @@ describe("--define-process-env-node-env flag", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - expect(stdout).toContain("mode: 'none'"); + expect(stdout).toContain("[webpack.config.js] process.env.NODE_ENV: undefined"); + expect(stdout).toContain("[WebpackCLITestPlugin] process.env.NODE_ENV: none"); }); - it('should set "process.env.NODE_ENV" and the "mode" option to "development"', async () => { + it('should throw error if both "--node-env" and "--define-process-env-node-env" are passed', async () => { const { exitCode, stderr, stdout } = await run(__dirname, [ - "--define-process-env-node-env", + "--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, [ "--define-process-env-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, [ - "--define-process-env-node-env", - "none", - "--config", - "./auto-mode.config.js", ]); - expect(exitCode).toBe(0); - expect(stderr).toBeFalsy(); - expect(stdout).toContain("mode: 'none'"); + expect(exitCode).toBe(2); + expect(stderr).toContain( + "You can't use both '--node-env' and '--define-process-env-node-env' options at the same time.", + ); + expect(stdout).toBeFalsy(); }); }); diff --git a/test/build/define-process-env-node-env/webpack.config.js b/test/build/define-process-env-node-env/webpack.config.js index 71e8ab21cc3..93de1c20a3e 100644 --- a/test/build/define-process-env-node-env/webpack.config.js +++ b/test/build/define-process-env-node-env/webpack.config.js @@ -1,6 +1,8 @@ const WebpackCLITestPlugin = require("../../utils/webpack-cli-test-plugin"); +console.log("[webpack.config.js] process.env.NODE_ENV:", process.env.NODE_ENV); + module.exports = { - mode: process.env.NODE_ENV, - plugins: [new WebpackCLITestPlugin()], + mode: "none", + plugins: [new WebpackCLITestPlugin(null, { showAll: false, envVariables: ["NODE_ENV"] })], }; diff --git a/test/help/__snapshots__/help.test.js.snap.devServer5.webpack5 b/test/help/__snapshots__/help.test.js.snap.devServer5.webpack5 index 473a5be356b..22cc24952a5 100644 --- a/test/help/__snapshots__/help.test.js.snap.devServer5.webpack5 +++ b/test/help/__snapshots__/help.test.js.snap.devServer5.webpack5 @@ -99,7 +99,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -156,7 +156,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -213,7 +213,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -269,7 +269,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -316,7 +316,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -363,7 +363,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -410,7 +410,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -457,7 +457,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -504,7 +504,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -831,7 +831,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -876,7 +876,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -921,7 +921,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -966,7 +966,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -1011,7 +1011,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -1056,7 +1056,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -1102,7 +1102,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -1159,7 +1159,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -1207,7 +1207,7 @@ exports[`help should show help information using the "help --cache-type" option: exports[`help should show help information using the "help --cache-type" option: stdout 1`] = ` "Usage: webpack --cache-type Description: In memory caching. Filesystem caching. -Possible values: "memory | filesystem" +Possible values: 'memory' | 'filesystem' To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1234,7 +1234,7 @@ exports[`help should show help information using the "help --mode" option: stder exports[`help should show help information using the "help --mode" option: stdout 1`] = ` "Usage: webpack --mode Description: Enable production optimizations or development hints. -Possible values: "development | production | none" +Possible values: 'development' | 'production' | 'none' To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1274,7 +1274,7 @@ exports[`help should show help information using the "help --stats" option: stde exports[`help should show help information using the "help --stats" option: stdout 1`] = ` "Usage: webpack --stats [value] Description: Stats options object or preset name. -Possible values: "none | summary | errors-only | errors-warnings | minimal | normal | detailed | verbose" +Possible values: 'none' | 'summary' | 'errors-only' | 'errors-warnings' | 'minimal' | 'normal' | 'detailed' | 'verbose' To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1289,7 +1289,7 @@ exports[`help should show help information using the "help --target" option: std "Usage: webpack --target Short: webpack -t Description: Environment to build for. Environment to build for. An array of environments to build for all of them when possible. -Possible values: "false" +Possible values: false To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1344,7 +1344,7 @@ exports[`help should show help information using the "help serve --mode" option: exports[`help should show help information using the "help serve --mode" option: stdout 1`] = ` "Usage: webpack serve --mode Description: Enable production optimizations or development hints. -Possible values: "development | production | none" +Possible values: 'development' | 'production' | 'none' To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1406,7 +1406,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. @@ -1461,7 +1461,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". - --node-env Sets process.env.NODE_ENV to the specified value. + --node-env Sets process.env.NODE_ENV before loading the config, making it accessible within the configuration. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. diff --git a/test/serve/basic/dev-server-false.multi.config.js b/test/serve/basic/dev-server-false.multi.config.js index e18304c1fe9..fe72bc80c0b 100644 --- a/test/serve/basic/dev-server-false.multi.config.js +++ b/test/serve/basic/dev-server-false.multi.config.js @@ -8,7 +8,9 @@ module.exports = [ devtool: false, target: "web", entry: "./src/index.js", - plugins: [new WebpackCLITestPlugin(["mode"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode"], { showAll: false, showHooks: "hooks.compilation.taps" }), + ], }, { name: "worker", diff --git a/test/serve/basic/dev-server-output-public-path.config.js b/test/serve/basic/dev-server-output-public-path.config.js index 338ceb1b075..f31d881d96e 100644 --- a/test/serve/basic/dev-server-output-public-path.config.js +++ b/test/serve/basic/dev-server-output-public-path.config.js @@ -8,5 +8,10 @@ module.exports = { publicPath: "/my-public-path/", }, devServer: devServerConfig, - plugins: [new WebpackCLITestPlugin(["mode", "output"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode", "output"], { + showAll: false, + showHooks: "hooks.compilation.taps", + }), + ], }; diff --git a/test/serve/basic/function-with-argv.config.js b/test/serve/basic/function-with-argv.config.js index 1e4b9fd6f57..3d58b010c2a 100644 --- a/test/serve/basic/function-with-argv.config.js +++ b/test/serve/basic/function-with-argv.config.js @@ -6,7 +6,9 @@ module.exports = (env, argv) => { return { mode: "development", devtool: false, - plugins: [new WebpackCLITestPlugin(["mode"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode"], { showAll: false, showHooks: "hooks.compilation.taps" }), + ], devServer: { client: { logging: "info", diff --git a/test/serve/basic/function-with-env.config.js b/test/serve/basic/function-with-env.config.js index 84896976970..3ad5e640050 100644 --- a/test/serve/basic/function-with-env.config.js +++ b/test/serve/basic/function-with-env.config.js @@ -6,7 +6,9 @@ module.exports = (env) => { return { mode: "development", devtool: false, - plugins: [new WebpackCLITestPlugin(["mode"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode"], { showAll: false, showHooks: "hooks.compilation.taps" }), + ], devServer: { client: { logging: "info", diff --git a/test/serve/basic/multi-dev-server-output-public-path.config.js b/test/serve/basic/multi-dev-server-output-public-path.config.js index 8cba5383127..b39693fad65 100644 --- a/test/serve/basic/multi-dev-server-output-public-path.config.js +++ b/test/serve/basic/multi-dev-server-output-public-path.config.js @@ -15,7 +15,12 @@ module.exports = [ logging: "info", }, }, - plugins: [new WebpackCLITestPlugin(["mode", "output"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode", "output"], { + showAll: false, + showHooks: "hooks.compilation.taps", + }), + ], }, { name: "two", @@ -27,6 +32,11 @@ module.exports = [ filename: "second-output/[name].js", }, devServer: devServerConfig, - plugins: [new WebpackCLITestPlugin(["mode", "output"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode", "output"], { + showAll: false, + showHooks: "hooks.compilation.taps", + }), + ], }, ]; diff --git a/test/serve/basic/multi-dev-server.config.js b/test/serve/basic/multi-dev-server.config.js index 5644e58f910..1a14380141f 100644 --- a/test/serve/basic/multi-dev-server.config.js +++ b/test/serve/basic/multi-dev-server.config.js @@ -15,7 +15,12 @@ module.exports = async () => [ ...devServerConfig, port: await getPort(), }, - plugins: [new WebpackCLITestPlugin(["mode", "output"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode", "output"], { + showAll: false, + showHooks: "hooks.compilation.taps", + }), + ], }, { name: "two", diff --git a/test/serve/basic/multi-output-public-path.config.js b/test/serve/basic/multi-output-public-path.config.js index 8a07533b041..47490526a9d 100644 --- a/test/serve/basic/multi-output-public-path.config.js +++ b/test/serve/basic/multi-output-public-path.config.js @@ -9,7 +9,12 @@ module.exports = [ publicPath: "/my-public-path/", filename: "first-output/[name].js", }, - plugins: [new WebpackCLITestPlugin(["mode", "output"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode", "output"], { + showAll: false, + showHooks: "hooks.compilation.taps", + }), + ], devServer: { client: { logging: "info", diff --git a/test/serve/basic/multi.config.js b/test/serve/basic/multi.config.js index 841d682573b..fb4ad291e31 100644 --- a/test/serve/basic/multi.config.js +++ b/test/serve/basic/multi.config.js @@ -10,7 +10,12 @@ module.exports = [ filename: "first-output/[name].js", }, devServer: devServerConfig, - plugins: [new WebpackCLITestPlugin(["mode", "output"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode", "output"], { + showAll: false, + showHooks: "hooks.compilation.taps", + }), + ], }, { name: "two", diff --git a/test/serve/basic/multiple-dev-server.config.js b/test/serve/basic/multiple-dev-server.config.js index 73fb17db397..e23e9295159 100644 --- a/test/serve/basic/multiple-dev-server.config.js +++ b/test/serve/basic/multiple-dev-server.config.js @@ -10,7 +10,7 @@ module.exports = [ filename: "first-output/[name].js", }, devServer: devServerConfig, - plugins: [new WebpackCLITestPlugin(["mode", "output"], false)], + plugins: [new WebpackCLITestPlugin(["mode", "output"], { showAll: false })], }, { name: "two", @@ -21,6 +21,6 @@ module.exports = [ filename: "first-output/[name].js", }, devServer: devServerConfig, - plugins: [new WebpackCLITestPlugin(["mode", "output"], false)], + plugins: [new WebpackCLITestPlugin(["mode", "output"], { showAll: false })], }, ]; diff --git a/test/serve/basic/output-public-path.config.js b/test/serve/basic/output-public-path.config.js index 473ec830343..3f117ac26ca 100644 --- a/test/serve/basic/output-public-path.config.js +++ b/test/serve/basic/output-public-path.config.js @@ -6,7 +6,12 @@ module.exports = { output: { publicPath: "/my-public-path/", }, - plugins: [new WebpackCLITestPlugin(["mode", "output"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode", "output"], { + showAll: false, + showHooks: "hooks.compilation.taps", + }), + ], devServer: { client: { logging: "info", diff --git a/test/serve/basic/serve.config.js b/test/serve/basic/serve.config.js index 90a3adb6cfd..8976987c4d0 100644 --- a/test/serve/basic/serve.config.js +++ b/test/serve/basic/serve.config.js @@ -3,7 +3,9 @@ const WebpackCLITestPlugin = require("../../utils/webpack-cli-test-plugin"); module.exports = { mode: "development", devtool: false, - plugins: [new WebpackCLITestPlugin(["mode"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode"], { showAll: false, showHooks: "hooks.compilation.taps" }), + ], devServer: { client: { logging: "info", diff --git a/test/serve/basic/watch.config.js b/test/serve/basic/watch.config.js index aa4ce1f6967..ca757ffb973 100644 --- a/test/serve/basic/watch.config.js +++ b/test/serve/basic/watch.config.js @@ -3,7 +3,9 @@ const WebpackCLITestPlugin = require("../../utils/webpack-cli-test-plugin"); module.exports = { mode: "development", devtool: false, - plugins: [new WebpackCLITestPlugin(["mode"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode"], { showAll: false, showHooks: "hooks.compilation.taps" }), + ], watch: true, devServer: { client: { diff --git a/test/serve/basic/webpack.config.js b/test/serve/basic/webpack.config.js index 90a3adb6cfd..8976987c4d0 100644 --- a/test/serve/basic/webpack.config.js +++ b/test/serve/basic/webpack.config.js @@ -3,7 +3,9 @@ const WebpackCLITestPlugin = require("../../utils/webpack-cli-test-plugin"); module.exports = { mode: "development", devtool: false, - plugins: [new WebpackCLITestPlugin(["mode"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode"], { showAll: false, showHooks: "hooks.compilation.taps" }), + ], devServer: { client: { logging: "info", diff --git a/test/serve/with-custom-port/webpack.config.js b/test/serve/with-custom-port/webpack.config.js index b745b59a799..776b4387f14 100644 --- a/test/serve/with-custom-port/webpack.config.js +++ b/test/serve/with-custom-port/webpack.config.js @@ -8,5 +8,7 @@ module.exports = { port: 1234, host: "0.0.0.0", }, - plugins: [new WebpackCLITestPlugin(["mode"], false, "hooks.compilation.taps")], + plugins: [ + new WebpackCLITestPlugin(["mode"], { showAll: false, showHooks: "hooks.compilation.taps" }), + ], }; diff --git a/test/utils/webpack-cli-test-plugin.js b/test/utils/webpack-cli-test-plugin.js index 9ed07714be2..ec01b1bd2a2 100644 --- a/test/utils/webpack-cli-test-plugin.js +++ b/test/utils/webpack-cli-test-plugin.js @@ -1,11 +1,19 @@ class WebpackCLITestPlugin { - constructor(options, showAll = true, showHooks) { - this.opts = options; + constructor(configOptions, pluginOptions = {}) { + const { showAll, showHooks, envVariables } = pluginOptions; + this.opts = configOptions; this.showAll = showAll; this.showHooks = showHooks; + this.envVariables = envVariables; } apply(compiler) { + if (Array.isArray(this.envVariables)) { + this.envVariables.forEach((envVariable) => { + console.log(`[WebpackCLITestPlugin] process.env.${envVariable}:`, process.env[envVariable]); + }); + } + compiler.hooks.done.tap("webpack-cli Test Plugin", () => { if (this.showHooks) { const identifiers = this.showHooks.split(".");