Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 views
1
var path = require("path");
2
var fs = require("fs");
3
var vm = require("vm");
4
var sys = require("util");
5
6
var UglifyJS = vm.createContext({
7
sys : sys,
8
console : console,
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
sys.debug("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
].map(function(file){
36
return path.join(path.dirname(fs.realpathSync(__filename)), file);
37
});
38
39
FILES.forEach(load_global);
40
41
UglifyJS.AST_Node.warn_function = function(txt) {
42
sys.error("WARN: " + txt);
43
};
44
45
// XXX: perhaps we shouldn't export everything but heck, I'm lazy.
46
for (var i in UglifyJS) {
47
if (UglifyJS.hasOwnProperty(i)) {
48
exports[i] = UglifyJS[i];
49
}
50
}
51
52
exports.minify = function(files, options) {
53
options = UglifyJS.defaults(options, {
54
outSourceMap : null,
55
sourceRoot : null,
56
inSourceMap : null,
57
fromString : false,
58
warnings : false,
59
mangle : {},
60
output : null,
61
compress : {}
62
});
63
if (typeof files == "string")
64
files = [ files ];
65
66
// 1. parse
67
var toplevel = null;
68
files.forEach(function(file){
69
var code = options.fromString
70
? file
71
: fs.readFileSync(file, "utf8");
72
toplevel = UglifyJS.parse(code, {
73
filename: options.fromString ? "?" : file,
74
toplevel: toplevel
75
});
76
});
77
78
// 2. compress
79
if (options.compress) {
80
var compress = { warnings: options.warnings };
81
UglifyJS.merge(compress, options.compress);
82
toplevel.figure_out_scope();
83
var sq = UglifyJS.Compressor(compress);
84
toplevel = toplevel.transform(sq);
85
}
86
87
// 3. mangle
88
if (options.mangle) {
89
toplevel.figure_out_scope();
90
toplevel.compute_char_frequency();
91
toplevel.mangle_names(options.mangle);
92
}
93
94
// 4. output
95
var inMap = options.inSourceMap;
96
var output = {};
97
if (typeof options.inSourceMap == "string") {
98
inMap = fs.readFileSync(options.inSourceMap, "utf8");
99
}
100
if (options.outSourceMap) {
101
output.source_map = UglifyJS.SourceMap({
102
file: options.outSourceMap,
103
orig: inMap,
104
root: options.sourceRoot
105
});
106
}
107
if (options.output) {
108
UglifyJS.merge(output, options.output);
109
}
110
var stream = UglifyJS.OutputStream(output);
111
toplevel.print(stream);
112
return {
113
code : stream + "",
114
map : output.source_map + ""
115
};
116
};
117
118
// exports.describe_ast = function() {
119
// function doitem(ctor) {
120
// var sub = {};
121
// ctor.SUBCLASSES.forEach(function(ctor){
122
// sub[ctor.TYPE] = doitem(ctor);
123
// });
124
// var ret = {};
125
// if (ctor.SELF_PROPS.length > 0) ret.props = ctor.SELF_PROPS;
126
// if (ctor.SUBCLASSES.length > 0) ret.sub = sub;
127
// return ret;
128
// }
129
// return doitem(UglifyJS.AST_Node).sub;
130
// }
131
132
exports.describe_ast = function() {
133
var out = UglifyJS.OutputStream({ beautify: true });
134
function doitem(ctor) {
135
out.print("AST_" + ctor.TYPE);
136
var props = ctor.SELF_PROPS.filter(function(prop){
137
return !/^\$/.test(prop);
138
});
139
if (props.length > 0) {
140
out.space();
141
out.with_parens(function(){
142
props.forEach(function(prop, i){
143
if (i) out.space();
144
out.print(prop);
145
});
146
});
147
}
148
if (ctor.documentation) {
149
out.space();
150
out.print_string(ctor.documentation);
151
}
152
if (ctor.SUBCLASSES.length > 0) {
153
out.space();
154
out.with_block(function(){
155
ctor.SUBCLASSES.forEach(function(ctor, i){
156
out.indent();
157
doitem(ctor);
158
out.newline();
159
});
160
});
161
}
162
};
163
doitem(UglifyJS.AST_Node);
164
return out + "";
165
};
166
167