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

LiveReload always sends all assets #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
47 changes: 43 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* jshint node:true */
var statSync = require('fs').statSync;
var lr = require('tiny-lr');
var servers = {};

Expand All @@ -17,6 +18,10 @@ function LiveReloadPlugin(options) {
this.protocol = this.options.protocol ? this.options.protocol + ':' : '';
this.hostname = this.options.hostname || '" + location.hostname + "';
this.server = null;

this.startTime = new Date();
this.startTime.setSeconds(-process.uptime());
this.prevTimestamps = {};
Copy link
Owner

Choose a reason for hiding this comment

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

I see no usage of prevTimestamps

}

function arraysEqual(a1, a2){
Expand Down Expand Up @@ -53,18 +58,52 @@ LiveReloadPlugin.prototype.start = function start(watching, cb) {
};

LiveReloadPlugin.prototype.done = function done(stats) {
var timestamps = stats.compilation ? stats.compilation.fileTimestamps : {};
var assets = stats.compilation.assets;
var include = [];

// If no timestamp information is available, use all assets.
if (!timestamps) {
include.push.apply(include, Object.keys(assets));
}
else {
this.changedFiles = Object.keys(timestamps).filter(function(watchfile) {
return this.startTime < Math.ceil(statSync(watchfile).mtime);
Copy link
Owner

Choose a reason for hiding this comment

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

Synchronously stating all files seems like it could be a cause for a hang in a larger product.

Copy link
Author

Choose a reason for hiding this comment

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

True, and I suspect webpack has already stat'd these files at some other point. Wonder if there is a way to tap into a stat cache of some kind?

Copy link
Owner

Choose a reason for hiding this comment

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

In the webpack wiki you linked, it looks like they're using prevTimestamps as a way to see changes.

 var changedFiles = Object.keys(compilation.fileTimestamps).filter(function(watchfile) {
      return (this.prevTimestamps[watchfile] || this.startTime) < (compilation.fileTimestamps[watchfile] || Infinity);
    }.bind(this));

}.bind(this)).forEach(function(changedFile) {
Object.keys(assets).forEach(function(assetName) {
const asset = Object.assign({}, assets[assetName]);
const sources = [];

if (asset.emitted && asset.existsAt.split('.').slice(-1)[0] !== 'css') {
include.push(assetName);
}

(asset.children || []).forEach(function(child) {
if (child && child._sourceMap && child._sourceMap.sources) {
sources.push.apply(sources, child._sourceMap.sources);
}
});

if (sources.includes(changedFile)) {
include.push(assetName);
}
}, this);
}, this);
}

this.startTime = Date.now();

var hash = stats.compilation.hash;
var childHashes = (stats.compilation.children || []).map(child => child.hash);
var files = Object.keys(stats.compilation.assets);
var include = files.filter(function(file) {
var updated = include.filter(function(file) {
return !file.match(this.ignore);
}, this);

if (this.isRunning && (hash !== this.lastHash || !arraysEqual(childHashes, this.lastChildHashes)) && include.length > 0) {
if (this.isRunning && (hash !== this.lastHash || !arraysEqual(childHashes, this.lastChildHashes)) && updated.length > 0) {
this.lastHash = hash;
this.lastChildHashes = childHashes;
setTimeout(function onTimeout() {
this.server.notifyClients(include);
this.server.notifyClients(updated);
}.bind(this), this.delay);
}
};
Expand Down