diff --git a/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js b/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js index 4b25801772d..7e34eb7dac3 100644 --- a/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js +++ b/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js @@ -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' }); + }); }); diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 9a32f5544af..b2076f95959 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -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', @@ -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', diff --git a/test/mode/mode-single-arg/mode-single-arg.test.js b/test/mode/mode-single-arg/mode-single-arg.test.js index c5282c653ae..1976447de82 100644 --- a/test/mode/mode-single-arg/mode-single-arg.test.js +++ b/test/mode/mode-single-arg/mode-single-arg.test.js @@ -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); @@ -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); @@ -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); diff --git a/test/mode/mode-single-arg/webpack.config.js b/test/mode/mode-single-arg/webpack.config.js new file mode 100644 index 00000000000..b1dcc533e36 --- /dev/null +++ b/test/mode/mode-single-arg/webpack.config.js @@ -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()], +};