From 7f62cd46319dbaa86cbb1a39fc2fa83b7ff2081a Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Wed, 27 May 2020 18:17:57 +0530 Subject: [PATCH 1/3] feat(webpack-cli): add --no-hot flag --- packages/webpack-cli/README.md | 1 + packages/webpack-cli/lib/utils/cli-flags.js | 8 ++++ test/no-hot/no-hot.test.js | 44 +++++++++++++++++++ test/no-hot/src/index.js | 1 + test/no-hot/webpack.config.js | 4 ++ test/serve/basic/serve-basic.test.js | 7 +++ .../serve-custom-config.test.js | 11 +++++ 7 files changed, 76 insertions(+) create mode 100644 test/no-hot/no-hot.test.js create mode 100644 test/no-hot/src/index.js create mode 100644 test/no-hot/webpack.config.js 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..4b1b7d5e009 --- /dev/null +++ b/test/no-hot/no-hot.test.js @@ -0,0 +1,44 @@ +'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 */'); + 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 */'); + 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(); + }); } }); From 8940239a8151402bae35dbe873883f51d0f97c6f Mon Sep 17 00:00:00 2001 From: Nitin Kumar <46647141+snitin315@users.noreply.github.com> Date: Wed, 3 Jun 2020 11:05:09 +0530 Subject: [PATCH 2/3] chore: suggestion Co-authored-by: James George --- test/no-hot/no-hot.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/no-hot/no-hot.test.js b/test/no-hot/no-hot.test.js index 4b1b7d5e009..5120de11cd2 100644 --- a/test/no-hot/no-hot.test.js +++ b/test/no-hot/no-hot.test.js @@ -3,7 +3,7 @@ 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) => { + it('should be successful when --no-hot is passed', (done) => { const { stderr, stdout } = run(__dirname, ['--no-hot']); expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); From 4d1f4666a82aca236bfac9c726ffa523002fe6f1 Mon Sep 17 00:00:00 2001 From: Nitin Kumar <46647141+snitin315@users.noreply.github.com> Date: Wed, 3 Jun 2020 11:05:38 +0530 Subject: [PATCH 3/3] chore: suggestions Co-authored-by: James George --- test/serve/with-custom-port/serve-custom-config.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 911ee88c60a..7025374304c 100644 --- a/test/serve/with-custom-port/serve-custom-config.test.js +++ b/test/serve/with-custom-port/serve-custom-config.test.js @@ -54,7 +54,7 @@ describe('serve with devServer in config', () => { expect(stderr).toBeFalsy(); }); - it('Passing no-hot flag works alongside other server config', async () => { + it('works fine when no-hot flag is passed 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');