forked from requirejs/requirejs
/
optimize.js
255 lines (219 loc) · 11 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
/**
* @license Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
/*jslint plusplus: false, nomen: false, regexp: false */
/*global define: false */
"use strict";
define([ 'lang', 'logger', 'env!env/optimize', 'env!env/file', 'uglifyjs/index'],
function (lang, logger, envOptimize, file, uglify) {
var optimize,
cssImportRegExp = /\@import\s+(url\()?\s*([^);]+)\s*(\))?([\w, ]*)(;)?/g,
cssUrlRegExp = /\url\(\s*([^\)]+)\s*\)?/g;
/**
* 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;
}
/**
* Inlines nested stylesheets that have @import calls in them.
* @param {String} fileName
* @param {String} fileContents
* @param {String} [cssImportIgnore]
*/
function flattenCss(fileName, fileContents, cssImportIgnore) {
//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) : "";
//Make sure we have a delimited ignore list to make matching faster
if (cssImportIgnore && cssImportIgnore.charAt(cssImportIgnore.length - 1) !== ",") {
cssImportIgnore += ",";
}
return 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")) {
return fullMatch;
}
importFileName = cleanCssUrlQuotes(importFileName);
//Ignore the file import if it is part of an ignore list.
if (cssImportIgnore && cssImportIgnore.indexOf(importFileName + ",") !== -1) {
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), i,
importEndIndex, importPath, fixedUrlMatch, colonIndex, parts;
//Make sure to flatten any nested imports.
importContents = flattenCss(fullImportFileName, importContents);
//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) : "";
//Modify URL paths to match the path represented by this file.
importContents = importContents.replace(cssUrlRegExp, function (fullMatch, urlMatch) {
fixedUrlMatch = cleanCssUrlQuotes(urlMatch);
fixedUrlMatch = fixedUrlMatch.replace(lang.backSlashRegExp, "/");
//Only do the work for relative URLs. Skip things that start with / or have
//a protocol.
colonIndex = fixedUrlMatch.indexOf(":");
if (fixedUrlMatch.charAt(0) !== "/" && (colonIndex === -1 || colonIndex > fixedUrlMatch.indexOf("/"))) {
//It is a relative URL, tack on the path prefix
urlMatch = importPath + fixedUrlMatch;
} else {
logger.trace(importFileName + "\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("/") + ")";
});
return importContents;
} catch (e) {
logger.trace(fileName + "\n Cannot inline css import, skipping: " + importFileName);
return fullMatch;
}
});
}
optimize = {
/**
* Optimizes a file that contains JavaScript content. It will inline
* text plugin files and run it through Google Closure Compiler
* minification, if the config options specify it.
*
* @param {String} fileName the name of the file to optimize
* @param {String} outFileName the name of the file to use for the
* saved optimized content.
* @param {Object} config the build config object.
*/
jsFile: function (fileName, outFileName, config) {
var parts = (config.optimize + "").split('.'),
optimizerName = parts[0],
keepLines = parts[1] === 'keepLines',
fileContents, optFunc;
fileContents = file.readFile(fileName);
//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');
}
fileContents = optFunc(fileName, fileContents, keepLines,
config[optimizerName]);
}
file.saveUtf8File(outFileName, 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),
fileContents = flattenCss(fileName, originalFileContents, config.cssImportIgnore),
startIndex, endIndex;
//Do comment removal.
try {
startIndex = -1;
//Get rid of comments.
while ((startIndex = fileContents.indexOf("/*")) !== -1) {
endIndex = fileContents.indexOf("*/", startIndex + 2);
if (endIndex === -1) {
throw "Improper comment in CSS file: " + fileName;
}
fileContents = fileContents.substring(0, startIndex) + fileContents.substring(endIndex + 2, fileContents.length);
}
//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");
}
} catch (e) {
fileContents = originalFileContents;
logger.error("Could not optimized CSS file: " + fileName + ", error: " + e);
}
file.saveUtf8File(outFileName, fileContents);
},
/**
* 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) {
if (config.optimizeCss.indexOf("standard") !== -1) {
var i, fileName,
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);
optimize.cssFile(fileName, fileName, config);
}
}
}
},
optimizers: {
uglify: function (fileName, fileContents, keepLines, config) {
var parser = uglify.parser,
processor = uglify.uglify,
ast, genCodeConfig;
config = config || {};
genCodeConfig = config.gen_codeOptions || keepLines;
logger.trace("Uglifying file: " + fileName);
try {
ast = parser.parse(fileContents, config.strict_semicolons);
ast = processor.ast_mangle(ast, config.do_toplevel);
ast = processor.ast_squeeze(ast, config.ast_squeezeOptions);
fileContents = processor.gen_code(ast, genCodeConfig);
} catch (e) {
logger.error('Cannot uglify file: ' + fileName + '. Skipping it. Error is:\n' + e.toString());
}
return fileContents;
}
}
};
return optimize;
});