-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
60 lines (53 loc) · 1.65 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
var Buffer = require('buffer').Buffer;
var Writable = require('stream').Writable;
var exorcist = require('exorcist');
var browserify = require('browserify');
var minifyStream = require('minify-stream');
var util;
module.exports = function(racer) {
var Backend = racer.Backend || racer.Store;
Backend.prototype.bundle = bundle;
util = racer.util;
};
function bundle(file, options, cb) {
if (typeof options === 'function') {
cb = options;
options = null;
}
options = Object.assign({}, options, {debug: true});
var minify = (options.minify == null) ? util.isProduction : options.minify;
var b = browserify(options);
this.emit('bundle', b);
b.add(file);
var bundleStream = (minify) ?
b.plugin('common-shakeify')
.plugin('browser-pack-flat/plugin')
.bundle()
.pipe(minifyStream()) :
b.bundle();
var sourceStream = new BufferStream();
var sourceMapStream = new BufferStream();
bundleStream
.pipe(exorcist(sourceMapStream, '/'))
.pipe(sourceStream);
sourceStream.on('finish', function() {
var source = sourceStream.toString()
// Remove the sourceMappingURL inserted by exorcist
.replace(/\n\/\/# sourceMappingURL=.*/, '');
var sourceMap = sourceMapStream.toString();
cb(null, source, sourceMap);
});
}
function BufferStream() {
Writable.call(this);
this.chunks = [];
}
BufferStream.prototype = Object.create(Writable.prototype);
BufferStream.prototype.constructor = BufferStream;
BufferStream.prototype._write = function(chunk, encoding, callback) {
this.chunks.push(chunk);
callback();
};
BufferStream.prototype.toString = function() {
return Buffer.concat(this.chunks).toString();
};