-
Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathoptimize.js
523 lines (455 loc) · 23.2 KB
/
optimize.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
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
/**
* @license Copyright (c) 2010-2014, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
/*jslint plusplus: true, nomen: true, regexp: true */
/*global define: false */
define([ 'lang', 'logger', 'env!env/optimize', 'env!env/file', 'parse',
'pragma', 'uglifyjs/index', 'uglifyjs2',
'source-map'],
function (lang, logger, envOptimize, file, parse,
pragma, uglify, uglify2,
sourceMap) {
'use strict';
var optimize,
cssImportRegExp = /\@import\s+(url\()?\s*([^);]+)\s*(\))?([\w, ]*)(;)?/ig,
cssCommentImportRegExp = /\/\*[^\*]*@import[^\*]*\*\//g,
cssUrlRegExp = /\url\(\s*([^\)]+)\s*\)?/g,
SourceMapGenerator = sourceMap.SourceMapGenerator,
SourceMapConsumer =sourceMap.SourceMapConsumer;
/**
* If an URL from a CSS url value contains start/end quotes, remove them.
* This is not done in the regexp, since my regexp fu is not that strong,
* and the CSS spec allows for ' and " in the URL if they are backslash escaped.
* @param {String} url
*/
function cleanCssUrlQuotes(url) {
//Make sure we are not ending in whitespace.
//Not very confident of the css regexps above that there will not be ending
//whitespace.
url = url.replace(/\s+$/, "");
if (url.charAt(0) === "'" || url.charAt(0) === "\"") {
url = url.substring(1, url.length - 1);
}
return url;
}
function fixCssUrlPaths(fileName, path, contents, cssPrefix) {
return contents.replace(cssUrlRegExp, function (fullMatch, urlMatch) {
var colonIndex, firstChar, parts, i,
fixedUrlMatch = cleanCssUrlQuotes(urlMatch);
fixedUrlMatch = fixedUrlMatch.replace(lang.backSlashRegExp, "/");
//Only do the work for relative URLs. Skip things that start with / or #, or have
//a protocol.
firstChar = fixedUrlMatch.charAt(0);
colonIndex = fixedUrlMatch.indexOf(":");
if (firstChar !== "/" && firstChar !== "#" && (colonIndex === -1 || colonIndex > fixedUrlMatch.indexOf("/"))) {
//It is a relative URL, tack on the cssPrefix and path prefix
urlMatch = cssPrefix + path + fixedUrlMatch;
} else {
logger.trace(fileName + "\n URL not a relative URL, skipping: " + urlMatch);
}
//Collapse .. and .
parts = urlMatch.split("/");
for (i = parts.length - 1; i > 0; i--) {
if (parts[i] === ".") {
parts.splice(i, 1);
} else if (parts[i] === "..") {
if (i !== 0 && parts[i - 1] !== "..") {
parts.splice(i - 1, 2);
i -= 1;
}
}
}
return "url(" + parts.join("/") + ")";
});
}
/**
* Inlines nested stylesheets that have @import calls in them.
* @param {String} fileName the file name
* @param {String} fileContents the file contents
* @param {String} cssImportIgnore comma delimited string of files to ignore or array of regexp/string
* @param {String} cssPrefix string to be prefixed before relative URLs
* @param {Object} included an object used to track the files already imported
*/
function flattenCss(fileName, fileContents, cssImportIgnore, cssPrefix, included, topLevel) {
//Find the last slash in the name.
fileName = fileName.replace(lang.backSlashRegExp, "/");
var endIndex = fileName.lastIndexOf("/"),
//Make a file path based on the last slash.
//If no slash, so must be just a file name. Use empty string then.
filePath = (endIndex !== -1) ? fileName.substring(0, endIndex + 1) : "",
//store a list of merged files
importList = [],
skippedList = [];
//First make a pass by removing any commented out @import calls.
fileContents = fileContents.replace(cssCommentImportRegExp, '');
//Make sure we have a delimited ignore list to make matching faster
if (cssImportIgnore && typeof cssImportIgnore === "string" && cssImportIgnore.charAt(cssImportIgnore.length - 1) !== ",") {
cssImportIgnore += ",";
}
fileContents = fileContents.replace(cssImportRegExp, function (fullMatch, urlStart, importFileName, urlEnd, mediaTypes) {
//Only process media type "all" or empty media type rules.
if (mediaTypes && ((mediaTypes.replace(/^\s\s*/, '').replace(/\s\s*$/, '')) !== "all")) {
skippedList.push(fileName);
return fullMatch;
}
importFileName = cleanCssUrlQuotes(importFileName);
//Ignore the file import if it is part of an ignore list.
if (cssImportIgnore) {
//Strings may be comma-separated lists.
if (typeof cssImportIgnore === "string" && cssImportIgnore.indexOf(importFileName + ",") !== -1) {
return fullMatch;
}
//Regex are checked for simple matches.
if (cssImportIgnore instanceof RegExp && cssImportIgnore.test(importFileName)) {
return fullMatch;
}
//Arrays requiring checking individual elements.
if (Array.isArray(cssImportIgnore)) {
cssImportIgnore.forEach(function(expr){
if (typeof expr === "string" && expr === importFileName) {
return fullMatch;
}
if (expr instanceof RegExp && expr.test(importFileName)) {
return fullMatch;
}
})
}
}
//Make sure we have a unix path for the rest of the operation.
importFileName = importFileName.replace(lang.backSlashRegExp, "/");
try {
//if a relative path, then tack on the filePath.
//If it is not a relative path, then the readFile below will fail,
//and we will just skip that import.
var fullImportFileName = importFileName.charAt(0) === "/" ? importFileName : filePath + importFileName,
importContents = file.readFile(fullImportFileName),
importEndIndex, importPath, flat;
//Skip the file if it has already been included.
if (included[fullImportFileName]) {
return '';
}
included[fullImportFileName] = true;
//Make sure to flatten any nested imports.
flat = flattenCss(fullImportFileName, importContents, cssImportIgnore, cssPrefix, included);
importContents = flat.fileContents;
if (flat.importList.length) {
importList.push.apply(importList, flat.importList);
}
if (flat.skippedList.length) {
skippedList.push.apply(skippedList, flat.skippedList);
}
//Make the full import path
importEndIndex = importFileName.lastIndexOf("/");
//Make a file path based on the last slash.
//If no slash, so must be just a file name. Use empty string then.
importPath = (importEndIndex !== -1) ? importFileName.substring(0, importEndIndex + 1) : "";
//fix url() on relative import (#5)
importPath = importPath.replace(/^\.\//, '');
//Modify URL paths to match the path represented by this file.
importContents = fixCssUrlPaths(importFileName, importPath, importContents, cssPrefix);
importList.push(fullImportFileName);
return importContents;
} catch (e) {
logger.warn(fileName + "\n Cannot inline css import, skipping: " + importFileName);
return fullMatch;
}
});
if (cssPrefix && topLevel) {
//Modify URL paths to match the path represented by this file.
fileContents = fixCssUrlPaths(fileName, '', fileContents, cssPrefix);
}
return {
importList : importList,
skippedList: skippedList,
fileContents : fileContents
};
}
optimize = {
/**
* Optimizes a file that contains JavaScript content. Optionally collects
* plugin resources mentioned in a file, and then passes the content
* through an minifier if one is specified via config.optimize.
*
* @param {String} fileName the name of the file to optimize
* @param {String} fileContents the contents to optimize. If this is
* a null value, then fileName will be used to read the fileContents.
* @param {String} outFileName the name of the file to use for the
* saved optimized content.
* @param {Object} config the build config object.
* @param {Array} [pluginCollector] storage for any plugin resources
* found.
*/
jsFile: function (fileName, fileContents, outFileName, config, pluginCollector) {
if (!fileContents) {
fileContents = file.readFile(fileName);
}
fileContents = optimize.js(fileName, fileContents, outFileName, config, pluginCollector);
file.saveUtf8File(outFileName, fileContents);
},
/**
* Optimizes a file that contains JavaScript content. Optionally collects
* plugin resources mentioned in a file, and then passes the content
* through an minifier if one is specified via config.optimize.
*
* @param {String} fileName the name of the file that matches the
* fileContents.
* @param {String} fileContents the string of JS to optimize.
* @param {Object} [config] the build config object.
* @param {Array} [pluginCollector] storage for any plugin resources
* found.
*/
js: function (fileName, fileContents, outFileName, config, pluginCollector) {
var optFunc, optConfig,
parts = (String(config.optimize)).split('.'),
optimizerName = parts[0],
keepLines = parts[1] === 'keepLines',
licenseContents = '';
config = config || {};
//Apply pragmas/namespace renaming
fileContents = pragma.process(fileName, fileContents, config, 'OnSave', pluginCollector);
//Optimize the JS files if asked.
if (optimizerName && optimizerName !== 'none') {
optFunc = envOptimize[optimizerName] || optimize.optimizers[optimizerName];
if (!optFunc) {
throw new Error('optimizer with name of "' +
optimizerName +
'" not found for this environment');
}
optConfig = config[optimizerName] || {};
if (config.generateSourceMaps) {
optConfig.generateSourceMaps = !!config.generateSourceMaps;
optConfig._buildSourceMap = config._buildSourceMap;
}
try {
if (config.preserveLicenseComments) {
//Pull out any license comments for prepending after optimization.
try {
licenseContents = parse.getLicenseComments(fileName, fileContents);
} catch (e) {
throw new Error('Cannot parse file: ' + fileName + ' for comments. Skipping it. Error is:\n' + e.toString());
}
}
fileContents = licenseContents + optFunc(fileName,
fileContents,
outFileName,
keepLines,
optConfig);
if (optConfig._buildSourceMap && optConfig._buildSourceMap !== config._buildSourceMap) {
config._buildSourceMap = optConfig._buildSourceMap;
}
} catch (e) {
if (config.throwWhen && config.throwWhen.optimize) {
throw e;
} else {
logger.error(e);
}
}
} else {
if (config._buildSourceMap) {
config._buildSourceMap = null;
}
}
return fileContents;
},
/**
* Optimizes one CSS file, inlining @import calls, stripping comments, and
* optionally removes line returns.
* @param {String} fileName the path to the CSS file to optimize
* @param {String} outFileName the path to save the optimized file.
* @param {Object} config the config object with the optimizeCss and
* cssImportIgnore options.
*/
cssFile: function (fileName, outFileName, config) {
//Read in the file. Make sure we have a JS string.
var originalFileContents = file.readFile(fileName),
flat = flattenCss(fileName, originalFileContents, config.cssImportIgnore, config.cssPrefix, {}, true),
//Do not use the flattened CSS if there was one that was skipped.
fileContents = flat.skippedList.length ? originalFileContents : flat.fileContents,
startIndex, endIndex, buildText, comment;
if (flat.skippedList.length) {
logger.warn('Cannot inline @imports for ' + fileName +
',\nthe following files had media queries in them:\n' +
flat.skippedList.join('\n'));
}
//Do comment removal.
try {
if (config.optimizeCss.indexOf(".keepComments") === -1) {
startIndex = 0;
//Get rid of comments.
while ((startIndex = fileContents.indexOf("/*", startIndex)) !== -1) {
endIndex = fileContents.indexOf("*/", startIndex + 2);
if (endIndex === -1) {
throw "Improper comment in CSS file: " + fileName;
}
comment = fileContents.substring(startIndex, endIndex);
if (config.preserveLicenseComments &&
(comment.indexOf('license') !== -1 ||
comment.indexOf('opyright') !== -1 ||
comment.indexOf('(c)') !== -1)) {
//Keep the comment, just increment the startIndex
startIndex = endIndex;
} else {
fileContents = fileContents.substring(0, startIndex) + fileContents.substring(endIndex + 2, fileContents.length);
startIndex = 0;
}
}
}
//Get rid of newlines.
if (config.optimizeCss.indexOf(".keepLines") === -1) {
fileContents = fileContents.replace(/[\r\n]/g, " ");
fileContents = fileContents.replace(/\s+/g, " ");
fileContents = fileContents.replace(/\{\s/g, "{");
fileContents = fileContents.replace(/\s\}/g, "}");
} else {
//Remove multiple empty lines.
fileContents = fileContents.replace(/(\r\n)+/g, "\r\n");
fileContents = fileContents.replace(/(\n)+/g, "\n");
}
//Remove unnecessary whitespace
if (config.optimizeCss.indexOf(".keepWhitespace") === -1) {
//Remove leading and trailing whitespace from lines
fileContents = fileContents.replace(/^[ \t]+/gm, "");
fileContents = fileContents.replace(/[ \t]+$/gm, "");
//Remove whitespace after semicolon, colon, curly brackets and commas
fileContents = fileContents.replace(/(;|:|\{|}|,)[ \t]+/g, "$1");
//Remove whitespace before opening curly brackets
fileContents = fileContents.replace(/[ \t]+(\{)/g, "$1");
//Truncate double whitespace
fileContents = fileContents.replace(/([ \t])+/g, "$1");
//Remove empty lines
fileContents = fileContents.replace(/^[ \t]*[\r\n]/gm,'');
}
} catch (e) {
fileContents = originalFileContents;
logger.error("Could not optimized CSS file: " + fileName + ", error: " + e);
}
file.saveUtf8File(outFileName, fileContents);
//text output to stdout and/or written to build.txt file
buildText = "\n"+ outFileName.replace(config.dir, "") +"\n----------------\n";
flat.importList.push(fileName);
buildText += flat.importList.map(function(path){
return path.replace(config.dir, "");
}).join("\n");
return {
importList: flat.importList,
buildText: buildText +"\n"
};
},
/**
* Optimizes CSS files, inlining @import calls, stripping comments, and
* optionally removes line returns.
* @param {String} startDir the path to the top level directory
* @param {Object} config the config object with the optimizeCss and
* cssImportIgnore options.
*/
css: function (startDir, config) {
var buildText = "",
importList = [],
shouldRemove = config.dir && config.removeCombined,
i, fileName, result, fileList;
if (config.optimizeCss.indexOf("standard") !== -1) {
fileList = file.getFilteredFileList(startDir, /\.css$/, true);
if (fileList) {
for (i = 0; i < fileList.length; i++) {
fileName = fileList[i];
logger.trace("Optimizing (" + config.optimizeCss + ") CSS file: " + fileName);
result = optimize.cssFile(fileName, fileName, config);
buildText += result.buildText;
if (shouldRemove) {
result.importList.pop();
importList = importList.concat(result.importList);
}
}
}
if (shouldRemove) {
importList.forEach(function (path) {
if (file.exists(path)) {
file.deleteFile(path);
}
});
}
}
return buildText;
},
optimizers: {
uglify: function (fileName, fileContents, outFileName, keepLines, config) {
var parser = uglify.parser,
processor = uglify.uglify,
ast, errMessage, errMatch;
config = config || {};
logger.trace("Uglifying file: " + fileName);
try {
ast = parser.parse(fileContents, config.strict_semicolons);
if (config.no_mangle !== true) {
ast = processor.ast_mangle(ast, config);
}
ast = processor.ast_squeeze(ast, config);
fileContents = processor.gen_code(ast, config);
if (config.max_line_length) {
fileContents = processor.split_lines(fileContents, config.max_line_length);
}
//Add trailing semicolon to match uglifyjs command line version
fileContents += ';';
} catch (e) {
errMessage = e.toString();
errMatch = /\nError(\r)?\n/.exec(errMessage);
if (errMatch) {
errMessage = errMessage.substring(0, errMatch.index);
}
throw new Error('Cannot uglify file: ' + fileName + '. Skipping it. Error is:\n' + errMessage);
}
return fileContents;
},
uglify2: function (fileName, fileContents, outFileName, keepLines, config) {
var result, existingMap, resultMap, finalMap, sourceIndex,
uconfig = {},
existingMapPath = outFileName + '.map',
baseName = fileName && fileName.split('/').pop();
config = config || {};
lang.mixin(uconfig, config, true);
uconfig.fromString = true;
if (config.generateSourceMaps && (outFileName || config._buildSourceMap)) {
uconfig.outSourceMap = baseName;
if (config._buildSourceMap) {
existingMap = JSON.parse(config._buildSourceMap);
uconfig.inSourceMap = existingMap;
} else if (file.exists(existingMapPath)) {
uconfig.inSourceMap = existingMapPath;
existingMap = JSON.parse(file.readFile(existingMapPath));
}
}
logger.trace("Uglify2 file: " + fileName);
try {
//var tempContents = fileContents.replace(/\/\/\# sourceMappingURL=.*$/, '');
result = uglify2.minify(fileContents, uconfig, baseName + '.src.js');
if (uconfig.outSourceMap && result.map) {
resultMap = result.map;
if (existingMap) {
resultMap = JSON.parse(resultMap);
finalMap = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(resultMap));
finalMap.applySourceMap(new SourceMapConsumer(existingMap));
resultMap = finalMap.toString();
} else if (!config._buildSourceMap) {
file.saveFile(outFileName + '.src.js', fileContents);
}
fileContents = result.code;
if (config._buildSourceMap) {
config._buildSourceMap = resultMap;
} else {
file.saveFile(outFileName + '.map', resultMap);
fileContents += "\n//# sourceMappingURL=" + baseName + ".map";
}
} else {
fileContents = result.code;
}
} catch (e) {
throw new Error('Cannot uglify2 file: ' + fileName + '. Skipping it. Error is:\n' + e.toString());
}
return fileContents;
}
}
};
return optimize;
});