-
-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new
configtest
command (#2303)
- Loading branch information
Showing
16 changed files
with
197 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "@webpack-cli/configtest", | ||
"version": "1.0.0", | ||
"description": "Tests webpack configuration against validation errors.", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"peerDependencies": { | ||
"webpack": "4.x.x || 5.x.x", | ||
"webpack-cli": "4.x.x" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import webpack from 'webpack'; | ||
|
||
class ConfigTestCommand { | ||
async apply(cli): Promise<void> { | ||
const { logger } = cli; | ||
|
||
await cli.makeCommand( | ||
{ | ||
name: 'configtest <config-path>', | ||
alias: 't', | ||
description: 'Tests webpack configuration against validation errors.', | ||
usage: '<config-path>', | ||
pkg: '@webpack-cli/configtest', | ||
}, | ||
[], | ||
async (configPath: string): Promise<void> => { | ||
const config = await cli.resolveConfig({ config: [configPath] }); | ||
|
||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const error: any = webpack.validate(config.options); | ||
|
||
// TODO remove this after drop webpack@4 | ||
if (error && error.length > 0) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
throw new webpack.WebpackOptionsValidationError(error); | ||
} | ||
} catch (error) { | ||
const isValidationError = (error) => { | ||
// https://github.com/webpack/webpack/blob/master/lib/index.js#L267 | ||
// https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90 | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const ValidationError = (webpack.ValidationError || webpack.WebpackOptionsValidationError) as any; | ||
|
||
return error instanceof ValidationError; | ||
}; | ||
|
||
if (isValidationError(error)) { | ||
logger.error(error.message); | ||
} else { | ||
logger.error(error); | ||
} | ||
|
||
process.exit(2); | ||
} | ||
|
||
logger.success('There are no validation errors in the given webpack configuration.'); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
export default ConfigTestCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./lib", | ||
"rootDir": "./src" | ||
}, | ||
"include": ["./src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
const { run } = require('../utils/test-utils'); | ||
|
||
describe('basic info usage', () => { | ||
it('should validate webpack config successfully', () => { | ||
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './webpack.config.js'], false); | ||
|
||
expect(exitCode).toBe(0); | ||
expect(stderr).toBeFalsy(); | ||
expect(stdout).toContain('There are no validation errors in the given webpack configuration.'); | ||
}); | ||
|
||
it('should throw validation error', () => { | ||
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './error.config.js'], false); | ||
|
||
expect(exitCode).toBe(2); | ||
expect(stderr).toContain('Invalid configuration object.'); | ||
expect(stderr).toContain('configuration.mode should be one of these:'); | ||
expect(stdout).toBeFalsy(); | ||
}); | ||
|
||
it('should throw syntax error', () => { | ||
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './syntax-error.config.js'], false); | ||
|
||
expect(exitCode).toBe(2); | ||
expect(stderr).toContain(`SyntaxError: Unexpected token ';'`); | ||
expect(stdout).toBeFalsy(); | ||
}); | ||
|
||
it(`should validate the config with alias 't'`, () => { | ||
const { exitCode, stderr, stdout } = run(__dirname, ['t', './error.config.js'], false); | ||
|
||
expect(exitCode).toBe(2); | ||
expect(stderr).toContain('Invalid configuration object.'); | ||
expect(stderr).toContain('configuration.mode should be one of these:'); | ||
expect(stdout).toBeFalsy(); | ||
}); | ||
|
||
it('should throw error if configuration does not exist', () => { | ||
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './a.js'], false); | ||
|
||
expect(exitCode).toBe(2); | ||
expect(stderr).toContain(`The specified config file doesn't exist`); | ||
expect(stdout).toBeFalsy(); | ||
}); | ||
|
||
it('should throw error if no configuration was provided', () => { | ||
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false); | ||
|
||
expect(exitCode).toBe(2); | ||
expect(stderr).toContain(`error: missing required argument 'config-path'`); | ||
expect(stdout).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
mode: 'dev', // error | ||
target: 'node', | ||
stats: 'normal', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('configtest command'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
name: 'config-error', | ||
mode: 'development', | ||
target: 'node'; //SyntaxError: Unexpected token ';' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
mode: 'development', | ||
target: 'node', | ||
stats: 'verbose', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters