-
Notifications
You must be signed in to change notification settings - Fork 83
/
index.js
275 lines (221 loc) · 6.77 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var unpack = require('browser-unpack')
var builtins = require('builtins')
var through = require('through')
var duplex = require('duplexer')
var pluck = require('plucker')
var uniq = require('uniq')
var commondir = require('commondir')
var fileTree = require('file-tree')
var path = require('path')
var fs = require('fs')
var bl = require('bl')
var versions = require('./lib/versions')
module.exports = createStream
createStream.json = json
createStream.bundle = bundle
// TODO: When Node 10 support is dropped, replace this with Array.prototype.flat
function flat (arr1) {
return arr1.reduce(
(acc, val) => Array.isArray(val)
? acc.concat(flat(val))
: acc.concat(val),
[]
)
}
function createStream(opts) {
opts = opts || {}
var buffer = bl(function(err, content) {
if (err) return stream.emit('error', err)
bundle(content, opts, function(err, html) {
if (err) return stream.emit('error', err)
output.queue(html)
output.queue(null)
})
})
var output = through()
var stream = duplex(buffer, output)
return stream
}
function json(bundles, callback) {
var modules = flat(bundles
.map(String)
.map(unpack)
).map(function(module) {
if (typeof module === 'undefined') return callback(new Error(
'Unable to compile one of the supplied bundles!'
))
if (typeof module.id !== 'number') return module
return callback(new Error(
'Please recompile this browserify bundle using the --full-paths flag ' +
'(when using the command-line interface) or with the fullPaths option ' +
'set to true (when using the JavaScript API).'
))
})
modules = modules.filter(function(module) {
return module && !isEmpty(module)
})
if (!modules.length) return
modules.forEach(function (m) {
if (! isAbsolute(m.id)) {
// modules included with browserify -r
if (m.id[0] == '.') {
// "-r ./foobar"
m.id = path.resolve(m.id);
} else {
// "-r foobar"
// assume it's under node_modules
m.id = path.join(path.resolve('node_modules', m.id), 'index.js');
}
}
});
var browserifyModules = modules.filter(fromBrowserify(true))
var otherModules = modules.filter(function(module) {
if (path.basename(module.id) === '_empty.js') return false
if (browserifyModules.indexOf(module) === -1) return true
})
var root = commondir(otherModules.map(pluck('id')))
browserifyModules.forEach(function(module) {
var regex = /^.+\/node_modules\/browserify\/(?:node_modules\/)(.+)$/g
module.id = module.id.replace(regex, function(_, subpath) {
return path.resolve(root, 'browserify-core/' + subpath)
})
return module
})
uniq(modules, function(a, b) {
return a.id === b.id ? 0 : 1
}, true)
var ids = modules.map(pluck('id'))
var main = path.basename(root)
var byid = modules.reduce(function(memo, mod) {
memo[mod.id] = mod
return memo
}, {})
fileTree(ids, function(id, next) {
var row = byid[id]
if (! row) {
return next(new Error('Unknown module ' + id + ', did you compile with --full-paths?'));
}
next(null, {
size: row.source.length
, deps: Object.keys(row.deps).length
, path: id
})
}, function(err, tree) {
if (err) return callback(err)
tree = { name: main, children: tree }
dirsizes(tree)
versions(tree)
callback(null, tree)
})
}
function bundle(bundles, opts, callback) {
bundles = Array.isArray(bundles)
? bundles
: bundles ? [bundles] : []
if (typeof opts === 'function') {
callback = opts
opts = {}
}
opts = opts || {}
callback = callback || noop
var header = opts.header || opts.button || ''
var footer = opts.footer || ''
return json(bundles, function(err, data) {
if (err) return callback(err)
data.mode = opts.mode || 'size'
data = '<script type="text/javascript">'
+ ';window.disc = ('
+ JSON.stringify(data)
+ ');</script>'
var script = '<script type="text/javascript">'
+ bundled().replace(/\/script/gi, '\\/script')
+ '</script>'
callback(null, template()({
scripts: script
, styles: styles()
, markdown: footer
, header: header
, data: data
}))
})
}
function template() {
if (template.text) return template.text
return template.text = require('./lib/lazy-template')(
fs.readFileSync(__dirname + '/src/base.html', 'utf8')
)
}
function styles() {
if (styles.text) return styles.text
return styles.text = fs.readFileSync(__dirname + '/build/style.css', 'utf8')
}
function bundled() {
if (bundled.text) return bundled.text
return bundled.text = fs.readFileSync(__dirname + '/build/bundle.js', 'utf8')
}
function dirsizes(child) {
return child.size = "size" in child ? child.size : child.children.reduce(function(size, child) {
return size + ("size" in child ? child.size : dirsizes(child))
}, 0)
}
function noop(){}
function fromBrowserify(yes) {
var existsCache = {}
var no = !yes
return function(module) {
var search = '/node_modules/browserify'
var idx = module.id.indexOf(search)
var from = idx !== -1
if (!from) return no
// special case for process.js
// from insert-module-globals
if (
module.id.indexOf('insert-module-globals') !== -1 &&
module.id.split(path.sep).slice(-2).join('/') === 'process/browser.js'
) return yes
// Look up browserify's builtins file to
// determine if this file is part of browserify
// core.
var builtinFile = (
module.id.slice(0, idx + search.length) +
'/lib/builtins.js'
)
if (!(builtinFile in existsCache)) {
existsCache[builtinFile] = values(
fs.existsSync(builtinFile) &&
require(builtinFile) || {}
)
}
var localBuiltins = existsCache[builtinFile]
var bidx = localBuiltins.indexOf(module.id)
if (bidx !== -1) return yes
// Guess remaining helper files based on module
// name: this should probably be improved in the
// future.
var split = module.id.split(path.sep)
var j = split.length - 1
while (split[--j] !== 'node_modules');;
var dir = split.slice(j + 1)[0].replace(/\-(?:browser(?:ify)?|es3)$/g, '')
if (dir === 'Base64') return yes
if (dir === 'base64-js') return yes
if (dir === 'inherits') return yes
if (dir === 'process') return yes
if (dir === 'ieee754') return yes
if (builtins.indexOf(dir) !== -1) return yes
return no
}
}
function values(object) {
return Object.keys(object).map(function(key) {
return object[key]
})
}
function isEmpty(module) {
return (
path.basename(module.id) === '_empty.js' &&
(!fs.existsSync(module.id) || !fs.statSync(module.id).size)
)
}
function isAbsolute(file) {
return file.match(/^([A-Za-z]:)?\/|\\/);
}