Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webpack-cli): add --no-hot flag #1591

Merged
merged 7 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/webpack-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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
44 changes: 44 additions & 0 deletions test/no-hot/no-hot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';
const { run } = require('../utils/test-utils');
const { stat, readFile } = 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();
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 */');
done();
});
});

it('should warn 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 */');
done();
});
});
});
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
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('works fine when no-hot flag is passed 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();
});
}
});