diff --git a/packages/webpack-cli/README.md b/packages/webpack-cli/README.md index 8cb633924c5..702de739c7f 100644 --- a/packages/webpack-cli/README.md +++ b/packages/webpack-cli/README.md @@ -48,6 +48,7 @@ Options -t, --target string Sets the build target -w, --watch Watch for files changes -h, --hot Enables Hot Module Replacement + --no-hot Disables Hot Module Replacement -s, --sourcemap string Determine source maps to use --prefetch string Prefetch this request -j, --json Prints result as JSON diff --git a/packages/webpack-cli/__tests__/AdvancedGroup.test.js b/packages/webpack-cli/__tests__/AdvancedGroup.test.js new file mode 100644 index 00000000000..4d0f2e4d9bb --- /dev/null +++ b/packages/webpack-cli/__tests__/AdvancedGroup.test.js @@ -0,0 +1,14 @@ +const AdvancedGroup = require('../lib/groups/AdvancedGroup'); + +describe('GroupHelper', function() { + it('should load the hot advanced config', () => { + const group = new AdvancedGroup([ + { + hot: true, + }, + ]); + + const result = group.run(); + expect(result.options.plugins).toBeTruthy(); + }); +}); diff --git a/packages/webpack-cli/lib/groups/AdvancedGroup.js b/packages/webpack-cli/lib/groups/AdvancedGroup.js index c3abafb23e4..77f5c1c73ad 100644 --- a/packages/webpack-cli/lib/groups/AdvancedGroup.js +++ b/packages/webpack-cli/lib/groups/AdvancedGroup.js @@ -57,6 +57,12 @@ class AdvancedGroup extends GroupHelper { options.plugins = [hotModuleVal]; } } + if (args.noHot && args.hot) { + logger.warn('You provided both --hot and --no-hot. You should provide just one, "--hot" will be used') + } + if (args.noHot){ + args.hot = false; + } if (args.prefetch) { const { PrefetchPlugin } = require('webpack'); const prefetchVal = new PrefetchPlugin(null, args.prefetch); diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index dc90ea2873c..5e869d818b6 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -232,6 +232,14 @@ module.exports = { description: 'Enables Hot Module Replacement', link: 'https://webpack.js.org/concepts/hot-module-replacement/', }, + { + name: 'no-hot', + usage: 'webpack --no-hot', + type: Boolean, + group: ADVANCED_GROUP, + description: 'Disables Hot Module Replacement', + link: 'https://webpack.js.org/concepts/hot-module-replacement/', + }, { name: 'sourcemap', usage: '--sourcemap ', diff --git a/test/no-hot/no-hot.test.js b/test/no-hot/no-hot.test.js new file mode 100644 index 00000000000..c16142f412d --- /dev/null +++ b/test/no-hot/no-hot.test.js @@ -0,0 +1,28 @@ +'use strict'; +const { run } = require('../utils/test-utils'); +const { stat } = require('fs'); +const { resolve } = require('path'); +describe('no-hot flag', () => { + it('should be successful when --no-hot is passed', done => { + const { stderr, stdout } = run(__dirname, ['--no-hot']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); + + it('should use --hot when --hot and --no-hot are passed', done => { + const { stderr, stdout } = run(__dirname, ['--no-hot', '--hot']); + expect(stderr).toContain('"--hot" will be used'); + expect(stdout).toBeTruthy(); + + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); +}); diff --git a/test/no-hot/src/index.js b/test/no-hot/src/index.js new file mode 100644 index 00000000000..d71c6a84ba2 --- /dev/null +++ b/test/no-hot/src/index.js @@ -0,0 +1 @@ +console.log('HELLO WORLD') ; diff --git a/test/no-hot/test-with-config/index.js b/test/no-hot/test-with-config/index.js new file mode 100644 index 00000000000..b3d3cb57e42 --- /dev/null +++ b/test/no-hot/test-with-config/index.js @@ -0,0 +1 @@ +console.log('TEST WITH CONFIG'); diff --git a/test/no-hot/test-with-config/no-hot-with-config.test.js b/test/no-hot/test-with-config/no-hot-with-config.test.js new file mode 100644 index 00000000000..7132eec1a5c --- /dev/null +++ b/test/no-hot/test-with-config/no-hot-with-config.test.js @@ -0,0 +1,16 @@ +'use strict'; +const { run } = require('../../utils/test-utils'); +const { stat } = require('fs'); +const { resolve } = require('path'); +describe('no-hot flag', () => { + it('should be successful when --no-hot is passed', done => { + const { stderr, stdout } = run(__dirname, ['--no-hot']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); +}); diff --git a/test/no-hot/test-with-config/webpack.config.js b/test/no-hot/test-with-config/webpack.config.js new file mode 100644 index 00000000000..62d4c68698c --- /dev/null +++ b/test/no-hot/test-with-config/webpack.config.js @@ -0,0 +1,7 @@ +module.exports = { + mode: 'development', + entry: './index.js', + devServer: { + hot: true, + }, +}; diff --git a/test/no-hot/webpack.config.js b/test/no-hot/webpack.config.js new file mode 100644 index 00000000000..4b3c3c0ccdc --- /dev/null +++ b/test/no-hot/webpack.config.js @@ -0,0 +1,4 @@ +module.exports = { + mode: 'development', + entry: './src/index.js', +}; diff --git a/test/serve/serve-basic.test.js b/test/serve/serve-basic.test.js index 2ebcd812937..6dc69880136 100644 --- a/test/serve/serve-basic.test.js +++ b/test/serve/serve-basic.test.js @@ -30,6 +30,12 @@ describe('basic serve usage', () => { expect(stderr).toHaveLength(0); }); + it('uses no-hot flag', async () => { + const { stdout, stderr } = await runServe(['--no-hot']); + expect(stdout).toContain('main.js'); + expect(stderr).toHaveLength(0); + }); + it('uses hot flag and progress flag', async () => { const { stdout, stderr } = await runServe(['--hot', '--progress']); expect(stdout).toContain('main.js');