From 2f0594084a2d676dfe0675e54e967099c201f30c Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 1 Aug 2020 22:37:30 +0530 Subject: [PATCH] fix: supply argv to config with functions (#1721) * fix: supply argv to config with functions * tests: add tests for argv in config --- packages/webpack-cli/lib/groups/ConfigGroup.js | 4 ++-- packages/webpack-cli/lib/webpack-cli.js | 6 +++--- test/config/type/function-with-argv/a.js | 1 + .../function-with-argv/function-with-argv.test.js | 15 +++++++++++++++ .../type/function-with-argv/webpack.config.js | 10 ++++++++++ 5 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 test/config/type/function-with-argv/a.js create mode 100644 test/config/type/function-with-argv/function-with-argv.test.js create mode 100644 test/config/type/function-with-argv/webpack.config.js diff --git a/packages/webpack-cli/lib/groups/ConfigGroup.js b/packages/webpack-cli/lib/groups/ConfigGroup.js index c4ad3ecf2c5..29f58bb681a 100644 --- a/packages/webpack-cli/lib/groups/ConfigGroup.js +++ b/packages/webpack-cli/lib/groups/ConfigGroup.js @@ -83,6 +83,7 @@ class ConfigGroup extends GroupHelper { } async finalize(moduleObj) { + const { argv } = this.args; const newOptionsObject = { outputOptions: {}, options: {}, @@ -103,7 +104,7 @@ class ConfigGroup extends GroupHelper { return envObject; }, {}); } - const newOptions = configOptions(formattedEnv); + const newOptions = configOptions(formattedEnv, argv); // When config function returns a promise, resolve it, if not it's resolved by default newOptionsObject['options'] = await Promise.resolve(newOptions); } else { @@ -131,7 +132,6 @@ class ConfigGroup extends GroupHelper { async resolveConfigFiles() { const { config, mode } = this.args; - if (config) { const configPath = resolve(process.cwd(), config); const configFiles = getConfigInfoFromFileName(configPath); diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 1ff8712b69a..aab0e10f6ab 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -82,7 +82,7 @@ class WebpackCLI extends GroupHelper { * * @returns {void} */ - resolveGroups() { + resolveGroups(parsedArgs) { let mode; // determine the passed mode for ConfigGroup if (this.groupMap.has(groups.ZERO_CONFIG_GROUP)) { @@ -108,7 +108,7 @@ class WebpackCLI extends GroupHelper { } case groups.CONFIG_GROUP: { const ConfigGroup = require('./groups/ConfigGroup'); - this.configGroup = new ConfigGroup([...value, { mode }]); + this.configGroup = new ConfigGroup([...value, { mode }, { argv: parsedArgs }]); break; } case groups.DISPLAY_GROUP: { @@ -253,7 +253,7 @@ class WebpackCLI extends GroupHelper { async processArgs(args, cliOptions) { this.setMappedGroups(args, cliOptions); - this.resolveGroups(); + this.resolveGroups(args); const groupResult = await this.runOptionGroups(); return groupResult; } diff --git a/test/config/type/function-with-argv/a.js b/test/config/type/function-with-argv/a.js new file mode 100644 index 00000000000..d2525d8ea73 --- /dev/null +++ b/test/config/type/function-with-argv/a.js @@ -0,0 +1 @@ +console.log('Dio'); diff --git a/test/config/type/function-with-argv/function-with-argv.test.js b/test/config/type/function-with-argv/function-with-argv.test.js new file mode 100644 index 00000000000..d13de6a1944 --- /dev/null +++ b/test/config/type/function-with-argv/function-with-argv.test.js @@ -0,0 +1,15 @@ +'use strict'; +const { existsSync } = 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, ['--mode', 'development'], false); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + expect(stdout).toContain("argv: { config: null, color: true, mode: 'development' }"); + // Should generate the appropriate files + expect(existsSync(resolve(__dirname, './dist/dev.js'))).toBeTruthy(); + }); +}); diff --git a/test/config/type/function-with-argv/webpack.config.js b/test/config/type/function-with-argv/webpack.config.js new file mode 100644 index 00000000000..7c313379be8 --- /dev/null +++ b/test/config/type/function-with-argv/webpack.config.js @@ -0,0 +1,10 @@ +module.exports = (env, argv) => { + console.log({ argv }); + const { mode } = argv; + return { + entry: './a.js', + output: { + filename: mode === 'production' ? 'prod.js' : 'dev.js', + }, + }; +};