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']);