-
Notifications
You must be signed in to change notification settings - Fork 146
/
runner.js
227 lines (195 loc) · 5.74 KB
/
runner.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
var _ = require('lodash');
var path = require('path');
var resolve = require('resolve');
var glob = require('glob');
module.exports = GruntBrowserifyRunner;
function GruntBrowserifyRunner(options) {
this.browserify = options.browserify;
this.watchify = options.watchify;
this.logger = options.logger;
this.writer = options.writer;
this.firstBuild = true;
}
GruntBrowserifyRunner.prototype = _.create(GruntBrowserifyRunner.prototype, {
run: function (files, destination, options, next) {
var self = this;
//set constructor options and instantiate
var bOpts = _.cloneDeep(options.browserifyOptions) || {};
bOpts.entries = bOpts.entries || files;
// watchify options
var wOpts = options.watchifyOptions || {};
// Watchify requires specific arguments
if(options.watch) {
bOpts = _.extend({ cache: {}, packageCache: {} }, bOpts);
}
//determine watchify or browserify
var b = options.watch ? this.watchify(this.browserify(bOpts), wOpts) : this.browserify(bOpts);
b.on('error', function (err) {
self.logger.fail.warn(err);
});
if(options.bundleOptions) {
throw new Error('bundleOptions is no longer used. Move all option in browserifyOptions.');
}
if(options.alias) {
if(_.isPlainObject(options.alias)) {
for(var alias in options.alias) {
b.require(options.alias[alias], {expose: alias});
}
}
else {
requireFiles(b, options.alias);
}
}
if(options.require) {
requireFiles(b, options.require);
}
if (options.exclude) {
_.forEach(options.exclude, function (file) {
runOptionForGlob(b, 'exclude', file);
});
}
if (options.ignore) {
_.forEach(options.ignore, function (file) {
runOptionForGlob(b, 'ignore', file);
});
}
if (options.external) {
// allow externalizing of alias object
if(_.isPlainObject(options.external)) {
for(var id in options.external) {
if (testForGlob(id)) {
runOptionForGlob(b, 'external', id);
}
else {
b.external(id);
}
}
}
else {
_.forEach(options.external, function (id) {
//allow externalizing of require lists
if (id.match(':')) {
id = id.split(':')[1];
}
if (testForGlob(id)) {
runOptionForGlob(b, 'external', id);
}
else {
b.external(id);
}
});
}
}
if (options.transform) {
_.forEach(options.transform, function (transformer) {
if (typeof transformer !== 'object') {
b.transform(transformer);
}
else {
b.transform(transformer[1], transformer[0]);
}
});
}
if (options.plugin) {
_.forEach(options.plugin, function (plugin) {
if (typeof plugin !== 'object') {
b.plugin(plugin);
}
else {
b.plugin(plugin[0], plugin[1]);
}
});
}
var destPath = this.createDestDir(destination);
var keepAlive = this.keepAliveFn.bind(this, destination);
var done = options.keepAlive? keepAlive : next;
var bundleComplete = this.onBundleComplete(destination, options, done);
if (options.watch) {
var bundleUpdate = this.onBundleComplete(destination, options, keepAlive);
b.on('update', function (ids) {
ids.forEach(function (id) {
self.logger.log.ok(id.cyan + ' changed, updating bundle.');
});
doBundle(b, options, bundleUpdate);
});
}
if (options.configure) {
options.configure(b);
}
doBundle(b, options, bundleComplete);
},
createDestDir: function (destination) {
var destPath = path.dirname(path.resolve(destination));
if (!this.writer.exists(destPath)) {
this.writer.mkdir(destPath);
}
return destPath;
},
keepAliveFn: function (destination) {
//this.logger.log.ok('Watchifying...');
},
onBundleComplete: function (destination, options, next) {
var self = this;
return function (err, buf) {
if (err) {
self.logger.log.error(err);
if (self.firstBuild || !options.keepAlive) {
self.logger.fail.warn('Error running grunt-browserify.');
}
}
else if (buf) {
// prepend the banner
if(options.banner) {
buf = Buffer.concat([new Buffer(options.banner + '\n', 'utf8'), buf]);
}
self.logger.log.ok('Bundle ' + destination.cyan + ' created. ' + (options.keepAlive ? 'Watchifying...' : ''));
self.writer.write(destination, buf);
}
self.firstBuild = false;
next();
};
}
});
function doBundle(browserifyInstance, opts, bundleComplete) {
if (opts.preBundleCB) {
opts.preBundleCB(browserifyInstance);
}
browserifyInstance.bundle(function (err, buf) {
if (opts.postBundleCB) {
opts.postBundleCB(err, buf, bundleComplete);
}
else {
bundleComplete(err, buf);
}
});
}
function testForGlob(id) {
return (/\*/.test(id));
}
function runOptionForGlob(browserifyInstance, method, pattern) {
var files = glob.sync(pattern);
if (!files || files.length < 1) {
//it's not a glob, it's a file / module path
files = [pattern];
}
files.forEach(function (f) {
browserifyInstance[method].call(browserifyInstance, f);
});
}
function requireFiles(b, requiredFiles) {
_.forEach(requiredFiles, function (file) {
var filePath, opts;
if (Array.isArray(file)) {
filePath = file[0];
opts = file[1];
}
else {
var filePair = file.split(':');
filePath = filePair[0];
opts = {
expose: filePair.length === 1 ? filePair[0] : filePair[1]
};
}
b.require(filePath, opts);
});
}