Skip to content

Commit f653409

Browse files
authored
fix: log error if --config-name is used without multiple configs (#1874)
1 parent 604bd40 commit f653409

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,27 @@ const finalize = async (moduleObj, args) => {
163163
const newOptions = configOptions(formattedEnv, args);
164164
// When config function returns a promise, resolve it, if not it's resolved by default
165165
newOptionsObject['options'] = await Promise.resolve(newOptions);
166-
} else if (Array.isArray(configOptions) && configName) {
167-
// In case of exporting multiple configurations, If you pass a name to --config-name flag,
168-
// webpack will only build that specific configuration.
169-
const namedOptions = configOptions.filter((opt) => configName.includes(opt.name));
170-
if (namedOptions.length === 0) {
171-
logger.error(`Configuration with name "${configName}" was not found.`);
172-
process.exit(2);
166+
} else if (configName) {
167+
if (Array.isArray(configOptions) && configOptions.length > 1) {
168+
// In case of exporting multiple configurations, If you pass a name to --config-name flag,
169+
// webpack will only build that specific configuration.
170+
const namedOptions = configOptions.filter((opt) => configName.includes(opt.name));
171+
if (namedOptions.length === 0) {
172+
logger.error(`Configuration with name "${configName}" was not found.`);
173+
process.exit(2);
174+
} else {
175+
newOptionsObject['options'] = namedOptions;
176+
}
173177
} else {
174-
newOptionsObject['options'] = namedOptions;
178+
logger.error('Multiple configurations not found. Please use "--config-name" with multiple configurations.');
179+
process.exit(2);
175180
}
176181
} else {
177182
if (Array.isArray(configOptions) && !configOptions.length) {
178183
newOptionsObject['options'] = {};
179184
return newOptionsObject;
180185
}
186+
181187
newOptionsObject['options'] = configOptions;
182188
}
183189

test/config-name/config-name.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ describe('--config-name flag', () => {
4343
expect(stderr).toContain('Configuration with name "test" was not found.');
4444
expect(stdout).toBeFalsy();
4545
});
46+
47+
it('should log error if multiple configurations are not found', () => {
48+
const { stderr, stdout } = run(__dirname, ['--config-name', 'test', '-c', 'single-config.js'], false);
49+
expect(stderr).toContain('Multiple configurations not found. Please use "--config-name" with multiple configurations');
50+
expect(stdout).toBeFalsy();
51+
});
4652
});

test/config-name/single-config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
output: {
3+
filename: './dist-single.js',
4+
},
5+
name: 'first',
6+
entry: './src/first.js',
7+
mode: 'development',
8+
};

0 commit comments

Comments
 (0)