/
uglify.js
282 lines (228 loc) · 8.36 KB
/
uglify.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
/*
* grunt-contrib-uglify
* https://gruntjs.com/
*
* Copyright (c) 2016 "Cowboy" Ben Alman, contributors
* Licensed under the MIT license.
*/
'use strict';
// External libs.
var path = require('path');
var UglifyJS = require('uglify-js');
var assign = require('lodash.assign');
var uriPath = require('uri-path');
var getOutputOptions;
exports.init = function(grunt) {
var exports = {};
// Minify with UglifyJS.
// From https://github.com/mishoo/UglifyJS2
// API docs at http://lisperator.net/uglifyjs/
exports.minify = function(files, dest, options) {
options = options || {};
grunt.verbose.write('Minifying with UglifyJS...');
var topLevel = null;
var totalCode = '';
var sourcesContent = {};
var outputOptions = getOutputOptions(options, dest);
var output = UglifyJS.OutputStream(outputOptions);
// Grab and parse all source files
files.forEach(function(file) {
var code = grunt.file.read(file);
totalCode += code;
// The src file name must be relative to the source map for things to work
var basename = path.basename(file);
var fileDir = path.dirname(file);
var sourceMapDir = path.dirname(options.generatedSourceMapName);
var relativePath = path.relative(sourceMapDir, fileDir);
var pathPrefix = relativePath ? relativePath + path.sep : '';
var bare_returns = options.bare_returns || undefined;
// Convert paths to use forward slashes for sourcemap use in the browser
file = uriPath(pathPrefix + basename);
sourcesContent[file] = code;
topLevel = UglifyJS.parse(code, {
filename: file,
toplevel: topLevel,
expression: options.expression,
bare_returns: bare_returns
});
});
// Wrap code in a common js wrapper.
if (options.wrap) {
topLevel = topLevel.wrap_commonjs(options.wrap, options.exportAll);
}
// Wrap code in closure with configurable arguments/parameters list.
if (options.enclose) {
var argParamList = Object.keys(options.enclose).map(function(key) {
return key + ':' + options.enclose[key];
});
topLevel = topLevel.wrap_enclose(argParamList);
}
var topLevelCache = null;
if (options.nameCache) {
topLevelCache = UglifyJS.readNameCache(options.nameCache, 'vars');
}
// Need to call this before we mangle or compress,
// and call after any compression or ast altering
if (options.expression === false) {
topLevel.figure_out_scope({ screw_ie8: options.screwIE8, cache: topLevelCache });
}
if (options.compress !== false) {
if (options.compress === true) {
options.compress = {};
}
if (options.compress.warnings !== true) {
options.compress.warnings = false;
}
if (options.screwIE8) {
options.compress.screw_ie8 = true;
}
var compressor = UglifyJS.Compressor(options.compress);
topLevel = topLevel.transform(compressor);
// Need to figure out scope again after source being altered
if (options.expression === false) {
topLevel.figure_out_scope({screw_ie8: options.screwIE8, cache: topLevelCache});
}
}
var mangleExclusions = { vars: [], props: [] };
if (options.reserveDOMProperties) {
mangleExclusions = UglifyJS.readDefaultReservedFile();
}
if (options.exceptionsFiles) {
try {
options.exceptionsFiles.forEach(function(filename) {
mangleExclusions = UglifyJS.readReservedFile(filename, mangleExclusions);
});
} catch (ex) {
grunt.warn(ex);
}
}
var cache = null;
if (options.nameCache) {
cache = UglifyJS.readNameCache(options.nameCache, 'props');
}
if (typeof(options.mangleProperties) !== 'undefined' && options.mangleProperties !== false) {
// if options.mangleProperties is a boolean (true) convert it into an object
if (typeof options.mangleProperties !== 'object') {
options.mangleProperties = {};
}
options.mangleProperties.reserved = mangleExclusions ? mangleExclusions.props : null;
options.mangleProperties.cache = cache;
topLevel = UglifyJS.mangle_properties(topLevel, options.mangleProperties);
if (options.nameCache) {
UglifyJS.writeNameCache(options.nameCache, 'props', cache);
}
// Need to figure out scope again since topLevel has been altered
if (options.expression === false) {
topLevel.figure_out_scope({screw_ie8: options.screwIE8, cache: topLevelCache});
}
}
if (options.mangle !== false) {
if (options.mangle === true) {
options.mangle = {};
}
if (options.screwIE8) {
options.mangle.screw_ie8 = true;
}
// disabled due to:
// 1) preserve stable name mangling
// 2) it increases gzipped file size, see https://github.com/mishoo/UglifyJS2#mangler-options
// // compute_char_frequency optimizes names for compression
// topLevel.compute_char_frequency(options.mangle);
// if options.mangle is a boolean (true) convert it into an object
if (typeof options.mangle !== 'object') {
options.mangle = {};
}
options.mangle.cache = topLevelCache;
options.mangle.except = options.mangle.except ? options.mangle.except : [];
if (mangleExclusions.vars) {
mangleExclusions.vars.forEach(function(name) {
UglifyJS.push_uniq(options.mangle.except, name);
});
}
// Requires previous call to figure_out_scope
// and should always be called after compressor transform
topLevel.mangle_names(options.mangle);
UglifyJS.writeNameCache(options.nameCache, 'vars', options.mangle.cache);
}
if (options.sourceMap && options.sourceMapIncludeSources) {
for (var file in sourcesContent) {
if (sourcesContent.hasOwnProperty(file)) {
outputOptions.source_map.get().setSourceContent(file, sourcesContent[file]);
}
}
}
// Print the ast to OutputStream
topLevel.print(output);
var min = output.get();
// Add the source map reference to the end of the file
if (options.sourceMap) {
// Set all paths to forward slashes for use in the browser
var sourceMappingURL;
sourceMappingURL = options.destToSourceMap.match(/^http[s]?\:\/\//) === null ? uriPath(options.destToSourceMap) : options.destToSourceMap;
min += '\n//# sourceMappingURL=' + sourceMappingURL;
}
var result = {
max: totalCode,
min: min,
sourceMap: outputOptions.source_map
};
grunt.verbose.ok();
return result;
};
getOutputOptions = function(options, dest) {
var outputOptions = {
beautify: false,
source_map: null
};
if (options.banner && options.sourceMap) {
outputOptions.preamble = options.banner;
}
if (options.screwIE8) {
outputOptions.screw_ie8 = true;
}
if (options.sourceMap) {
var destBasename = path.basename(dest);
var sourceMapIn;
if (options.sourceMapIn) {
sourceMapIn = grunt.file.readJSON(options.sourceMapIn);
}
outputOptions.source_map = UglifyJS.SourceMap({
file: destBasename,
root: options.sourceMapRoot,
orig: sourceMapIn
});
if (options.sourceMapIncludeSources && sourceMapIn && sourceMapIn.sourcesContent) {
sourceMapIn.sourcesContent.forEach(function(content, idx) {
outputOptions.source_map.get().setSourceContent(sourceMapIn.sources[idx], content);
});
}
if (options.sourceMapIn) {
outputOptions.source_map.get()._file = destBasename;
}
}
if (typeof options.indentLevel !== 'undefined') {
outputOptions.indent_level = options.indentLevel;
}
if (typeof options.maxLineLen !== 'undefined') {
outputOptions.max_line_len = options.maxLineLen;
}
if (typeof options.ASCIIOnly !== 'undefined') {
outputOptions.ascii_only = options.ASCIIOnly;
}
if (typeof options.quoteStyle !== 'undefined') {
outputOptions.quote_style = options.quoteStyle;
}
if (typeof options.preserveComments !== 'undefined') {
outputOptions.comments = options.preserveComments;
}
if (options.beautify) {
if (typeof options.beautify === 'object') {
assign(outputOptions, options.beautify);
} else {
outputOptions.beautify = true;
}
}
return outputOptions;
};
return exports;
};