Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80760 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 map = null;
96
var inMap = null;
97
if (options.inSourceMap) {
98
inMap = fs.readFileSync(options.inSourceMap, "utf8");
99
}
100
if (options.outSourceMap) map = UglifyJS.SourceMap({
101
file: options.outSourceMap,
102
orig: inMap,
103
root: options.sourceRoot
104
});
105
var output = { source_map: map };
106
if (options.output) {
107
UglifyJS.merge(output, options.output);
108
}
109
var stream = UglifyJS.OutputStream(output);
110
toplevel.print(stream);
111
return {
112
code : stream + "",
113
map : map + ""
114
};
115
};
116
117
// exports.describe_ast = function() {
118
// function doitem(ctor) {
119
// var sub = {};
120
// ctor.SUBCLASSES.forEach(function(ctor){
121
// sub[ctor.TYPE] = doitem(ctor);
122
// });
123
// var ret = {};
124
// if (ctor.SELF_PROPS.length > 0) ret.props = ctor.SELF_PROPS;
125
// if (ctor.SUBCLASSES.length > 0) ret.sub = sub;
126
// return ret;
127
// }
128
// return doitem(UglifyJS.AST_Node).sub;
129
// }
130
131
exports.describe_ast = function() {
132
var out = UglifyJS.OutputStream({ beautify: true });
133
function doitem(ctor) {
134
out.print("AST_" + ctor.TYPE);
135
var props = ctor.SELF_PROPS.filter(function(prop){
136
return !/^\$/.test(prop);
137
});
138
if (props.length > 0) {
139
out.space();
140
out.with_parens(function(){
141
props.forEach(function(prop, i){
142
if (i) out.space();
143
out.print(prop);
144
});
145
});
146
}
147
if (ctor.documentation) {
148
out.space();
149
out.print_string(ctor.documentation);
150
}
151
if (ctor.SUBCLASSES.length > 0) {
152
out.space();
153
out.with_block(function(){
154
ctor.SUBCLASSES.forEach(function(ctor, i){
155
out.indent();
156
doitem(ctor);
157
out.newline();
158
});
159
});
160
}
161
};
162
doitem(UglifyJS.AST_Node);
163
return out + "";
164
};
165
166