Skip to content

Commit

Permalink
fix: set mode=production by default
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jul 17, 2020
1 parent f3fe4cf commit 83cf618
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/webpack-cli/__tests__/ZeroConfigGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,12 @@ describe('ZeroConfigGroup', function () {
// ensure no other properties are added
expect(result.options).toMatchObject({ mode: 'development' });
});

it('should set mode=production by default', () => {
const group = new ZeroConfigGroup([{}]);

const result = group.run();
// ensure no other properties are added
expect(result.options).toMatchObject({ mode: 'production' });
});
});
4 changes: 2 additions & 2 deletions packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ module.exports = {
usage: '--dev',
alias: 'd',
type: Boolean,
defaultValue: undefined,
defaultValue: false,
group: ZERO_CONFIG_GROUP,
description: 'Run development build',
link: 'https://webpack.js.org/concepts/#mode',
Expand All @@ -201,7 +201,7 @@ module.exports = {
alias: 'p',
usage: '--prod',
type: Boolean,
defaultValue: undefined,
defaultValue: false,
group: ZERO_CONFIG_GROUP,
description: 'Run production build',
link: 'https://webpack.js.org/concepts/#mode',
Expand Down
31 changes: 27 additions & 4 deletions test/mode/mode-single-arg/mode-single-arg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,27 @@ const { run } = require('../../utils/test-utils');
const { stat, readFile } = require('fs');
const { resolve } = require('path');
describe('mode flags', () => {
it('should set mode=production by default', (done) => {
const { stderr, stdout } = run(__dirname);
expect(stderr).toBeFalsy();
expect(stdout).toContain(`mode: 'production'`);

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 test');
done();
});
});

it('should load a development config when --mode=development is passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--mode', 'development']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(stdout).toContain(`mode: 'development'`);

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
Expand All @@ -18,19 +35,24 @@ describe('mode flags', () => {
it('should load a production config when --mode=production is passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--mode', 'production']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(stdout).toContain(`mode: 'production'`);

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 test');
done();
});
});

it('should load a none config when --mode=none is passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--mode', 'none']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(stdout).toContain(`mode: 'none'`);

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
Expand All @@ -42,7 +64,8 @@ describe('mode flags', () => {
it('should show warning and load a production config when --mode=abcd is passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--mode', 'abcd']);
expect(stderr).toContain('invalid value for "mode" option. Using "production" by default');
expect(stdout).toBeTruthy();
expect(stdout).toContain(`mode: 'production'`);

stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
Expand Down
7 changes: 7 additions & 0 deletions test/mode/mode-single-arg/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// eslint-disable-next-line node/no-unpublished-require
const WebpackCLITestPlugin = require('../../utils/webpack-cli-test-plugin');

module.exports = {
entry: './src/index.js',
plugins: [new WebpackCLITestPlugin()],
};

0 comments on commit 83cf618

Please sign in to comment.