uglify.js
4.4 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
/*
* grunt-contrib-uglify
* https://gruntjs.com/
*
* Copyright (c) 2013 "Cowboy" Ben Alman, contributors
* Licensed under the MIT license.
*/
'use strict';
// External libs.
var UglifyJS = require('uglify-js');
var fs = require('fs');
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 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);
if (typeof options.sourceMapPrefix !== 'undefined') {
file = file.replace(/^\/+/, "").split(/\/+/).slice(options.sourceMapPrefix).join("/");
}
totalCode += code;
topLevel = UglifyJS.parse(code, {
filename: file,
toplevel: topLevel
});
});
// 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 = grunt.util._.map(options.enclose, function(val, key) {
return key + ':' + val;
});
topLevel = topLevel.wrap_enclose(argParamList);
}
// Need to call this before we mangle or compress,
// and call after any compression or ast altering
topLevel.figure_out_scope();
if (options.compress !== false) {
if (options.compress.warnings !== true) {
options.compress.warnings = false;
}
var compressor = UglifyJS.Compressor(options.compress);
topLevel = topLevel.transform(compressor);
// Need to figure out scope again after source being altered
topLevel.figure_out_scope();
}
if (options.mangle !== false) {
// 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);
// Requires previous call to figure_out_scope
// and should always be called after compressor transform
topLevel.mangle_names(options.mangle);
}
// Print the ast to OutputStream
topLevel.print(output);
var min = output.get();
if (options.sourceMappingURL || options.sourceMap) {
min += "\n//# sourceMappingURL=" + (options.sourceMappingURL || options.sourceMap);
}
var result = {
max: totalCode,
min: min,
sourceMap: outputOptions.source_map
};
grunt.verbose.ok();
return result;
};
var getOutputOptions = function(options, dest) {
var outputOptions = {
beautify: false,
source_map: null
};
if (options.preserveComments) {
if (options.preserveComments === 'all' || options.preserveComments === true) {
// preserve all the comments we can
outputOptions.comments = true;
} else if (options.preserveComments === 'some') {
// preserve comments with directives or that start with a bang (!)
outputOptions.comments = /^!|@preserve|@license|@cc_on/i;
} else if (grunt.util._.isFunction(options.preserveComments)) {
// support custom functions passed in
outputOptions.comments = options.preserveComments;
}
}
if (options.banner && options.sourceMap) {
outputOptions.preamble = options.banner;
}
if (options.beautify) {
if (grunt.util._.isObject(options.beautify)) {
// beautify options sent as an object are merged
// with outputOptions and passed to the OutputStream
grunt.util._.extend(outputOptions, options.beautify);
} else {
outputOptions.beautify = true;
}
}
if (options.sourceMap) {
var sourceMapIn;
if (options.sourceMapIn) {
sourceMapIn = grunt.file.readJSON(options.sourceMapIn);
}
outputOptions.source_map = UglifyJS.SourceMap({
file: dest,
root: options.sourceMapRoot,
orig: sourceMapIn
});
}
return outputOptions;
};
return exports;
};