Skip to content

Commit 487691a

Browse files
feat: configtest validate default configuration (#2354)
1 parent 38869d2 commit 487691a

File tree

18 files changed

+139
-21
lines changed

18 files changed

+139
-21
lines changed

.eslintignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ test/typescript/webpack.config.ts
1010
test/config/error-commonjs/syntax-error.js
1111
test/config/error-mjs/syntax-error.mjs
1212
test/config/error-array/webpack.config.js
13-
test/configtest/syntax-error.config.js
13+
test/configtest/with-config-path/syntax-error.config.js

.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ test/config/error-mjs/syntax-error.mjs
1010
packages/webpack-cli/__tests__/test-assets/.yo-rc.json
1111
test/build-errors/stats.json
1212
packages/**/lib
13-
test/configtest/syntax-error.config.js
13+
test/configtest/with-config-path/syntax-error.config.js

packages/configtest/src/index.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,34 @@ class ConfigTestCommand {
66

77
await cli.makeCommand(
88
{
9-
name: 'configtest <config-path>',
9+
name: 'configtest [config-path]',
1010
alias: 't',
1111
description: 'Tests webpack configuration against validation errors.',
12-
usage: '<config-path>',
1312
pkg: '@webpack-cli/configtest',
1413
},
1514
[],
16-
async (configPath: string): Promise<void> => {
17-
const config = await cli.resolveConfig({ config: [configPath] });
15+
async (configPath: string | undefined): Promise<void> => {
16+
const config = await cli.resolveConfig(configPath ? { config: [configPath] } : {});
17+
const configPaths = new Set<string>();
18+
19+
if (Array.isArray(config.options)) {
20+
config.options.forEach((options) => {
21+
if (config.path.get(options)) {
22+
configPaths.add(config.path.get(options));
23+
}
24+
});
25+
} else {
26+
if (config.path.get(config.options)) {
27+
configPaths.add(config.path.get(config.options));
28+
}
29+
}
30+
31+
if (configPaths.size === 0) {
32+
logger.error('No configuration found.');
33+
process.exit(2);
34+
}
35+
36+
logger.info(`Validate '${Array.from(configPaths).join(' ,')}'.`);
1837

1938
try {
2039
// eslint-disable-next-line @typescript-eslint/no-explicit-any

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class WebpackCLI {
277277
pkg: '@webpack-cli/migrate',
278278
},
279279
{
280-
name: 'configtest',
280+
name: 'configtest [config-path]',
281281
alias: 't',
282282
pkg: '@webpack-cli/configtest',
283283
},
@@ -362,7 +362,7 @@ class WebpackCLI {
362362
} else {
363363
const builtInExternalCommandInfo = externalBuiltInCommandsInfo.find(
364364
(externalBuiltInCommandInfo) =>
365-
externalBuiltInCommandInfo.name === commandName ||
365+
getCommandName(externalBuiltInCommandInfo.name) === commandName ||
366366
(typeof Array.isArray(externalBuiltInCommandInfo.alias)
367367
? externalBuiltInCommandInfo.alias.includes(commandName)
368368
: externalBuiltInCommandInfo.alias === commandName),
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
'use strict';
22

3-
const { run } = require('../utils/test-utils');
3+
const path = require('path');
44

5-
describe('basic info usage', () => {
5+
const { run } = require('../../utils/test-utils');
6+
7+
describe("'configtest' command with the configuration path option", () => {
68
it('should validate webpack config successfully', () => {
7-
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './webpack.config.js'], false);
9+
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './basic.config.js'], false);
810

911
expect(exitCode).toBe(0);
1012
expect(stderr).toBeFalsy();
13+
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'basic.config.js')}'.`);
1114
expect(stdout).toContain('There are no validation errors in the given webpack configuration.');
1215
});
1316

@@ -17,7 +20,7 @@ describe('basic info usage', () => {
1720
expect(exitCode).toBe(2);
1821
expect(stderr).toContain('Invalid configuration object.');
1922
expect(stderr).toContain('configuration.mode should be one of these:');
20-
expect(stdout).toBeFalsy();
23+
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'error.config.js')}'.`);
2124
});
2225

2326
it('should throw syntax error', () => {
@@ -34,7 +37,7 @@ describe('basic info usage', () => {
3437
expect(exitCode).toBe(2);
3538
expect(stderr).toContain('Invalid configuration object.');
3639
expect(stderr).toContain('configuration.mode should be one of these:');
37-
expect(stdout).toBeFalsy();
40+
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'error.config.js')}'.`);
3841
});
3942

4043
it('should throw error if configuration does not exist', () => {
@@ -44,12 +47,4 @@ describe('basic info usage', () => {
4447
expect(stderr).toContain(`The specified config file doesn't exist`);
4548
expect(stdout).toBeFalsy();
4649
});
47-
48-
it('should throw error if no configuration was provided', () => {
49-
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);
50-
51-
expect(exitCode).toBe(2);
52-
expect(stderr).toContain(`error: missing required argument 'config-path'`);
53-
expect(stdout).toBeFalsy();
54-
});
5550
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"mode": "development",
3+
"target": "node",
4+
"stats": "verbose"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
const { run } = require('../../utils/test-utils');
6+
7+
describe("'configtest' command without the configuration path option", () => {
8+
it.only('should validate default configuration', () => {
9+
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);
10+
11+
expect(exitCode).toBe(0);
12+
expect(stderr).toBeFalsy();
13+
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'webpack.config.json')}'.`);
14+
expect(stdout).toContain('There are no validation errors in the given webpack configuration.');
15+
});
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
mode: 'invalid',
3+
target: 'node',
4+
stats: 'verbose',
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
const { run } = require('../../utils/test-utils');
6+
7+
describe("'configtest' command without the configuration path option", () => {
8+
it.only('should validate default configuration', () => {
9+
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);
10+
11+
expect(exitCode).toBe(2);
12+
expect(stderr).toContain('Invalid configuration object.');
13+
expect(stderr).toContain('configuration.mode should be one of these:');
14+
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'webpack.config.js')}'.`);
15+
});
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = [
2+
{
3+
mode: 'development',
4+
target: 'node',
5+
stats: 'verbose',
6+
},
7+
{
8+
mode: 'development',
9+
target: 'node',
10+
stats: 'verbose',
11+
},
12+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
const { run } = require('../../utils/test-utils');
6+
7+
describe("'configtest' command without the configuration path option", () => {
8+
it.only('should validate default configuration', () => {
9+
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);
10+
11+
expect(exitCode).toBe(0);
12+
expect(stderr).toBeFalsy();
13+
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'webpack.config.js')}'.`);
14+
expect(stdout).toContain('There are no validation errors in the given webpack configuration.');
15+
});
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const { run } = require('../../utils/test-utils');
4+
5+
describe("'configtest' command without the configuration path option", () => {
6+
it.only('should validate default configuration', () => {
7+
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);
8+
9+
expect(exitCode).toBe(2);
10+
expect(stderr).toContain('No configuration found.');
11+
expect(stdout).toBeFalsy();
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
mode: 'development',
3+
target: 'node',
4+
stats: 'verbose',
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
const { run } = require('../../utils/test-utils');
6+
7+
describe("'configtest' command without the configuration path option", () => {
8+
it.only('should validate default configuration', () => {
9+
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);
10+
11+
expect(exitCode).toBe(0);
12+
expect(stderr).toBeFalsy();
13+
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'webpack.config.js')}'.`);
14+
expect(stdout).toContain('There are no validation errors in the given webpack configuration.');
15+
});
16+
});

0 commit comments

Comments
 (0)