From f2cae29d2cbea2abc1bd5501fbc4cf3bbc5a38e9 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 30 May 2020 15:37:22 +0530 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20support=20for?= =?UTF-8?q?=20env=20flag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webpack-cli/lib/groups/ConfigGroup.js | 7 +++-- packages/webpack-cli/lib/utils/cli-flags.js | 7 +++++ test/config/type/function-with-env/a.js | 1 + .../function-with-env.test.js | 27 +++++++++++++++++++ .../type/function-with-env/webpack.config.js | 17 ++++++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 test/config/type/function-with-env/a.js create mode 100644 test/config/type/function-with-env/function-with-env.test.js create mode 100644 test/config/type/function-with-env/webpack.config.js diff --git a/packages/webpack-cli/lib/groups/ConfigGroup.js b/packages/webpack-cli/lib/groups/ConfigGroup.js index 49737d576b6..bf75f70b74f 100644 --- a/packages/webpack-cli/lib/groups/ConfigGroup.js +++ b/packages/webpack-cli/lib/groups/ConfigGroup.js @@ -83,10 +83,9 @@ class ConfigGroup extends GroupHelper { } const configPath = moduleObj.path; const configOptions = moduleObj.content; - if (configOptions.length > 0) { - newOptionsObject['options'] = configOptions; - } else if (typeof configOptions === 'function') { - const newOptions = configOptions(); + if (typeof configOptions === 'function') { + // when config is a function, pass the env from args to the config function + const newOptions = configOptions(this.args.env); newOptionsObject['options'] = newOptions; } else { if (Array.isArray(configOptions) && !configOptions.length) { diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index b18df20a84d..5968a315268 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -240,6 +240,13 @@ module.exports = { group: DISPLAY_GROUP, description: 'It tells webpack to output all the information', }, + { + name: 'env', + usage: '--env', + type: String, + group: CONFIG_GROUP, + description: 'It tells webpack to output all the information', + }, /* { name: "analyze", type: Boolean, diff --git a/test/config/type/function-with-env/a.js b/test/config/type/function-with-env/a.js new file mode 100644 index 00000000000..ebe8dc7e539 --- /dev/null +++ b/test/config/type/function-with-env/a.js @@ -0,0 +1 @@ +console.log('chuntaro'); diff --git a/test/config/type/function-with-env/function-with-env.test.js b/test/config/type/function-with-env/function-with-env.test.js new file mode 100644 index 00000000000..d4721d7417c --- /dev/null +++ b/test/config/type/function-with-env/function-with-env.test.js @@ -0,0 +1,27 @@ +'use strict'; +const { stat } = require('fs'); +const { resolve } = require('path'); +const { run } = require('../../../utils/test-utils'); + +describe('function configuration', () => { + it('is able to understand a configuration file as a function', () => { + const { stderr, stdout } = run(__dirname, ['--env', 'isProd']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + // Should generate the appropriate files + stat(resolve(__dirname, './bin/prod.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + }); + }); + it('is able to understand a configuration file as a function', () => { + const { stderr, stdout } = run(__dirname, ['--env', 'isDev']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + // Should generate the appropriate files + stat(resolve(__dirname, './bin/dev.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + }); + }); +}); diff --git a/test/config/type/function-with-env/webpack.config.js b/test/config/type/function-with-env/webpack.config.js new file mode 100644 index 00000000000..c2275d1b48f --- /dev/null +++ b/test/config/type/function-with-env/webpack.config.js @@ -0,0 +1,17 @@ +module.exports = (env) => { + if (env === 'isProd') { + return { + entry: './a.js', + output: { + filename: 'prod.js', + }, + }; + } + return { + entry: './a.js', + mode: 'development', + output: { + filename: 'dev.js', + }, + }; +}; From f0fbc098bd5e8726bbd946cafa00591f751ff0fd Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 30 May 2020 16:14:13 +0530 Subject: [PATCH 2/3] chore: update env flag desc --- packages/webpack-cli/lib/utils/cli-flags.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 5968a315268..29423b6fc80 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -245,7 +245,7 @@ module.exports = { usage: '--env', type: String, group: CONFIG_GROUP, - description: 'It tells webpack to output all the information', + description: 'Environment passed to the configuration when it is a function', }, /* { name: "analyze", From 9a71c3f6e277305989a25e44e73c724cd8f71211 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Mon, 1 Jun 2020 18:16:30 +0530 Subject: [PATCH 3/3] docs: add env flag in docs --- packages/webpack-cli/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/webpack-cli/README.md b/packages/webpack-cli/README.md index 7b7a8b42e7e..440194c761f 100644 --- a/packages/webpack-cli/README.md +++ b/packages/webpack-cli/README.md @@ -39,6 +39,7 @@ Options -c, --config string Provide path to a webpack configuration file -m, --merge string Merge a configuration file using webpack-merge --progress Print compilation progress during build + --env Environment passed to the configuration when it is a function --silent Disable any output that webpack makes --help Outputs list of supported flags -o, --output string Output location of the file generated by webpack