-
Notifications
You must be signed in to change notification settings - Fork 10
/
plugin-livereload.js
59 lines (49 loc) · 1.73 KB
/
plugin-livereload.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
const livereload = require('livereload');
const { ResourceInterface } = require('../../lib/resource-interface');
const { ServerInterface } = require('../../lib/server-interface');
class LiveReloadServer extends ServerInterface {
constructor(compilation, options = {}) {
super(compilation, options);
this.liveReloadServer = livereload.createServer({
exts: ['html', 'css', 'js', 'md'],
applyCSSLive: false // https://github.com/napcs/node-livereload/issues/33#issuecomment-693707006
});
}
async start() {
const { userWorkspace } = this.compilation.context;
this.liveReloadServer.watch(userWorkspace, () => {
console.info(`Now watching directory "${userWorkspace}" for changes.`);
return Promise.resolve(true);
});
}
}
class LiveReloadResource extends ResourceInterface {
async shouldIntercept(url, body, headers) {
const { accept } = headers.request;
return Promise.resolve(accept && accept.indexOf('text/html') >= 0 && process.env.__GWD_COMMAND__ === 'develop'); // eslint-disable-line no-underscore-dangle
}
async intercept(url, body) {
return new Promise((resolve, reject) => {
try {
const contents = body.replace('</head>', `
<script src="http://localhost:35729/livereload.js?snipver=1"></script>
</head>
`);
resolve({ body: contents });
} catch (e) {
reject(e);
}
});
}
}
module.exports = (options = {}) => {
return [{
type: 'server',
name: 'plugin-live-reload:server',
provider: (compilation) => new LiveReloadServer(compilation, options)
}, {
type: 'resource',
name: 'plugin-live-reload:resource',
provider: (compilation) => new LiveReloadResource(compilation, options)
}];
};