Skip to content

Commit

Permalink
feat(webpack-cli): add --no-hot flag
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed May 27, 2020
1 parent e64cf82 commit 6265b10
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/webpack-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sourcemap | eval>',
Expand Down
29 changes: 29 additions & 0 deletions test/no-hot/no-hot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const { run } = require('../utils/test-utils');
const { stat } = require('fs');
const { resolve } = require('path');
describe('no-hot flag', () => {
it('should be successful when --no-hot is passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--no-hot']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});

it('should warn when --hot and --no-hot both are passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--no-hot', '--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();
});
});
});
1 change: 1 addition & 0 deletions test/no-hot/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('no-hot test');
1 change: 1 addition & 0 deletions test/no-hot/test-with-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('TEST WITH CONFIG');
16 changes: 16 additions & 0 deletions test/no-hot/test-with-config/no-hot-with-config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const { run } = require('../../utils/test-utils');
const { stat } = require('fs');
const { resolve } = require('path');
describe('no-hot flag', () => {
it('should be successful when --no-hot is passed with custom config', (done) => {
const { stderr, stdout } = run(__dirname, ['--no-hot']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
stat(resolve(__dirname, './bin/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
});
7 changes: 7 additions & 0 deletions test/no-hot/test-with-config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
mode: 'development',
entry: './index.js',
devServer: {
hot: true,
},
};
7 changes: 7 additions & 0 deletions test/serve/basic/serve-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
11 changes: 11 additions & 0 deletions test/serve/with-custom-port/serve-custom-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
});

0 comments on commit 6265b10

Please sign in to comment.