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

Enable reload on custom logic #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -152,7 +155,7 @@ class LiveReloadPlugin {
if (
this._isRunning()
&& include.length > 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested this code and in my case include.length is already 0 since none of the "assets" in the compilation has changed. In my scenario the shouldReload-call should need to "winn" over the include as well.

&& (hash !== this.lastHash || !LiveReloadPlugin.arraysEqual(childHashes, this.lastChildHashes))
&& ((hash !== this.lastHash || !LiveReloadPlugin.arraysEqual(childHashes, this.lastChildHashes)) || this.options.shouldReload(compilation))
andreyvolokitin marked this conversation as resolved.
Show resolved Hide resolved
) {
this.lastHash = hash;
this.lastChildHashes = childHashes;
Expand Down