From e0eb08a4b76fb72b4fb7580582fa70ca00568005 Mon Sep 17 00:00:00 2001 From: andreyvolokitin Date: Mon, 15 Mar 2021 18:23:43 +0400 Subject: [PATCH 1/2] Enable reload on custom logic --- README.md | 7 +++++++ index.js | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 366afec..4d2ff4b 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,13 @@ Add a script tag to your page pointed at the livereload server ignore nothing. It is also possible to define an array and use multiple [anymatch](https://github.com/micromatch/anymatch) patterns. - `delay` - (Default: `0`) amount of milliseconds by which to delay the live reload (in case build takes longer) - `useSourceHash` - (Default: `false`) create hash for each file source and only notify livereload if hash has changed +- `shouldReload` - (Default: `function`) callback which gets called with a `compilation` argument to decide if page should reload, by returning a boolean. Usually page gets reloaded when compilation hash changes, but sometimes it may be desirable to reload even if hash stays the same. I.e. `CopyWebpackPlugin` can emit changed files, but it doesn't change compilation hash. In this case you could check emitted files to decide if reloading is required: + +``` +shouldReload(compilation) { + return Array.from(compilation.emittedAssets).some(assetName => path.extname(assetName) === '.php'); +} +``` ## Why? diff --git a/index.js b/index.js index b820806..2730fb9 100755 --- a/index.js +++ b/index.js @@ -27,6 +27,9 @@ class LiveReloadPlugin { useSourceHash: false, appendScriptTag: false, delay: 0, + shouldReload(compilation) { + return false; + }, }, options); // Random alphanumeric string appended to id to allow multiple instances of live reload @@ -152,7 +155,7 @@ class LiveReloadPlugin { if ( this._isRunning() && include.length > 0 - && (hash !== this.lastHash || !LiveReloadPlugin.arraysEqual(childHashes, this.lastChildHashes)) + && ((hash !== this.lastHash || !LiveReloadPlugin.arraysEqual(childHashes, this.lastChildHashes)) || this.options.shouldReload(compilation)) ) { this.lastHash = hash; this.lastChildHashes = childHashes; From 4b6a0f9119e60f2c644d050c035d409b4f7bc5d5 Mon Sep 17 00:00:00 2001 From: andreyvolokitin Date: Sun, 19 Mar 2023 15:00:40 +0400 Subject: [PATCH 2/2] Update index.js Co-authored-by: Marc Itzenthaler --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 2730fb9..8ae8e83 100755 --- a/index.js +++ b/index.js @@ -155,7 +155,7 @@ class LiveReloadPlugin { if ( this._isRunning() && include.length > 0 - && ((hash !== this.lastHash || !LiveReloadPlugin.arraysEqual(childHashes, this.lastChildHashes)) || this.options.shouldReload(compilation)) + && (hash !== this.lastHash || !LiveReloadPlugin.arraysEqual(childHashes, this.lastChildHashes) || this.options.shouldReload(compilation)) ) { this.lastHash = hash; this.lastChildHashes = childHashes;