Skip to content

Commit

Permalink
fix: log warning if --config-name is used without multiple configs
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Oct 5, 2020
1 parent e250b63 commit 5c38fab
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Expand Up @@ -163,21 +163,27 @@ const finalize = async (moduleObj, args) => {
const newOptions = configOptions(formattedEnv, args);
// When config function returns a promise, resolve it, if not it's resolved by default
newOptionsObject['options'] = await Promise.resolve(newOptions);
} else if (Array.isArray(configOptions) && configName) {
// In case of exporting multiple configurations, If you pass a name to --config-name flag,
// webpack will only build that specific configuration.
const namedOptions = configOptions.filter((opt) => configName.includes(opt.name));
if (namedOptions.length === 0) {
logger.error(`Configuration with name "${configName}" was not found.`);
process.exit(2);
} else if (configName) {
if (Array.isArray(configOptions) && configOptions.length > 1) {
// In case of exporting multiple configurations, If you pass a name to --config-name flag,
// webpack will only build that specific configuration.
const namedOptions = configOptions.filter((opt) => configName.includes(opt.name));
if (namedOptions.length === 0) {
logger.error(`Configuration with name "${configName}" was not found.`);
process.exit(2);
} else {
newOptionsObject['options'] = namedOptions;
}
} else {
newOptionsObject['options'] = namedOptions;
logger.warn('Multiple configurations not found. Please use "--config-name" with multiple configurations.');
newOptionsObject['options'] = configOptions;
}
} else {
if (Array.isArray(configOptions) && !configOptions.length) {
newOptionsObject['options'] = {};
return newOptionsObject;
}

newOptionsObject['options'] = configOptions;
}

Expand Down
6 changes: 6 additions & 0 deletions test/config-name/config-name.test.js
Expand Up @@ -43,4 +43,10 @@ describe('--config-name flag', () => {
expect(stderr).toContain('Configuration with name "test" was not found.');
expect(stdout).toBeFalsy();
});

it('should log warning if multiple configurations are not found', () => {
const { stderr, stdout } = run(__dirname, ['--config-name', 'test', '-c', 'single-config.js'], false);
expect(stderr).toContain('Multiple configurations not found. Please use "--config-name" with multiple configurations');
expect(stdout).toContain('dist-single.js');
});
});
8 changes: 8 additions & 0 deletions test/config-name/single-config.js
@@ -0,0 +1,8 @@
module.exports = {
output: {
filename: './dist-single.js',
},
name: 'first',
entry: './src/first.js',
mode: 'development',
};

0 comments on commit 5c38fab

Please sign in to comment.