Skip to content

Commit

Permalink
feat(webpack-cli): add mode argument validation
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Mar 4, 2020
1 parent a069d73 commit 125096a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
11 changes: 9 additions & 2 deletions packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { logger } = require('@webpack-cli/logger');
const HELP_GROUP = 'help';
const CONFIG_GROUP = 'config';
const BASIC_GROUP = 'basic';
Expand Down Expand Up @@ -286,11 +287,17 @@ module.exports = {
{
name: 'mode',
usage: '--mode <development | production>',
type: String,
type: (value) => {
if (value === 'development' || value === 'production' || value === 'none') {
return value
} else {
logger.warn('You provided invalid value for "mode" option, webpack will fallback to "production" for this value.')
}
},
group: ZERO_CONFIG_GROUP,
description: 'Defines the mode to pass to webpack',
link: 'https://webpack.js.org/concepts/#mode',
acceptedValues: ["development", "production"]
acceptedValues: ["development", "production", "none"]
},
{
name: 'no-mode',
Expand Down
2 changes: 1 addition & 1 deletion test/help/__snapshots__/help-single-arg.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Options
--standard Prints standard output
-d, --dev Run development build
-p, --prod Run production build
--mode string Defines the mode to pass to webpack
--mode type Defines the mode to pass to webpack
--no-mode Sets mode="none" which disables any default behavior
--version Get current version
--node-args string[] NodeJS flags
Expand Down
12 changes: 12 additions & 0 deletions test/mode/prod/prod.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ describe('mode flags', () => {
done();
});
});

it('should load a production config when --mode=abcd is passed', done => {
const { stderr, stdout } = run(__dirname, ['--mode', 'abcd']);
expect(stderr).toContain('"production" for this value');
expect(stdout).toBeTruthy();

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
});

0 comments on commit 125096a

Please sign in to comment.