Skip to content

Commit

Permalink
fix: supply argv to config with functions (#1721)
Browse files Browse the repository at this point in the history
* fix: supply argv to config with functions

* tests: add tests for argv in config
  • Loading branch information
anshumanv committed Aug 1, 2020
1 parent 8623343 commit 2f05940
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Expand Up @@ -83,6 +83,7 @@ class ConfigGroup extends GroupHelper {
}

async finalize(moduleObj) {
const { argv } = this.args;
const newOptionsObject = {
outputOptions: {},
options: {},
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -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)) {
Expand All @@ -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: {
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions test/config/type/function-with-argv/a.js
@@ -0,0 +1 @@
console.log('Dio');
15 changes: 15 additions & 0 deletions 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();
});
});
10 changes: 10 additions & 0 deletions 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',
},
};
};

0 comments on commit 2f05940

Please sign in to comment.