Skip to content

Commit 2f05940

Browse files
authored
fix: supply argv to config with functions (#1721)
* fix: supply argv to config with functions * tests: add tests for argv in config
1 parent 8623343 commit 2f05940

5 files changed

Lines changed: 31 additions & 5 deletions

File tree

packages/webpack-cli/lib/groups/ConfigGroup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class ConfigGroup extends GroupHelper {
8383
}
8484

8585
async finalize(moduleObj) {
86+
const { argv } = this.args;
8687
const newOptionsObject = {
8788
outputOptions: {},
8889
options: {},
@@ -103,7 +104,7 @@ class ConfigGroup extends GroupHelper {
103104
return envObject;
104105
}, {});
105106
}
106-
const newOptions = configOptions(formattedEnv);
107+
const newOptions = configOptions(formattedEnv, argv);
107108
// When config function returns a promise, resolve it, if not it's resolved by default
108109
newOptionsObject['options'] = await Promise.resolve(newOptions);
109110
} else {
@@ -131,7 +132,6 @@ class ConfigGroup extends GroupHelper {
131132

132133
async resolveConfigFiles() {
133134
const { config, mode } = this.args;
134-
135135
if (config) {
136136
const configPath = resolve(process.cwd(), config);
137137
const configFiles = getConfigInfoFromFileName(configPath);

packages/webpack-cli/lib/webpack-cli.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class WebpackCLI extends GroupHelper {
8282
*
8383
* @returns {void}
8484
*/
85-
resolveGroups() {
85+
resolveGroups(parsedArgs) {
8686
let mode;
8787
// determine the passed mode for ConfigGroup
8888
if (this.groupMap.has(groups.ZERO_CONFIG_GROUP)) {
@@ -108,7 +108,7 @@ class WebpackCLI extends GroupHelper {
108108
}
109109
case groups.CONFIG_GROUP: {
110110
const ConfigGroup = require('./groups/ConfigGroup');
111-
this.configGroup = new ConfigGroup([...value, { mode }]);
111+
this.configGroup = new ConfigGroup([...value, { mode }, { argv: parsedArgs }]);
112112
break;
113113
}
114114
case groups.DISPLAY_GROUP: {
@@ -253,7 +253,7 @@ class WebpackCLI extends GroupHelper {
253253

254254
async processArgs(args, cliOptions) {
255255
this.setMappedGroups(args, cliOptions);
256-
this.resolveGroups();
256+
this.resolveGroups(args);
257257
const groupResult = await this.runOptionGroups();
258258
return groupResult;
259259
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Dio');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
const { existsSync } = require('fs');
3+
const { resolve } = require('path');
4+
const { run } = require('../../../utils/test-utils');
5+
6+
describe('function configuration', () => {
7+
it('is able to understand a configuration file as a function', () => {
8+
const { stderr, stdout } = run(__dirname, ['--mode', 'development'], false);
9+
expect(stderr).toBeFalsy();
10+
expect(stdout).toBeTruthy();
11+
expect(stdout).toContain("argv: { config: null, color: true, mode: 'development' }");
12+
// Should generate the appropriate files
13+
expect(existsSync(resolve(__dirname, './dist/dev.js'))).toBeTruthy();
14+
});
15+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = (env, argv) => {
2+
console.log({ argv });
3+
const { mode } = argv;
4+
return {
5+
entry: './a.js',
6+
output: {
7+
filename: mode === 'production' ? 'prod.js' : 'dev.js',
8+
},
9+
};
10+
};

0 commit comments

Comments
 (0)