-
Notifications
You must be signed in to change notification settings - Fork 19
/
watchify-bundler.js
138 lines (120 loc) · 3.11 KB
/
watchify-bundler.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var createWatchify = require('watchify')
var fromArgs = require('browserify/bin/args')
var Emitter = require('events/')
var debounce = require('debounce')
var concat = require('concat-stream')
var parseError = require('./parse-error')
var minimist = require('minimist')
var electronBuiltins = [
'ipc', 'remote', 'web-frame', 'clipboard', 'crash-reporter',
'native-image', 'screen', 'shell'
]
module.exports = watchifyBundler
function watchifyBundler (browserifyArgs, opt) {
browserifyArgs = browserifyArgs.slice()
var emitter = new Emitter()
var delay = opt.delay || 0
var closed = false
var pending = true
// allow user to disable debug-by-default
var useDebug = opt.debug !== false
var userOpts = minimist(browserifyArgs, {
boolean: 'debug',
default: { debug: true },
alias: { debug: 'd' }
})
if (userOpts.debug === false) {
useDebug = false
}
var bOpts = {
cache: {},
packageCache: {},
commondir: false, // needed for __dirname and __filename
basedir: opt.basedir,
debug: useDebug
}
// ensure browserify does not mess with our Node code
if (opt.node) {
bOpts.browserField = false
bOpts.builtins = false
// browserify tries to use require('_process') by default
// in Electron we can just insert the global process name instead
bOpts.insertGlobalVars = {
process: function () { return 'window.process' }
}
}
var browserify = fromArgs(browserifyArgs, bOpts)
// allow Electron builtins to work by default
// when node integration is enabled
if (opt.node && opt.electronBuiltins) {
electronBuiltins.forEach(function (x) {
browserify.exclude(x)
})
}
var watchify = createWatchify(browserify, {
// we use our own debounce, so make sure watchify
// ignores theirs
delay: 0
})
var contents = null
emitter.close = function () {
if (closed) {
return
}
closed = true
if (watchify) {
// needed for watchify@3.0.0
// this needs to be revisited upstream
setTimeout(function () {
watchify.close()
}, 50)
}
}
var bundleDebounced = debounce(bundle, delay)
watchify.on('update', function () {
emitter.emit('pending')
pending = true
bundleDebounced()
})
// initial bundle
emitter.emit('pending')
pending = true
bundle()
function bundle () {
if (closed) {
update()
return
}
var didError = false
var outStream = concat(function (body) {
if (!didError) {
contents = body
bundleEnd()
}
})
var wb = watchify.bundle()
// whether we should quit on errors
if (!opt.quit) {
wb.once('error', function (err) {
if (!opt.print) { // avoid duplication
console.error('%s', err)
}
err = parseError(err)
contents = ';console.error(' + JSON.stringify(err) + ');'
didError = true
bundleEnd()
})
}
wb.pipe(outStream)
function bundleEnd () {
update()
}
}
return emitter
function update () {
if (pending) {
pending = false
emitter.emit('update', contents)
}
}
}