Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webpack-cli): add mode argument validation #1290

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
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');
}