From 8656e78d788bc8a504258d4dcc609767f63d60c4 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 16 Mar 2021 00:12:20 +0530 Subject: [PATCH] feat: allow string value for '--hot' (#2444) --- OPTIONS.md | 2 +- packages/webpack-cli/README.md | 2 +- packages/webpack-cli/lib/webpack-cli.js | 7 ++++++- test/hot/hot-flag.test.js | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/OPTIONS.md b/OPTIONS.md index cb3790a5192..04b5582fb33 100644 --- a/OPTIONS.md +++ b/OPTIONS.md @@ -10,7 +10,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --env Environment passed to the configuration when it is a function. --node-env Sets process.env.NODE_ENV to the specified value. - -h, --hot Enables Hot Module Replacement + -h, --hot [value] Enables Hot Module Replacement --no-hot Disables Hot Module Replacement. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. diff --git a/packages/webpack-cli/README.md b/packages/webpack-cli/README.md index bbd61977060..72c9dd71a00 100644 --- a/packages/webpack-cli/README.md +++ b/packages/webpack-cli/README.md @@ -91,7 +91,7 @@ npx webpack-cli --help verbose -d, --devtool Determine source maps to use. --no-devtool Do not generate source maps. --entry The entry point(s) of your application e.g. ./src/main.js. - -h, --hot Enables Hot Module Replacement + -h, --hot [value] Enables Hot Module Replacement --no-hot Disables Hot Module Replacement --mode Defines the mode to pass to webpack. --name Name of the configuration. Used when loading multiple configurations. diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 0377ed1ac5c..ec37353fe71 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -382,7 +382,7 @@ class WebpackCLI { { name: 'hot', alias: 'h', - type: Boolean, + type: [Boolean, String], negative: true, description: 'Enables Hot Module Replacement', negatedDescription: 'Disables Hot Module Replacement.', @@ -1456,6 +1456,11 @@ class WebpackCLI { process.exit(2); } + if (typeof options.hot === 'string' && options.hot !== 'only') { + this.logger.error(`'${options.hot}' is an invalid value for the --hot option. Use 'only' instead.`); + process.exit(2); + } + const outputHints = (configOptions) => { if ( configOptions.watch && diff --git a/test/hot/hot-flag.test.js b/test/hot/hot-flag.test.js index 2557252dc1e..6be6613e4d4 100644 --- a/test/hot/hot-flag.test.js +++ b/test/hot/hot-flag.test.js @@ -13,6 +13,23 @@ describe('--hot flag', () => { expect(readFileSync(resolve(__dirname, './dist/main.js')).toString()).toContain('webpackHotUpdate'); }); + it('should be successful when --hot=only is passed', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--hot', 'only']); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + expect(readFileSync(resolve(__dirname, './dist/main.js')).toString()).toContain('webpackHotUpdate'); + }); + + it('should throw an error for invalid value', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--hot', 'unknown']); + + expect(exitCode).toBe(2); + expect(stderr).toContain(`[webpack-cli] 'unknown' is an invalid value for the --hot option. Use 'only' instead.`); + expect(stdout).toBeFalsy(); + }); + it('should be successful when --no-hot is passed', () => { const { exitCode, stderr, stdout } = run(__dirname, ['--no-hot']);