Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "shellscape",
"extends": ["shellscape"],
"globals": {
"document": true,
"ʎɐɹɔosǝʌɹǝs": true,
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ This setting can be handy when using the HTML5 History API; `index.html` page wi
_Note: The `Accept` header is explicitly stripped from the `/wps` WebSocket path when using `historyFallback`, due to [an issue](https://github.com/shellscape/webpack-plugin-serve/issues/94) with how Firefox and the middleware interact._

### `hmr`
Type: `boolean`<br>
Type: `boolean | 'refresh-on-failure'`<br>
Default: `true`

If `true`, will enable [`Hot Module Replacement`](https://webpack.js.org/concepts/hot-module-replacement/) which exchanges, adds, or removes modules from a bundle dynamically while the application still running, without the need of a full page reload.

_Note: If the build process generates errors, the client (browser) will not be notified of new changes and no HMR will be performed. Errors must be resolved before HMR can proceed._
_Note: If the build process generates errors, the client (browser) will not be notified of new changes and no HMR will be performed. Errors must be resolved before HMR can proceed. To force a refresh, pass `refresh-on-failure' to this option._

_Note: If using in combination with `http2`, the `http2` option `allowHTTP1` must be enabled for the HMR WS connection to work._

Expand Down
4 changes: 2 additions & 2 deletions lib/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
/* eslint-disable global-require */
const run = (buildHash, options) => {
const { address, client = {}, progress, secure, status } = options;
const { address, client = {}, hmr, progress, secure, status } = options;

options.firstInstance = !window.webpackPluginServe; // eslint-disable-line no-param-reassign

Expand Down Expand Up @@ -67,7 +67,7 @@ const run = (buildHash, options) => {
// matches the wpsId set in options. this is how we can identify multiple compilers in the
// client.
if (wpsId && wpsId === options.wpsId) {
replace(buildHash, hash);
replace(buildHash, hash, hmr === 'refresh-on-failure');
}
break;
default:
Expand Down
42 changes: 29 additions & 13 deletions lib/client/hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@ const { error, info, refresh, warn } = require('./log')();

let latest = true;

const hmr = {
onUnaccepted(data) {
warn('Change in unaccepted module(s):\n', data);
warn(data);
},
onDeclined(data) {
warn('Change in declined module(s):\n', data);
},
onErrored(data) {
error('Error in module(s):\n', data);
}
const hmr = (onFailure) => {
return {
onUnaccepted(data) {
onFailure();
warn('Change in unaccepted module(s):\n', data);
warn(data);
},
onDeclined(data) {
onFailure();
warn('Change in declined module(s):\n', data);
},
onErrored(data) {
onFailure();
error('Error in module(s):\n', data);
}
};
};

const replace = async (buildHash, hash) => {
const replace = async (buildHash, hash, refreshOnFailure) => {
const { apply, check, status } = module.hot;

if (hash) {
Expand Down Expand Up @@ -57,7 +62,18 @@ const replace = async (buildHash, hash) => {
return;
}

modules = await apply(hmr);
modules = await apply(
hmr(
refreshOnFailure
? () => {
if (refreshOnFailure) {
// eslint-disable-next-line no-undef
location.reload();
}
}
: () => {}
)
);

if (modules) {
latest = true;
Expand Down
2 changes: 1 addition & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = {
compress: nullable(boolean()),
headers: nullable(type({})),
historyFallback: union([boolean(), type({})]),
hmr: boolean(),
hmr: union([boolean(), literal('refresh-on-failure')]),
host: nullable(union([promise, string()])),
http2: union([boolean(), type({})]),
https: nullable(type({})),
Expand Down
Loading