Skip to content

Commit

Permalink
fix: set mode=production by default (#1688)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jul 17, 2020
1 parent 3fed703 commit 8360df7
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 14 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
4 changes: 2 additions & 2 deletions test/defaults/output-defaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const { run } = require('../utils/test-utils');
describe('output flag defaults', () => {
it('should create default file for a given directory', (done) => {
const { stdout } = run(__dirname, ['--entry', './a.js', '--output', './binary'], false);
// Should print a warning about config fallback
expect(stdout).toContain('option has not been set, webpack will fallback to');
// Should not print warning about config fallback, as we have production as default
expect(stdout).not.toContain('option has not been set, webpack will fallback to');
stat(resolve(__dirname, './binary/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion test/merge/config/merge-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { run } = require('../../utils/test-utils');
describe('merge flag configuration', () => {
it('merges two configurations together', (done) => {
const { stdout } = run(__dirname, ['--config', './1.js', '--merge', './2.js'], false);
expect(stdout).toContain('option has not been set, webpack will fallback to');
expect(stdout).not.toContain('option has not been set, webpack will fallback to');
stat(resolve(__dirname, './dist/merged.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
Expand Down
18 changes: 15 additions & 3 deletions test/merge/defaults/merge-defaults.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
'use strict';

const { stat } = require('fs');
const { stat, readFile } = require('fs');
const { resolve } = require('path');

const { run } = require('../../utils/test-utils');

describe('merge flag defaults', () => {
it('merges a default webpack.base.config with default config lookup', (done) => {
const { stdout } = run(__dirname, ['-m', './'], false);
expect(stdout).toContain('option has not been set, webpack will fallback to');
expect(stdout).not.toContain('option has not been set, webpack will fallback to');

stat(resolve(__dirname, './dist/default.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
readFile(resolve(__dirname, './dist/default.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('some_entry');
done();
});
});
it('merges a configuration file with default base config', (done) => {
const { stdout } = run(__dirname, ['-c', './1.js'], false);
expect(stdout).toContain('option has not been set, webpack will fallback to');

expect(stdout).not.toContain('option has not been set, webpack will fallback to');
stat(resolve(__dirname, './dist/default.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
readFile(resolve(__dirname, './dist/default.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('some_entry');
done();
});
});
});
2 changes: 1 addition & 1 deletion test/merge/defaults/some_entry.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('<3');
console.log('some_entry');
2 changes: 1 addition & 1 deletion test/merge/defaults/some_other_entry.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('moo');
console.log('some_other_entry');
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 8360df7

Please sign in to comment.