diff --git a/packages/webpack-cli/README.md b/packages/webpack-cli/README.md index 7b7a8b42e7e..941a989676c 100644 --- a/packages/webpack-cli/README.md +++ b/packages/webpack-cli/README.md @@ -45,6 +45,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/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index b18df20a84d..408aaa78a9e 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -155,6 +155,14 @@ module.exports = { description: 'Enables Hot Module Replacement', link: 'https://webpack.js.org/concepts/hot-module-replacement/', }, + { + name: 'no-hot', + usage: '--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..28efa001c57 --- /dev/null +++ b/test/no-hot/no-hot.test.js @@ -0,0 +1,48 @@ +'use strict'; +const { run } = require('../utils/test-utils'); +const { stat, readFile } = require('fs'); +const { resolve } = require('path'); +describe('no-hot flag', () => { + it('shoul,d be successful when --no-hot is passed', (done) => { + const { stderr, stdout } = run(__dirname, ['--no-hot']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + expect(stdout).not.toContain('webpack/runtime/hot module replacement'); + + 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); + // check for absence of special functions invoked by HMR plugin only + expect(data).not.toContain('/* webpack/runtime/hot module replacement */'); + expect(data).not.toContain('function hotApply'); + expect(data).toContain('no-hot test'); + done(); + }); + }); + + it('should warn when --hot and --no-hot both are passed', (done) => { + const { stderr, stdout } = run(__dirname, ['--hot', '--no-hot']); + expect(stderr).toContain( + '[webpack-cli] You provided both --hot and --no-hot. We will use only the last of these flags that you provided in your CLI arguments', + ); + expect(stdout).toBeTruthy(); + + 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); + // check for absence of special functions invoked by HMR plugin only + expect(data).not.toContain('/* webpack/runtime/hot module replacement */'); + expect(data).not.toContain('function hotApply'); + expect(data).toContain('no-hot test'); + done(); + }); + }); +}); diff --git a/test/no-hot/src/index.js b/test/no-hot/src/index.js new file mode 100644 index 00000000000..6e995fcc00e --- /dev/null +++ b/test/no-hot/src/index.js @@ -0,0 +1 @@ +console.log('no-hot test'); diff --git a/test/no-hot/webpack.config.js b/test/no-hot/webpack.config.js new file mode 100644 index 00000000000..37c48e745b2 --- /dev/null +++ b/test/no-hot/webpack.config.js @@ -0,0 +1,4 @@ +module.exports = { + mode: 'development', + stats: 'verbose', +}; diff --git a/test/serve/basic/serve-basic.test.js b/test/serve/basic/serve-basic.test.js index bb32dfd1846..ddaca288ade 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -37,6 +37,13 @@ describe('basic serve usage', () => { expect(stderr).toHaveLength(0); }); + it('uses no-hot flag', async () => { + const { stdout, stderr } = await runServe(['--port', port, '--no-hot'], testPath); + expect(stdout).toContain('main.js'); + expect(stdout).not.toContain('hot/dev-server.js'); + expect(stderr).toHaveLength(0); + }); + it('uses hot flag and progress flag', async () => { const { stdout, stderr } = await runServe(['--port', port, '--hot', '--progress'], testPath); expect(stdout).toContain('main.js'); diff --git a/test/serve/with-custom-port/serve-custom-config.test.js b/test/serve/with-custom-port/serve-custom-config.test.js index e10bf8ee15b..911ee88c60a 100644 --- a/test/serve/with-custom-port/serve-custom-config.test.js +++ b/test/serve/with-custom-port/serve-custom-config.test.js @@ -53,5 +53,16 @@ describe('serve with devServer in config', () => { expect(stdout).toContain(`http://0.0.0.0:${port}`); expect(stderr).toBeFalsy(); }); + + it('Passing no-hot flag works alongside other server config', async () => { + const { stdout, stderr } = await runServe(['--port', port, '--no-hot'], testPath); + // Should output the correct bundle file + expect(stdout).toContain('main.js'); + // HMR is not being used + expect(stdout).not.toContain('hot/dev-server.js'); + // Runs at correct host and port + expect(stdout).toContain(`http://0.0.0.0:${port}`); + expect(stderr).toBeFalsy(); + }); } });