-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathbundler.js
More file actions
562 lines (503 loc) · 20.6 KB
/
bundler.js
File metadata and controls
562 lines (503 loc) · 20.6 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
Copyright (c) 2012 Demis Bellot <[email protected]>
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// uncaught exceptions should cause the application to crash and exit
// with an exit code that will be identified as a failure by most
// windows build systems
process.on("uncaughtException", function (err) {
console.error(err);
process.exit(1);
});
function clone(o) {
var ret = {};
Object.keys(o).forEach(function (val) {
ret[val] = o[val];
});
return ret;
}
String.prototype.startsWith = function (str){
return this.indexOf(str) === 0;
};
String.prototype.endsWith = function (suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
String.prototype.endsWithAny = function (endings) {
var str = this;
return endings.some(function (ending) { return str.endsWith(ending); });
};
String.prototype.toBoolOrString = function () {
return this === 'true' ? true : this === 'false' ? false : this.toString();
};
//recursively scans the directory below for *.js.bundle and *.css.bundle files
var commandLineArgs = process.argv.splice(2); //directories specified in bundler.cmd
var commandLineOptions = commandLineArgs.filter(function (arg) { return arg.startsWith('#'); });
var defaultOptions = {};
commandLineOptions.forEach(function (option) {
while (option.startsWith('#')) { option = option.substring(1); }
var parts = option.split(':');
defaultOptions[parts[0].toLowerCase()] = parts.length > 1 ? parts[1].toBoolOrString() : true;
});
var SCAN_ROOT_DIRS = commandLineArgs.filter(function (arg) { return !arg.startsWith('#'); });
if (!SCAN_ROOT_DIRS.length) {
console.log("No directories were specified.");
console.log("Usage: bundler.js [#option:value] ../Content [../Scripts]");
return;
}
var fs = require("fs"),
path = require("path"),
jsp = require("uglify-js").parser,
pro = require("uglify-js").uglify,
less = require('less'),
sass = require('node-sass'),
stylus = require('stylus'),
nib = require('nib'),
coffee = require('coffee-script'),
livescript = require('livescript'),
CleanCss = require('clean-css'),
Step = require('step'),
startedAt = Date.now();
var walk = function (dir, done) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err) throw err;
var i = 0;
(function next() {
var file = list[i++];
if (!file) return done(null, results);
file = dir + '/' + file;
fs.stat(file, function (_, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (_, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
})();
});
};
var scanIndex = 0;
(function scanNext() {
if (scanIndex < SCAN_ROOT_DIRS.length) {
var rootDir = SCAN_ROOT_DIRS[scanIndex++];
fs.exists(rootDir, function(exists) {
if (exists) {
walk(rootDir, function(err, allFiles) {
if (err) throw err;
scanDir(allFiles, scanNext);
});
} else {
console.log("\nSpecified dir '" + rootDir + "' does not exist, skipping...");
scanNext();
}
});
} else
console.log("\nDone. " + (Date.now() - startedAt) + "ms");
})();
function scanDir(allFiles, cb) {
var jsBundles = allFiles.filter(function (file) { return file.endsWith(".js.bundle"); });
var cssBundles = allFiles.filter(function (file) { return file.endsWith(".css.bundle"); });
function getOptions(fileLines) {
var options = clone(defaultOptions);
if (fileLines.length === 0) return options;
var optionsString = fileLines[0];
if (!optionsString.startsWith('#options ')) return options;
optionsString.substring(9).split(',').forEach(function (option) {
var parts = option.split(':');
options[parts[0].toLowerCase()] = parts.length > 1 ? parts[1].toBoolOrString() : true;
});
return options;
};
Step(
function () {
var next = this;
var index = 0;
var nextBundle = function () {
if (index < jsBundles.length)
processBundle(jsBundles[index++]);
else
next();
};
function processBundle(jsBundle) {
var bundleDir = path.dirname(jsBundle);
var bundleName = jsBundle.replace('.bundle', '');
readTextFile(jsBundle, function (data) {
var jsFiles = removeCR(data).split("\n");
var options = getOptions(jsFiles);
if (options.folder !== undefined) {
options.nobundle = true;
var recursive = options.folder === 'recursive';
jsFiles = allFiles.map(function jsMatches(fileName) {
if (!fileName.startsWith(bundleDir)) return '#';
if (!fileName.endsWithAny(['.js', '.coffee', '.ls', '.ts'])) return '#';
if (fileName.endsWithAny(['.min.js'])) return '#';
if (!recursive && (path.dirname(fileName) !== bundleDir)) return '#';
return fileName.substring(bundleDir.length + 1);
});
}
processJsBundle(options, jsBundle, bundleDir, jsFiles, bundleName, nextBundle);
});
};
nextBundle();
},
function () {
var next = this;
var index = 0;
var nextBundle = function () {
if (index < cssBundles.length)
processBundle(cssBundles[index++]);
else
next();
};
function processBundle(cssBundle) {
var bundleDir = path.dirname(cssBundle);
var bundleName = cssBundle.replace('.bundle', '');
readTextFile(cssBundle, function (data) {
var cssFiles = removeCR(data).split("\n");
var options = getOptions(cssFiles);
if (options.folder !== undefined) {
options.nobundle = true;
var recursive = options.folder === 'recursive';
cssFiles = allFiles.map(function cssMatches(fileName) {
if (!fileName.startsWith(bundleDir)) return '#';
if (!fileName.endsWithAny(['.css', '.less', '.sass', '.scss', '.styl'])) return '#';
if (fileName.endsWithAny(['.min.css'])) return '#';
if (!recursive && (path.dirname(fileName) !== bundleDir)) return '#';
if (fileName.match(/_[^/]+\.s[ca]ss$/)) return '#';
return fileName.substring(bundleDir.length + 1);
});
}
processCssBundle(options, cssBundle, bundleDir, cssFiles, bundleName, nextBundle);
});
};
nextBundle();
},
cb
);
}
function getMinFileName(fileName) {
var extension = fileName.substring(fileName.lastIndexOf('.'));
return fileName.substring(0, fileName.length - extension.length) + ".min" + extension;
}
function processJsBundle(options, jsBundle, bundleDir, jsFiles, bundleName, cb) {
console.log("\nprocessing " + jsBundle + ":");
for (var optionKey in options) {
console.log("option: " + optionKey + " -> " + options[optionKey]);
}
var allJsArr = [], allMinJsArr = [], index = 0, pending = 0;
var whenDone = function () {
if (options.nobundle && options.outputbundleonly !== true) {
setTimeout(cb, 0);
return;
}
console.log("writing " + bundleName + "...");
var allJs = "", allMinJs = "";
for (var i = 0; i < allJsArr.length; i++) {
allJs += ";" + allJsArr[i] + "\n";
allMinJs += ";" + allMinJsArr[i] + "\n";
}
var afterBundle = options.skipmin ? cb : function (_) {
var minFileName = getMinFileName(bundleName);
fs.writeFile(minFileName, allMinJs, cb);
};
if (!options.bundleminonly) {
fs.writeFile(bundleName, allJs, afterBundle);
} else {
afterBundle();
}
};
jsFiles.forEach(function (file) {
// Skip blank lines/files beginning with '.' or '#', but allow ../relative paths
if (!(file = file.trim())
|| (file.startsWith(".") && !file.startsWith(".."))
|| file.startsWith('#'))
return;
var isCoffee = file.endsWith(".coffee");
var isLiveScript = file.endsWith(".ls");
var jsFile = isCoffee ?
file.replace(".coffee", ".js")
: isLiveScript ?
file.replace(".ls", ".js") :
file;
var filePath = path.join(bundleDir, file),
jsPath = path.join(bundleDir, jsFile),
minJsPath = options.minfolder
? path.join(bundleDir, options.minfolder, path.dirname(jsFile), getMinFileName(path.basename(jsFile)))
: getMinFileName(jsPath);
var i = index++;
pending++;
Step(
function () {
var next = this;
if (isCoffee) {
readTextFile(filePath, function (coffee) {
getOrCreateJs(options, coffee, filePath, jsPath, next);
});
} else if(isLiveScript){
readTextFile(filePath, function(livescriptText){
getOrCreateJsLiveScript(options, livescriptText, filePath, jsPath, next);
});
} else {
readTextFile(jsPath, next);
}
},
function (js) {
allJsArr[i] = js;
var withMin = function (minJs) {
allMinJsArr[i] = minJs;
if (! --pending) whenDone();
};
if (options.skipmin) {
withMin('');
} else if (/(\.min\.|\.pack\.)/.test(file) && options.skipremin) {
readTextFile(jsPath, withMin);
} else {
getOrCreateMinJs(options, js, jsPath, minJsPath, withMin);
}
}
);
});
}
function processCssBundle(options, cssBundle, bundleDir, cssFiles, bundleName, cb) {
console.log("\nprocessing " + cssBundle + ":");
for (var optionKey in options) {
console.log("option: " + optionKey + " -> " + options[optionKey]);
}
var allCssArr = [], allMinCssArr = [], index = 0, pending = 0;
var whenDone = function () {
if (options.nobundle && options.outputbundleonly !== true) {
setTimeout(cb, 0);
return;
}
console.log("writing " + bundleName + "...");
var allCss = "", allMinCss = "";
for (var i = 0; i < allCssArr.length; i++) {
allCss += allCssArr[i] + "\n";
allMinCss += allMinCssArr[i] + "\n";
}
var afterBundle = options.skipmin ? cb : function (_) {
var minFileName = getMinFileName(bundleName);
fs.writeFile(minFileName, allMinCss, cb);
};
if (!options.bundleminonly) {
fs.writeFile(bundleName, allCss, afterBundle);
} else {
afterBundle();
}
};
cssFiles.forEach(function (file) {
if (!(file = file.trim())
|| (file.startsWith(".") && !file.startsWith(".."))
|| file.startsWith('#'))
return;
var isLess = file.endsWith(".less");
var isSass = (file.endsWith(".sass") || file.endsWith(".scss"));
var isStylus = file.endsWith(".styl");
var cssFile = isLess ?
file.replace(".less", ".css")
: isSass ?
file.replace(".sass", ".css").replace(".scss", ".css")
: isStylus ?
file.replace(".styl", ".css") :
file;
var filePath = path.join(bundleDir, file),
cssPath = path.join(bundleDir, cssFile),
minCssPath = options.minfolder
? path.join(bundleDir, options.minfolder, path.dirname(cssFile), getMinFileName(path.basename(cssFile)))
: getMinFileName(cssPath);
var i = index++;
pending++;
Step(
function () {
var next = this;
if (isLess) {
readTextFile(filePath, function (lessText) {
getOrCreateLessCss(options, lessText, filePath, cssPath, next);
});
} else if (isSass) {
readTextFile(filePath, function (sassText) {
getOrCreateSassCss(options, sassText, filePath, cssPath, next);
});
} else if (isStylus){
readTextFile(filePath, function (stylusText) {
getOrCreateStylusCss(options, stylusText, filePath, cssPath, next);
});
} else {
readTextFile(cssPath, next);
}
},
function (css) {
allCssArr[i] = css;
var withMin = function (minCss) {
var rebaseOptions = {
target: path.resolve(bundleName),
relativeTo: path.resolve(path.dirname(cssPath)),
advanced: false
};
allMinCssArr[i] = new CleanCss(rebaseOptions).minify(minCss).styles;
if (! --pending) whenDone();
};
if (options.skipmin) {
withMin('');
} else if (/(\.min\.|\.pack\.)/.test(file) && options.skipremin) {
readTextFile(cssPath, withMin);
}else {
getOrCreateMinCss(options, css, cssPath, minCssPath, withMin);
}
}
);
});
}
function getOrCreateJs(options, coffeeScript, csPath, jsPath, cb /*cb(js)*/) {
compileAsync(options, "compiling", function (coffeeScript, csPath, cb) {
cb(coffee.compile(coffeeScript));
}, coffeeScript, csPath, jsPath, cb);
}
function getOrCreateJsLiveScript(options, livescriptText, lsPath, jsPath, cb /*cb(js)*/) {
compileAsync(options, "compiling", function (livescriptText, lsPath, cb) {
cb(livescript.compile(livescriptText));
}, livescriptText, lsPath, jsPath, cb);
}
function getOrCreateMinJs(options, js, jsPath, minJsPath, cb /*cb(minJs)*/) {
compileAsync(options, "minifying", function (js, jsPath, cb) {
cb(minifyjs(js));
}, js, jsPath, minJsPath, cb);
}
function getOrCreateLessCss(options, less, lessPath, cssPath, cb /*cb(css)*/) {
compileAsync(options, "compiling", compileLess, less, lessPath, cssPath, cb);
}
function getOrCreateSassCss(options, sassText, sassPath, cssPath, cb /*cb(sass)*/) {
var explodedSassPath = sassPath.split('\\');
if (explodedSassPath.length == 0) {
explodedSassPath = sassPath.split('/');
}
var sassFileName = explodedSassPath.pop();
var includePaths = [sassPath.replace(sassFileName, '')];
compileAsync(options, "compiling", function (sassText, sassPath, cb) {
cb(sass.renderSync({
file: sassPath,
includePaths: includePaths
}));
}, sassText, sassPath, cssPath, cb);
}
function getOrCreateStylusCss(options, stylusText, stylusPath, cssPath, cb /*cb(css)*/) {
compileAsync(options, "compiling", function (stylusText, stylusPath, cb) {
stylus(stylusText)
.set('filename', stylusPath)
.use(nib())
.render(function(err, css){
if(err){
throw new Error(err);
}
cb(css);
});
}, stylusText, stylusPath, cssPath, cb);
}
function getOrCreateMinCss(options, css, cssPath, minCssPath, cb /*cb(minCss)*/) {
compileAsync(options, "minifying", function (css, cssPath, cb) {
cb(new CleanCss().minify(css).styles);
}, css, cssPath, minCssPath, cb);
}
function compileAsync(options, mode, compileFn /*compileFn(text, textPath, cb(compiledText))*/,
text, textPath, compileTextPath, cb /*cb(compiledText)*/) {
Step(
function () {
fs.exists(compileTextPath, this);
},
function (exists) {
var next = this;
if (!exists)
next(!exists);
else
fs.stat(textPath, function (_, textStat) {
fs.stat(compileTextPath, function (_, minTextStat) {
next(minTextStat.mtime.getTime() < textStat.mtime.getTime());
});
});
},
function (doCompile) {
if (doCompile) {
console.log(mode + " " + compileTextPath + "...");
var onAfterCompiled = function(minText) {
if (options.outputbundleonly) {
cb(minText);
} else {
fs.mkdirp(path.dirname(compileTextPath), null, function(_) {
fs.writeFile(compileTextPath, minText, 'utf-8', function(_) {
cb(minText);
});
});
}
};
compileFn(text, textPath, onAfterCompiled);
} else {
readTextFile(compileTextPath, cb);
}
}
);
}
function compileLess(lessCss, lessPath, cb) {
var lessDir = path.dirname(lessPath),
fileName = path.basename(lessPath),
options = {
paths: ['.', lessDir], // Specify search paths for @import directives
filename: fileName
};
less.render(lessCss, options, function (err, css) {
if (err) throw err;
cb(css);
});
}
function minifyjs(js) {
var ast = jsp.parse(js);
ast = pro.ast_mangle(ast);
ast = pro.ast_squeeze(ast);
var minJs = pro.gen_code(ast);
return minJs;
}
function removeCR(text) {
return text.replace(/\r/g, '');
}
function stripBOM(content) {
// Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
// because the buffer-to-string conversion in `fs.readFileSync()`
// translates it to FEFF, the UTF-16 BOM.
if (content.charCodeAt(0) === 0xFEFF) {
content = content.slice(1);
}
return content;
}
function readTextFile(filePath, cb) {
fs.readFile(filePath, 'utf-8', function(err, fileContents) {
if (err) throw err;
cb(stripBOM(fileContents));
});
}
fs.mkdirp = function(dirpath, mode, callback) {
fs.mkdir(dirpath, mode, function(error) {
if (error && error.errno === 34) {
fs.mkdirp(path.dirname(dirpath), mode, callback);
fs.mkdirp(dirpath, mode, callback);
}
callback && callback(error);
});
};