forked from statianzo/webpack-livereload-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·100 lines (89 loc) · 2.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* jshint node:true */
var lr = require('tiny-lr');
var servers = {};
function LiveReloadPlugin(options) {
this.options = options || {};
this.port = this.options.port || 35729;
this.ignore = this.options.ignore || null;
this.lastHash = null;
this.hostname = this.options.hostname || 'localhost';
this.server = null;
}
Object.defineProperty(LiveReloadPlugin.prototype, 'isRunning', {
get: function() { return !!this.server; }
});
LiveReloadPlugin.prototype.start = function start(watching, cb) {
var port = this.port;
if (servers[port]) {
this.server = servers[port];
cb();
}
else {
this.server = servers[port] = lr(this.options);
this.server.errorListener = function serverError(err) {
console.error('Live Reload disabled: ' + err.message);
if (err.code !== 'EADDRINUSE') {
console.error(err.stack);
}
cb();
};
this.server.listen(this.port, function serverStarted(err) {
if (!err) {
console.log('Live Reload listening on port ' + port + '\n');
}
cb();
});
}
};
LiveReloadPlugin.prototype.done = function done(stats) {
var hash = stats.compilation.hash;
var files = Object.keys(stats.compilation.assets);
var include = files.filter(function(file) {
return !file.match(this.ignore);
}, this);
if (this.isRunning && hash !== this.lastHash && include.length > 0) {
this.lastHash = hash;
setTimeout(function onTimeout() {
this.server.notifyClients(include);
}.bind(this));
}
};
LiveReloadPlugin.prototype.failed = function failed() {
this.lastHash = null;
};
LiveReloadPlugin.prototype.autoloadJs = function autoloadJs() {
return [
'// webpack-livereload-plugin',
'(function() {',
' if (typeof window === "undefined") { return };',
' var id = "webpack-livereload-plugin-script";',
' if (document.getElementById(id)) { return; }',
' var el = document.createElement("script");',
' el.id = id;',
' el.async = true;',
' el.src = "http://' + this.hostname + ':' + this.port + '/livereload.js";',
' document.head.appendChild(el);',
'}());',
''
].join('\n');
};
LiveReloadPlugin.prototype.scriptTag = function scriptTag(source) {
var js = this.autoloadJs();
if (this.options.appendScriptTag && this.isRunning) {
return js + source;
}
else {
return source;
}
};
LiveReloadPlugin.prototype.applyCompilation = function applyCompilation(compilation) {
compilation.mainTemplate.plugin('startup', this.scriptTag.bind(this));
};
LiveReloadPlugin.prototype.apply = function apply(compiler) {
this.compiler = compiler;
compiler.plugin('compilation', this.applyCompilation.bind(this));
compiler.plugin('watch-run', this.start.bind(this));
compiler.plugin('done', this.done.bind(this));
compiler.plugin('failed', this.failed.bind(this));
};
module.exports = LiveReloadPlugin;