-
Notifications
You must be signed in to change notification settings - Fork 348
/
uglify.js
160 lines (141 loc) · 4.4 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
/*
* grunt-contrib-uglify
* https://gruntjs.com/
*
* Copyright (c) 2017 "Cowboy" Ben Alman, contributors
* Licensed under the MIT license.
*/
'use strict';
// External libs.
var path = require('path');
var UglifyJS = require('uglify-js');
var uriPath = require('uri-path');
var domprops = require('uglify-js/tools/domprops');
// Converts \r\n to \n
function normalizeLf(string) {
return string.replace(/\r\n/g, '\n');
}
function toCache(cache, key) {
if (cache[key]) {
cache[key].props = UglifyJS.Dictionary.fromObject(cache[key].props);
} else {
cache[key] = {
cname: -1,
props: new UglifyJS.Dictionary()
};
}
return cache[key];
}
exports.init = function(grunt) {
var exports = {};
// Minify with UglifyJS.
// From https://github.com/mishoo/UglifyJS2
exports.minify = function(files, dest, options) {
options = options || {};
grunt.verbose.write('Minifying with UglifyJS...');
var totalCode = '';
var minifyOptions = {
compress: options.compress,
ie8: options.ie8,
keep_fnames: options.keep_fnames,
mangle: options.mangle,
output: options.output || {},
parse: options.parse || {},
sourceMap: options.sourceMap,
toplevel: options.toplevel,
wrap: options.wrap,
webkit: options.webkit
};
if (options.banner) {
minifyOptions.output.preamble = normalizeLf(options.banner);
}
if (options.beautify) {
minifyOptions.output.beautify = true;
for (var key in options.beautify) {
minifyOptions.output[key] = options.beautify[key];
}
}
var cache;
if (options.nameCache) {
try {
cache = JSON.parse(grunt.file.read(options.nameCache));
} catch (ex) {
cache = {};
}
}
if (minifyOptions.mangle) {
if (typeof minifyOptions.mangle !== 'object') {
minifyOptions.mangle = {};
}
if (cache) {
minifyOptions.mangle.cache = toCache(cache, 'vars');
}
if (!Array.isArray(minifyOptions.mangle.reserved)) {
minifyOptions.mangle.reserved = [];
}
if (minifyOptions.mangle.properties) {
if (typeof minifyOptions.mangle.properties !== 'object') {
minifyOptions.mangle.properties = {};
}
if (cache) {
minifyOptions.mangle.properties.cache = toCache(cache, 'props');
}
if (!Array.isArray(minifyOptions.mangle.properties.reserved)) {
minifyOptions.mangle.properties.reserved = [];
}
if (options.reserveDOMProperties) {
domprops.forEach(function(name) {
UglifyJS.push_uniq(minifyOptions.mangle.properties.reserved, name);
});
}
}
if (options.exceptionsFiles) {
options.exceptionsFiles.forEach(function(file) {
try {
var obj = JSON.parse(grunt.file.read(file));
if (minifyOptions.mangle && obj.vars) {
obj.vars.forEach(function(name) {
UglifyJS.push_uniq(minifyOptions.mangle.reserved, name);
});
}
if (minifyOptions.mangle.properties && obj.props) {
obj.props.forEach(function(name) {
UglifyJS.push_uniq(minifyOptions.mangle.properties.reserved, name);
});
}
} catch (ex) {
grunt.warn(ex);
}
});
}
}
var result = UglifyJS.minify(files.reduce(function(o, 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 : '';
// Convert paths to use forward slashes for sourcemap use in the browser
o[uriPath(pathPrefix + basename)] = code;
return o;
}, {}), minifyOptions);
if (result.error) {
throw result.error;
}
if (options.nameCache) {
grunt.file.write(options.nameCache, JSON.stringify(cache, function(key, value) {
return value instanceof UglifyJS.Dictionary ? value.toObject() : value;
}));
}
grunt.verbose.ok();
return {
max: totalCode,
min: result.code,
sourceMap: result.map
};
};
return exports;
};