Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
Allow Users to Completely Disable webpack-hot-client (#50)
Browse files Browse the repository at this point in the history
* allow total disabling of hot-client

* update koa-webpack version, add tests

* improve tests
  • Loading branch information
shellscape authored Mar 9, 2018
1 parent 16f4806 commit a7e5114
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 29 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,14 @@ properly.

##### hot

Type: `Object`
Type: `Object|Booelean`
Default: `{}`

An object containing options for [webpack-hot-client][hot-client].
An object containing options for [webpack-hot-client][hot-client].

As of `v0.2.1` setting this to `false` will completely disable `webpack-hot-client`
and all automatic Hot Module Replacement functionality. This is akin to the
`--no-hot-client` CLI flag.

##### http2

Expand Down Expand Up @@ -414,4 +418,4 @@ We welcome your contributions! Please have a read of

[dev-ware]: https://github.com/webpack/webpack-dev-middleware#options
[hot-client]: https://github.com/webpack-contrib/webpack-hot-client#options
[https-opts]: https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options
[https-opts]: https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const cli = meow(chalk`
--log-level Limit all process console messages to a specific level and above
{dim Levels: trace, debug, info, warn, error, silent}
--log-time Instruct the logger for webpack-serve and dependencies to display a timestamp
--no-hot-client Instruct the serve to completely disable automatic HMR functionality
--no-hot Instruct the client not to apply Hot Module Replacement patches
--no-reload Instruct middleware {italic not} to reload the page for build errors
--open Instruct the app to open in the default browser
Expand Down
31 changes: 21 additions & 10 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,31 @@ module.exports = (opts) => {
throw new Error('webpack-serve: The `http2` option can only be used with Node v9 and higher.');
}

if (options.hot.host && options.host && options.hot.host !== options.host) {
throw new Error('webpack-serve: The `hot.host` property must match `host` option.');
} else if (options.hot && !options.hot.host) {
options.hot.host = options.host;
if (flags.hotClient === false) {
options.hot = false;
} else {
/* istanbul ignore if */
if (flags.hot === false) {
options.hot = Object.assign({}, options.hot, { hot: false });
}

/* istanbul ignore if */
if (flags.reload === false) {
options.hot = Object.assign({}, options.hot, { reload: false });
}
}

/* istanbul ignore if */
if (flags.hot === false) {
options.hot = Object.assign({}, options.hot, { hot: false });
// because you just know someone is going to do this
if (options.hot === true) {
options.hot = defaults.hot;
}

/* istanbul ignore if */
if (flags.reload === false) {
options.hot = Object.assign({}, options.hot, { reload: false });
if (options.hot) {
if (options.hot.host && options.host && options.hot.host !== options.host) {
throw new Error('webpack-serve: The `hot.host` property must match `host` option.');
} else if (!options.hot.host) {
options.hot.host = options.host;
}
}

/* istanbul ignore if */
Expand Down
37 changes: 24 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"import-local": "^1.0.0",
"killable": "^1.0.0",
"koa": "^2.4.1",
"koa-webpack": "^3.0.0",
"koa-webpack": "^3.0.1",
"lodash": "^4.17.5",
"loud-rejection": "^1.6.0",
"meow": "^4.0.0",
Expand Down Expand Up @@ -67,6 +67,7 @@
"sinon": "^4.2.2",
"strip-ansi": "^4.0.0",
"webpack": "^4.0.0",
"webpack-cli": "^2.0.9"
"webpack-cli": "^2.0.9",
"ws": "^5.0.0"
}
}
15 changes: 15 additions & 0 deletions test/tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const assert = require('power-assert');
const execa = require('execa');
const fetch = require('node-fetch');
const strip = require('strip-ansi');
const WebSocket = require('ws');
const { pause, t, timeout } = require('../util');

const cliPath = path.resolve(__dirname, '../../cli.js');
Expand Down Expand Up @@ -178,4 +179,18 @@ describe('webpack-serve CLI', () => {
done();
});
});

t('should use the --no-hot-client flag', (done) => {
x((proc) => {
const socket = new WebSocket('ws://localhost:8081');

socket.on('error', (error) => {
// this asserts that the WebSocketServer is not running, a sure sign
// that webpack-hot-client has been disabled.
assert(/ECONNREFUSED/.test(error.message));
proc.kill('SIGINT');
done();
});
}, cliPath, ['--config', configPath, '--no-hot-client']);
});
});
32 changes: 31 additions & 1 deletion test/tests/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const clip = require('clipboardy');
const fetch = require('node-fetch');
const mock = require('mock-require');
const webpack = require('webpack'); // eslint-disable-line import/order
const WebSocket = require('ws');
const util = require('../util');

const nodeVersion = parseInt(process.version.substring(1), 10);
Expand Down Expand Up @@ -202,7 +203,7 @@ describe('webpack-serve Options', () => {
});
});

t('should accept a hot option of `false`', (done) => {
t('should accept a hot.hot option of `false`', (done) => {
const config = load('./fixtures/basic/webpack.config.js');
config.serve.hot = false;

Expand All @@ -217,6 +218,35 @@ describe('webpack-serve Options', () => {
});
});

t('should accept a hot option of `true`', (done) => {
const config = load('./fixtures/basic/webpack.config.js');
config.serve.hot = true;

serve({ config }).then((server) => {
server.on('listening', () => {
// options.hot should be mutated from the default setting as an object
assert(typeof server.options.hot === 'object');
setTimeout(() => server.close(done), 1000);
});
});
});

t('should accept a hot option of `false` and disable webpack-hot-client', (done) => {
const config = load('./fixtures/basic/webpack.config.js');
config.serve.hot = false;

serve({ config }).then((server) => {
const socket = new WebSocket('ws://localhost:8081');

socket.on('error', (error) => {
// this asserts that the WebSocketServer is not running, a sure sign
// that webpack-hot-client has been disabled.
assert(/ECONNREFUSED/.test(error.message));
setTimeout(() => server.close(done), 1000);
});
});
});

t('should merge child options', (done) => {
const config = load('./fixtures/basic/webpack.options-merge.config.js', false);
serve({ config }).then((server) => {
Expand Down

0 comments on commit a7e5114

Please sign in to comment.