-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
livereload.js
58 lines (50 loc) · 1.8 KB
/
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
import { createServer } from 'livereload'
import { find } from 'port-authority'
const state = (global.PLUGIN_LIVERELOAD = global.PLUGIN_LIVERELOAD || {
server: null,
})
export function livereload(options = { watch: '' }) {
if (typeof options === 'string') {
options = {
watch: options,
}
} else {
options.watch = options.watch || ''
}
// release previous server instance if rollup is reloading configuration
// in watch mode
if (state.server) {
state.server.close()
}
let enabled = options.verbose === false
const portPromise = find(options.port || 35729)
portPromise.then(port => {
state.server = createServer({ ...options, port })
})
return {
name: 'livereload',
async banner() {
const port = await portPromise
const snippetSrc = options.clientUrl
? JSON.stringify(options.clientUrl)
: process.env.CODESANDBOX_SSE
? `'//' + (window.location.host.replace(/^([^.]+)-\\d+/,"$1").replace(/^([^.]+)/, "$1-${port}")).split(':')[0] + '/livereload.js?snipver=1&port=443'`
: `'//' + (window.location.host || 'localhost').split(':')[0] + ':${port}/livereload.js?snipver=1'`
return `(function(l, r) { if (l.getElementById('livereloadscript')) return; r = l.createElement('script'); r.async = 1; r.src = ${snippetSrc}; r.id = 'livereloadscript'; l.getElementsByTagName('head')[0].appendChild(r) })(window.document);`
},
async generateBundle() {
if (!enabled) {
enabled = true
const port = await portPromise
const customPort = port !== 35729 ? ' on port ' + port : ''
console.log(green('LiveReload enabled' + customPort))
}
},
buildEnd() {
state.server.refresh('/');
}
}
}
function green(text) {
return '\u001b[1m\u001b[32m' + text + '\u001b[39m\u001b[22m'
}