forked from atomify/atomify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
255 lines (211 loc) · 7.23 KB
/
index.js
File metadata and controls
255 lines (211 loc) · 7.23 KB
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
'use strict'
var browserify = require('browserify')
, browserifyInc = require('browserify-incremental')
, path = require('path')
, fs = require('fs')
, events = require('events')
, mkdirp = require('mkdirp')
, watchify = require('watchify')
, ejsify = require('ejsify')
, hbsfy = require('hbsfy')
, jadeify = require('jadeify')
, envify = require('envify')
, partialify = require('partialify')
, babelify = require('babelify')
, brfs = require('brfs')
, writer = require('write-to-path')
, emitter = new events.EventEmitter()
, streamBuffer = require('stream-buffers')
, _ = require('lodash')
, minifyify = require('minifyify')
, resrcify = require('resrcify')
, factorBundle = require('factor-bundle')
, ctor
ctor = module.exports = function atomifyJs (opts, cb) {
var outputPath
, outputDir
, writeFile
, _outputcb
, _buffercb
, browserifyOptions
, transforms
, assets
, outputs
, b
, w
if (Array.isArray(opts)) opts = {entries: opts}
if (typeof opts === 'string') opts = {entries: [opts]}
if (opts.entry) opts.entries = [opts.entry]
if (!opts.entries) opts.entries = []
if (opts.watch && opts.cache) throw new Error('You can only cache if not using watch')
// js('entry.js', 'bundle.js')
if (typeof cb === 'string') opts.output = cb
if (opts.output) {
// we definitely have to write the file
outputPath = path.resolve(process.cwd(), opts.output)
outputDir = path.dirname(outputPath)
writeFile = writer(outputPath, {debug: opts.debug})
if (!fs.existsSync(outputDir)) mkdirp.sync(outputDir)
// we might need to call a callback also
if (typeof cb === 'function') {
_outputcb = cb
cb = function wrappedCallback (err, src, map) {
if (err) return _outputcb(err)
writeFile(null, src)
_outputcb(null, src, map)
}
}
else {
cb = writeFile
}
}
_buffercb = cb
cb = function wrappedBufferCallback (err, buff, map) {
if (typeof _buffercb === 'function') _buffercb(err, buff, map)
}
opts.debug = opts.debug || false
if (opts.minify === true) {
opts.minify = {map: false}
}
// Debug mode must be on to get sourcemaps
else if (typeof opts.minify === 'object') {
opts.debug = true
}
browserifyOptions = _.omit(opts, ['entry', 'entries', 'require'])
// mixin the required watchify options if we need to watch
// these are required for creating the browserify instance
// remove the default 600ms delay because speed is tood
// ignoreWatch to true to ignore node_modules
if (opts.watch) _.extend({delay: 0, ignoreWatch: true}, browserifyOptions, watchify.args)
// mixin the required browserifyInc options if we need to cache
if (opts.cache) _.extend(browserifyOptions, browserifyInc.args)
b = browserify(browserifyOptions)
if (opts.cache) {
b = browserifyInc(b
, _.isString(opts.cache) ? {cacheFile: opts.cache} : {}
)
}
emitter.emit('browserify', b)
if (opts.watch) {
w = watchify(b)
emitter.emit('watchify', w)
}
if (opts.watch) {
w.on('update', function onUpdate (ids) {
ids.forEach(function eachId (id) {
emitter.emit('changed', id)
})
w.bundle(cb)
})
w.on('time', function onTime (time) {
emitter.emit('bundle', time)
})
}
b.on('package', function onBrowserifyPackage (pkg) {
emitter.emit('package', pkg)
})
opts.entries.forEach(function eachEntry (entry) {
b.add(path.resolve(process.cwd(), entry))
})
// Browserify modifies the transforms property once opts is passed in to bundle()
// so we copy that prop here to ensure we only use what is passed in from config
if (!opts._transforms) {
opts._transforms = opts.transforms ? opts.transforms.slice(0) : []
}
transforms = opts.defaultTransforms !== false ?
[
envify
, ejsify
, hbsfy
, jadeify
, partialify
, [babelify, {modules: 'common'}]
]
// ensure brfs runs last because it requires valid js
.concat(opts._transforms, [brfs])
: opts._transforms || []
transforms.forEach(function eachTransform (transform) {
if (Array.isArray(transform)) {
b.transform(transform[1], transform[0])
}
else {
b.transform(transform)
}
})
// reset list of global transforms every time
opts._globalTransforms = opts.globalTransforms ? opts.globalTransforms.slice(0) : []
if (opts.assets) {
assets = [resrcify, {
dest: opts.assets.dest || ''
, prefix: opts.assets.prefix || ''
, retainName: opts.assets.retainName || ''
}]
opts._globalTransforms.push(assets)
}
opts._globalTransforms.forEach(function eachGlobalTransform (transform) {
var transformOptions
if (Array.isArray(transform)) {
transformOptions = transform[1]
transformOptions.global = true
b.transform(transform[0], transformOptions)
}
else {
b.transform(transform, {global: true})
}
})
if (typeof opts.minify === 'object') {
b.plugin(minifyify, opts.minify)
}
if (opts.require) {
if (Array.isArray(opts.require) || typeof opts.require === 'string'){
opts.require = {file: opts.require}
}
b.require(opts.require.file, opts.require.opts)
}
if (Array.isArray(opts.external) || typeof opts.external === 'string'){
b.external(opts.external)
}
// if we've got the common option, we want to use factor bundle
if (opts.common === true){
if (opts.entries.length < 2) {
// TODO: we should do this, but it casues tape to falsely see the error event on itself!?
// emitter.emit('error', error)
return void cb(new Error('the `common` option requires an `entries` option with more than one entry'))
}
// for each entry, we're going to create a stream-able buffer to put the factored bundle into
outputs = {}
opts.entries.forEach(function createOutputs (entry) {
outputs[path.basename(entry).replace(path.extname(entry), '')] = new streamBuffer.WritableStreamBuffer({
// these values are arbitrary, but we don't want to require huge buffers
// start as 1 kilobytes.
initialSize: 1 * 1024
// grow by 1 kilobytes each time buffer overflows.
, incrementAmount: 1 * 1024
})
})
// setup factor bundle, pass in our streamable-buffers as the output source
b.plugin(factorBundle, {
o: _.values(outputs)
})
// we need to wrap the callback to output an object with all the bundles
return b.bundle(function bundledWithCommon (err, common) {
var hasCallback = _.isFunction(cb)
, out = {}
if (err && hasCallback) return void cb(err)
// turn the stream-able buffers into plain buffers
out = _.mapValues(outputs, function convertStreamToBuffer (stream, entryName) {
var entryBuffer = stream.getContents()
// for those using the streaming interface, emit an event with the entry
// do this here so that we don't have to itterate through the outputs twice
emitter.emit('entry', entryBuffer, entryName)
return entryBuffer
})
// add in the common bundle
out.common = common
if (hasCallback) cb(err, out)
})
}
// if we don't need to use factor bundle, just browserify!
else return b.bundle(cb)
}
ctor.emitter = emitter