Skip to content

Commit 29ecf8d

Browse files
authored
fix: --config-name behaviour for fuctional configs (#2006)
1 parent c261e65 commit 29ecf8d

17 files changed

+166
-13
lines changed

packages/webpack-cli/__tests__/ConfigGroup/ConfigGroup.test.js packages/webpack-cli/__tests__/resolveConfig/resolveConfig.test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const ConfigGroup = require('../../lib/groups/ConfigGroup');
1+
const resolveConfig = require('../../lib/groups/resolveConfig.js');
22
const { resolve } = require('path');
33
const config1 = require('./webpack.config1.cjs');
44
const config2 = require('./webpack.config2.cjs');
55
const arrayConfig = require('./webpack.config.cjs');
66
const promiseConfig = require('./webpack.promise.config.cjs');
77

8-
describe('ConfigGroup', function () {
8+
describe('resolveConfig', function () {
99
it('should handle merge properly', async () => {
10-
const result = await ConfigGroup({
10+
const result = await resolveConfig({
1111
merge: true,
1212
config: [resolve(__dirname, './webpack.config.cjs')],
1313
});
@@ -25,7 +25,7 @@ describe('ConfigGroup', function () {
2525
});
2626

2727
it('should return array for multiple config', async () => {
28-
const result = await ConfigGroup({
28+
const result = await resolveConfig({
2929
config: [resolve(__dirname, './webpack.config1.cjs'), resolve(__dirname, './webpack.config2.cjs')],
3030
});
3131
const expectedOptions = [config1, config2];
@@ -34,20 +34,20 @@ describe('ConfigGroup', function () {
3434
});
3535

3636
it('should return config object for single config', async () => {
37-
const result = await ConfigGroup({ config: [resolve(__dirname, './webpack.config1.cjs')] });
37+
const result = await resolveConfig({ config: [resolve(__dirname, './webpack.config1.cjs')] });
3838
expect(result.options).toEqual(config1);
3939
expect(result.outputOptions).toEqual({});
4040
});
4141

4242
it('should return resolved config object for promise config', async () => {
43-
const result = await ConfigGroup({ config: [resolve(__dirname, './webpack.promise.config.cjs')] });
43+
const result = await resolveConfig({ config: [resolve(__dirname, './webpack.promise.config.cjs')] });
4444
const expectedOptions = await promiseConfig();
4545
expect(result.options).toEqual(expectedOptions);
4646
expect(result.outputOptions).toEqual({});
4747
});
4848

4949
it('should handle configs returning different types', async () => {
50-
const result = await ConfigGroup({
50+
const result = await resolveConfig({
5151
config: [resolve(__dirname, './webpack.promise.config.cjs'), resolve(__dirname, './webpack.config.cjs')],
5252
});
5353
const resolvedPromiseConfig = await promiseConfig();
@@ -57,7 +57,7 @@ describe('ConfigGroup', function () {
5757
});
5858

5959
it('should handle different env formats', async () => {
60-
const result = await ConfigGroup({
60+
const result = await resolveConfig({
6161
env: { test: true, name: 'Hisoka' },
6262
config: [resolve(__dirname, './env.webpack.config.cjs')],
6363
});

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ const finalize = async (moduleObj, args) => {
156156
const isMultiCompilerMode = Array.isArray(config);
157157
const rawConfigs = isMultiCompilerMode ? config : [config];
158158

159-
let configs = await Promise.all(
159+
let configs = [];
160+
161+
const allConfigs = await Promise.all(
160162
rawConfigs.map(async (rawConfig) => {
161163
const isPromise = typeof rawConfig.then === 'function';
162164

@@ -174,6 +176,14 @@ const finalize = async (moduleObj, args) => {
174176
}),
175177
);
176178

179+
for (const singleConfig of allConfigs) {
180+
if (Array.isArray(singleConfig)) {
181+
configs.push(...singleConfig);
182+
} else {
183+
configs.push(singleConfig);
184+
}
185+
}
186+
177187
if (configName) {
178188
const foundConfigNames = [];
179189

@@ -204,7 +214,7 @@ const finalize = async (moduleObj, args) => {
204214
process.exit(2);
205215
}
206216

207-
newOptionsObject['options'] = isMultiCompilerMode ? configs : configs[0];
217+
newOptionsObject['options'] = configs.length > 1 ? configs : configs[0];
208218

209219
return newOptionsObject;
210220
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { options: coloretteOptions } = require('colorette');
1111
const WebpackCLIPlugin = require('./plugins/WebpackCLIPlugin');
1212

1313
// CLI arg resolvers
14-
const handleConfigResolution = require('./groups/ConfigGroup');
14+
const handleConfigResolution = require('./groups/resolveConfig');
1515
const resolveMode = require('./groups/resolveMode');
1616
const resolveStats = require('./groups/resolveStats');
1717
const resolveOutput = require('./groups/resolveOutput');

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

+47
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,51 @@ describe('--config-name flag', () => {
8181
expect(stdout).toBeFalsy();
8282
expect(exitCode).toBe(2);
8383
});
84+
85+
it('should work with config as a function', (done) => {
86+
const { stderr, stdout, exitCode } = run(__dirname, ['--config', 'function-config.js', '--config-name', 'first'], false);
87+
expect(stderr).toBeFalsy();
88+
expect(stdout).toContain('first');
89+
expect(stdout).not.toContain('second');
90+
expect(stdout).not.toContain('third');
91+
expect(exitCode).toBe(0);
92+
93+
stat(resolve(__dirname, './dist/dist-first.js'), (err, stats) => {
94+
expect(err).toBe(null);
95+
expect(stats.isFile()).toBe(true);
96+
done();
97+
});
98+
});
99+
100+
it('should work with multiple values for --config-name when the config is a function', (done) => {
101+
const { stderr, stdout, exitCode } = run(
102+
__dirname,
103+
['--config', 'function-config.js', '--config-name', 'first', '--config-name', 'third'],
104+
false,
105+
);
106+
expect(stderr).toBeFalsy();
107+
expect(stdout).toContain('first');
108+
expect(stdout).not.toContain('second');
109+
expect(stdout).toContain('third');
110+
expect(exitCode).toBe(0);
111+
112+
stat(resolve(__dirname, './dist/dist-third.js'), (err, stats) => {
113+
expect(err).toBe(null);
114+
expect(stats.isFile()).toBe(true);
115+
116+
stat(resolve(__dirname, './dist/dist-first.js'), (err, stats) => {
117+
expect(err).toBe(null);
118+
expect(stats.isFile()).toBe(true);
119+
done();
120+
});
121+
});
122+
});
123+
124+
it('should log error if invalid config name is provided ', () => {
125+
const { stderr, stdout, exitCode } = run(__dirname, ['--config', 'function-config.js', '--config-name', 'test'], false);
126+
127+
expect(stderr).toContain('Configuration with name "test" was not found.');
128+
expect(stdout).toBeFalsy();
129+
expect(exitCode).toBe(2);
130+
});
84131
});

test/config-name/function-config.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = () => [
2+
{
3+
output: {
4+
filename: './dist-first.js',
5+
},
6+
name: 'first',
7+
entry: './src/first.js',
8+
mode: 'development',
9+
},
10+
{
11+
output: {
12+
filename: './dist-second.js',
13+
},
14+
name: 'second',
15+
entry: './src/second.js',
16+
mode: 'production',
17+
},
18+
{
19+
output: {
20+
filename: './dist-third.js',
21+
},
22+
name: 'third',
23+
entry: './src/third.js',
24+
mode: 'none',
25+
},
26+
];

test/config-name/webpack.config.js

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module.exports = [
66
name: 'first',
77
entry: './src/first.js',
88
mode: 'development',
9-
stats: 'minimal',
109
},
1110
{
1211
output: {
@@ -15,7 +14,6 @@ module.exports = [
1514
name: 'second',
1615
entry: './src/second.js',
1716
mode: 'production',
18-
stats: 'minimal',
1917
},
2018
{
2119
output: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
const { resolve } = require('path');
3+
const { stat } = require('fs');
4+
const { run } = require('../../utils/test-utils');
5+
6+
describe('functional config', () => {
7+
it('should work as expected in case of single config', (done) => {
8+
const { stderr, stdout, exitCode } = run(__dirname, ['--config', resolve(__dirname, 'single-webpack.config.js')]);
9+
10+
expect(stderr).toBeFalsy();
11+
expect(stdout).toContain('./src/index.js');
12+
expect(exitCode).toBe(0);
13+
14+
stat(resolve(__dirname, './bin/dist-single.js'), (err, stats) => {
15+
expect(err).toBe(null);
16+
expect(stats.isFile()).toBe(true);
17+
done();
18+
});
19+
});
20+
21+
it('should work as expected in case of multiple config', (done) => {
22+
const { stderr, stdout, exitCode } = run(__dirname, ['--config', resolve(__dirname, 'multi-webpack.config.js')]);
23+
24+
expect(stderr).toBeFalsy();
25+
expect(stdout).toContain('first');
26+
expect(stdout).toContain('second');
27+
expect(exitCode).toBe(0);
28+
29+
stat(resolve(__dirname, './bin/dist-first.js'), (err, stats) => {
30+
expect(err).toBe(null);
31+
expect(stats.isFile()).toBe(true);
32+
done();
33+
});
34+
stat(resolve(__dirname, './bin/dist-second.js'), (err, stats) => {
35+
expect(err).toBe(null);
36+
expect(stats.isFile()).toBe(true);
37+
done();
38+
});
39+
});
40+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = () => [
2+
{
3+
output: {
4+
filename: './dist-first.js',
5+
},
6+
name: 'first',
7+
entry: './src/first.js',
8+
mode: 'development',
9+
stats: 'minimal',
10+
},
11+
{
12+
output: {
13+
filename: './dist-second.js',
14+
},
15+
name: 'second',
16+
entry: './src/second.js',
17+
mode: 'production',
18+
stats: 'minimal',
19+
},
20+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = () => {
2+
return {
3+
output: {
4+
filename: './dist-single.js',
5+
},
6+
name: 'single',
7+
mode: 'development',
8+
};
9+
};

test/config/function/src/first.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('first entry');

test/config/function/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('default index');

test/config/function/src/second.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('second entry');

0 commit comments

Comments
 (0)