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 29, 2020
1 parent e64cf82 commit 21aca9b
Show file tree
Hide file tree
Showing 7 changed files with 80 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
48 changes: 48 additions & 0 deletions test/no-hot/no-hot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'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 */');
expect(data).not.toContain('function hotApply');
expect(data).toContain('no-hot test');
done();
});
});

it('should warn --no-hot 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 */');
expect(data).not.toContain('function hotApply');
expect(data).toContain('no-hot test');
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');
4 changes: 4 additions & 0 deletions test/no-hot/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
mode: 'development',
stats: 'verbose',
};
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 21aca9b

Please sign in to comment.