-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwatch.js
79 lines (68 loc) · 2 KB
/
watch.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
import browserify from 'browserify'
import chalk from 'chalk'
import fs from 'fs'
import rollupify from 'rollupify'
import watchify from 'watchify'
let watchError
export default function watch(opts) {
const b = browserify({
cache: {},
// debug: true,
entries: [opts.entry],
packageCache: {},
plugin: [watchify]
})
const domain = chalk.dim(`[${opts.domain}${opts.root}]`)
// entry point of our app, panels needs this to require it
b.require(opts.entry, {expose: opts.expose})
// expose the app dependencies
b.require(opts.requires)
// rollupify the bundle
b.transform(rollupify, {config: opts.rollupConfig, sourceMaps: false})
// declare our build's externals
opts.externals.forEach(dep => b.external(dep))
// run the bundle and output to the console
function bundle() {
b.bundle().pipe(fs.createWriteStream(opts.tmp))
}
bundle()
b.on('update', bundle)
b.on('log', msg => {
console.log(domain, msg)
})
b.on('bundle', theBundle => {
theBundle.on('error', error => {
if (watchError !== error.stack) {
if (error.codeFrame) {
console.error(domain, chalk.red(`${error.constructor.name} at ${error.id}`))
console.error(domain, error.codeFrame)
} else {
const match = error.stack.match(/Error: Could not resolve (.+?) from (.+?) while/)
if (match) {
console.error(domain, chalk.red(`ImportError at ${match[2]}`))
console.error(domain, 'Does', chalk.blue(match[1]), 'exist? Check that import statement.')
} else {
console.error(domain, error.stack)
}
}
watchError = error.stack
// } else {
// console.error(domain, error)
}
b.removeAllListeners()
b.close()
setTimeout(() => watch(opts), 1000)
})
})
return function cleanup() {
try {
fs.unlinkSync(opts.tmp)
} catch(err) {
}
try {
fs.unlinkSync(`${opts.entry}.tmp`)
} catch(err) {
}
process.exit()
}
}