-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
141 lines (121 loc) · 3.04 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
'use strict'
const browserify = require('browserify')
const incremental = require('browserify-incremental')
const through2 = require('through2')
const colors = require('ansi-colors')
const fancyLog = require('fancy-log')
const concat = require('concat-stream')
const path = require('path')
const bundlers = {}
/**
* Return a vinyl transform stream.
*
* @param {object|function} [opts]
* @param {function} [callback]
* @return {streams.Transform}
*/
function bro(opts, callback) {
opts = parseArguments(opts, callback)
return transform(opts)
}
module.exports = bro
/* -------------------------------------------------------------------------- */
/**
* Return a vinyl transform stream.
*
* @param {object} opts
* @return {streams.Transform}
*/
function transform(opts) {
return through2.obj(function(file, encoding, next) {
const bundler = createBundler(opts, file, this)
bundler.bundle()
.on('error', createErrorHandler(opts, this))
.pipe(concat(data => {
file.contents = data
next(null, file)
}))
})
}
/**
* Return a new browserify bundler.
*
* @param {object} opts
* @param {vinyl} file
* @param {stream.Transform} transform
* @return {Browserify}
*/
function createBundler(opts, file, transform) {
// omit file contents to make browserify-incremental work propery
// on main entry (#4)
opts.entries = file.path
opts.basedir = path.dirname(file.path)
let bundler = bundlers[file.path]
if (bundler) {
bundler.removeAllListeners('log')
bundler.removeAllListeners('time')
}
else {
bundler = browserify(Object.assign(opts, incremental.args))
// only available via method call (#25)
if (opts.external) {
bundler.external(opts.external)
}
incremental(bundler)
bundlers[file.path] = bundler
}
bundler.on('log', message => transform.emit('log', message))
bundler.on('time', time => transform.emit('time', time))
return bundler
}
/**
* Return a new error handler.
*
* @param {object} opts
* @param {stream.Transform} transform
* @return {function}
*/
function createErrorHandler(opts, transform) {
return err => {
if ('emit' === opts.error) {
transform.emit('error', err)
}
else if ('function' === typeof opts.error) {
opts.error(err)
}
else {
const message = colors.red(err.name) + '\n' + err.toString()
.replace(/(ParseError.*)/, colors.red('$1'))
log(message)
}
transform.emit('end')
if (opts.callback) {
opts.callback(through2())
}
}
}
/* -------------------------------------------------------------------------- */
/**
* Parse options, arguments juggling.
*
* @param {object} opts
* @param {function} callback
* @return {object}
*/
function parseArguments(opts, callback) {
if ('function' === typeof opts) {
callback = opts
opts = {}
}
opts = opts || {}
opts.callback = callback
return opts
}
/**
* Format and log the given message.
*
* @param {string} message
*/
function log(message) {
fancyLog(`[${colors.cyan('bro')}] ${message}`)
}