Skip to content

Commit

Permalink
feat(webpack-cli): add mode argument validation (#1290)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored Mar 6, 2020
1 parent 36b384a commit e273303
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
13 changes: 10 additions & 3 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 an invalid value for "mode" option.');
return 'production' ;
}
},
group: ZERO_CONFIG_GROUP,
description: 'Defines the mode to pass to webpack',
link: 'https://webpack.js.org/concepts/#mode',
acceptedValues: ["development", "production"]
link: 'https://webpack.js.org/concepts/#mode'
},
{
name: 'no-mode',
Expand Down
18 changes: 17 additions & 1 deletion test/mode/prod/prod.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const { run } = require('../../utils/test-utils');
const { stat } = require('fs');
const { stat, readFile } = require('fs');
const { resolve } = require('path');
describe('mode flags', () => {
it('should load a production config when --prod is passed', done => {
Expand All @@ -25,6 +25,22 @@ describe('mode flags', () => {
});
});

it('should load a production config when --mode=abcd is passed', done => {
const { stderr, stdout } = run(__dirname, ['--mode', 'abcd']);
expect(stderr).toContain('invalid value for "mode"');
expect(stdout).toBeTruthy();
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
readFile(resolve(__dirname, './bin/main.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('production');
done();
});
});

it('should load a production config when --mode=production and --prod are passed', done => {
const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--prod']);
expect(stderr).toContain('"mode" will be used');
Expand Down
4 changes: 4 additions & 0 deletions test/mode/prod/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
console.log('default');

if (process.env.NODE_ENV === "production") {
console.log('production');
}

0 comments on commit e273303

Please sign in to comment.