Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: supply argv to config with functions #1721

Merged
merged 5 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Dio');
15 changes: 15 additions & 0 deletions test/config/type/function-with-argv/function-with-argv.test.js
Original file line number Diff line number Diff line change
@@ -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
anshumanv marked this conversation as resolved.
Show resolved Hide resolved
expect(existsSync(resolve(__dirname, './dist/dev.js'))).toBeTruthy();
});
});
10 changes: 10 additions & 0 deletions test/config/type/function-with-argv/webpack.config.js
Original file line number Diff line number Diff line change
@@ -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',
},
};
};