Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80743 views
1
var path = require("path");
2
var fs = require("fs");
3
var vm = require("vm");
4
5
var UglifyJS = vm.createContext({
6
console : console,
7
process : process,
8
Buffer : Buffer,
9
MOZ_SourceMap : require("source-map")
10
});
11
12
function load_global(file) {
13
file = path.resolve(path.dirname(module.filename), file);
14
try {
15
var code = fs.readFileSync(file, "utf8");
16
return vm.runInContext(code, UglifyJS, file);
17
} catch(ex) {
18
// XXX: in case of a syntax error, the message is kinda
19
// useless. (no location information).
20
console.log("ERROR in file: " + file + " / " + ex);
21
process.exit(1);
22
}
23
};
24
25
var FILES = exports.FILES = [
26
"../lib/utils.js",
27
"../lib/ast.js",
28
"../lib/parse.js",
29
"../lib/transform.js",
30
"../lib/scope.js",
31
"../lib/output.js",
32
"../lib/compress.js",
33
"../lib/sourcemap.js",
34
"../lib/mozilla-ast.js",
35
"../lib/propmangle.js"
36
].map(function(file){
37
return fs.realpathSync(path.join(path.dirname(__filename), file));
38
});
39
40
FILES.forEach(load_global);
41
42
UglifyJS.AST_Node.warn_function = function(txt) {
43
console.error("WARN: %s", txt);
44
};
45
46
// XXX: perhaps we shouldn't export everything but heck, I'm lazy.
47
for (var i in UglifyJS) {
48
if (UglifyJS.hasOwnProperty(i)) {
49
exports[i] = UglifyJS[i];
50
}
51
}
52
53
exports.minify = function(files, options) {
54
options = UglifyJS.defaults(options, {
55
spidermonkey : false,
56
outSourceMap : null,
57
sourceRoot : null,
58
inSourceMap : null,
59
fromString : false,
60
warnings : false,
61
mangle : {},
62
output : null,
63
compress : {}
64
});
65
UglifyJS.base54.reset();
66
67
// 1. parse
68
var toplevel = null,
69
sourcesContent = {};
70
71
if (options.spidermonkey) {
72
toplevel = UglifyJS.AST_Node.from_mozilla_ast(files);
73
} else {
74
if (typeof files == "string")
75
files = [ files ];
76
files.forEach(function(file){
77
var code = options.fromString
78
? file
79
: fs.readFileSync(file, "utf8");
80
sourcesContent[file] = code;
81
toplevel = UglifyJS.parse(code, {
82
filename: options.fromString ? "?" : file,
83
toplevel: toplevel
84
});
85
});
86
}
87
88
// 2. compress
89
if (options.compress) {
90
var compress = { warnings: options.warnings };
91
UglifyJS.merge(compress, options.compress);
92
toplevel.figure_out_scope();
93
var sq = UglifyJS.Compressor(compress);
94
toplevel = toplevel.transform(sq);
95
}
96
97
// 3. mangle
98
if (options.mangle) {
99
toplevel.figure_out_scope(options.mangle);
100
toplevel.compute_char_frequency(options.mangle);
101
toplevel.mangle_names(options.mangle);
102
}
103
104
// 4. output
105
var inMap = options.inSourceMap;
106
var output = {};
107
if (typeof options.inSourceMap == "string") {
108
inMap = fs.readFileSync(options.inSourceMap, "utf8");
109
}
110
if (options.outSourceMap) {
111
output.source_map = UglifyJS.SourceMap({
112
file: options.outSourceMap,
113
orig: inMap,
114
root: options.sourceRoot
115
});
116
if (options.sourceMapIncludeSources) {
117
for (var file in sourcesContent) {
118
if (sourcesContent.hasOwnProperty(file)) {
119
output.source_map.get().setSourceContent(file, sourcesContent[file]);
120
}
121
}
122
}
123
124
}
125
if (options.output) {
126
UglifyJS.merge(output, options.output);
127
}
128
var stream = UglifyJS.OutputStream(output);
129
toplevel.print(stream);
130
131
if(options.outSourceMap){
132
stream += "\n//# sourceMappingURL=" + options.outSourceMap;
133
}
134
135
var source_map = output.source_map;
136
if (source_map) {
137
source_map = source_map + "";
138
}
139
140
return {
141
code : stream + "",
142
map : source_map
143
};
144
};
145
146
// exports.describe_ast = function() {
147
// function doitem(ctor) {
148
// var sub = {};
149
// ctor.SUBCLASSES.forEach(function(ctor){
150
// sub[ctor.TYPE] = doitem(ctor);
151
// });
152
// var ret = {};
153
// if (ctor.SELF_PROPS.length > 0) ret.props = ctor.SELF_PROPS;
154
// if (ctor.SUBCLASSES.length > 0) ret.sub = sub;
155
// return ret;
156
// }
157
// return doitem(UglifyJS.AST_Node).sub;
158
// }
159
160
exports.describe_ast = function() {
161
var out = UglifyJS.OutputStream({ beautify: true });
162
function doitem(ctor) {
163
out.print("AST_" + ctor.TYPE);
164
var props = ctor.SELF_PROPS.filter(function(prop){
165
return !/^\$/.test(prop);
166
});
167
if (props.length > 0) {
168
out.space();
169
out.with_parens(function(){
170
props.forEach(function(prop, i){
171
if (i) out.space();
172
out.print(prop);
173
});
174
});
175
}
176
if (ctor.documentation) {
177
out.space();
178
out.print_string(ctor.documentation);
179
}
180
if (ctor.SUBCLASSES.length > 0) {
181
out.space();
182
out.with_block(function(){
183
ctor.SUBCLASSES.forEach(function(ctor, i){
184
out.indent();
185
doitem(ctor);
186
out.newline();
187
});
188
});
189
}
190
};
191
doitem(UglifyJS.AST_Node);
192
return out + "";
193
};
194
195
function readReservedFile(filename, reserved) {
196
if (!reserved) {
197
reserved = { vars: [], props: [] };
198
}
199
var data = fs.readFileSync(filename, "utf8");
200
data = JSON.parse(data);
201
if (data.vars) {
202
data.vars.forEach(function(name){
203
UglifyJS.push_uniq(reserved.vars, name);
204
});
205
}
206
if (data.props) {
207
data.props.forEach(function(name){
208
UglifyJS.push_uniq(reserved.props, name);
209
});
210
}
211
return reserved;
212
}
213
214
exports.readReservedFile = readReservedFile;
215
216
exports.readDefaultReservedFile = function(reserved) {
217
return readReservedFile(path.join(__dirname, "domprops.json"), reserved);
218
};
219
220
exports.readNameCache = function(filename, key) {
221
var cache = null;
222
if (filename) {
223
try {
224
var cache = fs.readFileSync(filename, "utf8");
225
cache = JSON.parse(cache)[key];
226
if (!cache) throw "init";
227
cache.props = UglifyJS.Dictionary.fromObject(cache.props);
228
} catch(ex) {
229
cache = {
230
cname: -1,
231
props: new UglifyJS.Dictionary()
232
};
233
}
234
}
235
return cache;
236
};
237
238
exports.writeNameCache = function(filename, key, cache) {
239
if (filename) {
240
var data;
241
try {
242
data = fs.readFileSync(filename, "utf8");
243
data = JSON.parse(data);
244
} catch(ex) {
245
data = {};
246
}
247
data[key] = {
248
cname: cache.cname,
249
props: cache.props.toObject()
250
};
251
fs.writeFileSync(filename, JSON.stringify(data, null, 2), "utf8");
252
}
253
};
254
255