Skip to content

Commit

Permalink
feat: allow string value for '--hot' (#2444)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored Mar 15, 2021
1 parent 889646b commit 8656e78
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Options:
-m, --merge Merge two or more configurations using 'webpack-merge'.
--env <value...> Environment passed to the configuration when it is a function.
--node-env <value> 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.
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ npx webpack-cli --help verbose
-d, --devtool <value> Determine source maps to use.
--no-devtool Do not generate source maps.
--entry <value...> 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 <value> Defines the mode to pass to webpack.
--name <value> Name of the configuration. Used when loading multiple configurations.
Expand Down
7 changes: 6 additions & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -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 &&
Expand Down
17 changes: 17 additions & 0 deletions test/hot/hot-flag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down

0 comments on commit 8656e78

Please sign in to comment.