Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bff7ad67bb | ||
|
|
c2334baa48 | ||
|
|
fb2b6c7c6f | ||
|
|
f5cbe19b75 | ||
|
|
b34fa11a13 |
@@ -621,7 +621,7 @@ function uglify(ast, options, mangle) {
|
|||||||
|
|
||||||
// Compression
|
// Compression
|
||||||
uAST.figure_out_scope();
|
uAST.figure_out_scope();
|
||||||
uAST = uAST.transform(UglifyJS.Compressor(options));
|
uAST = UglifyJS.Compressor(options).compress(uAST);
|
||||||
|
|
||||||
// Mangling (optional)
|
// Mangling (optional)
|
||||||
if (mangle) {
|
if (mangle) {
|
||||||
@@ -865,7 +865,7 @@ toplevel.figure_out_scope()
|
|||||||
Like this:
|
Like this:
|
||||||
```javascript
|
```javascript
|
||||||
var compressor = UglifyJS.Compressor(options);
|
var compressor = UglifyJS.Compressor(options);
|
||||||
var compressed_ast = toplevel.transform(compressor);
|
var compressed_ast = compressor.compress(toplevel);
|
||||||
```
|
```
|
||||||
|
|
||||||
The `options` can be missing. Available options are discussed above in
|
The `options` can be missing. Available options are discussed above in
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ function Compressor(options, false_by_default) {
|
|||||||
booleans : !false_by_default,
|
booleans : !false_by_default,
|
||||||
loops : !false_by_default,
|
loops : !false_by_default,
|
||||||
unused : !false_by_default,
|
unused : !false_by_default,
|
||||||
toplevel : !!options["top_retain"],
|
toplevel : !!(options && options["top_retain"]),
|
||||||
top_retain : null,
|
top_retain : null,
|
||||||
hoist_funs : !false_by_default,
|
hoist_funs : !false_by_default,
|
||||||
keep_fargs : true,
|
keep_fargs : true,
|
||||||
@@ -70,7 +70,7 @@ function Compressor(options, false_by_default) {
|
|||||||
if_return : !false_by_default,
|
if_return : !false_by_default,
|
||||||
join_vars : !false_by_default,
|
join_vars : !false_by_default,
|
||||||
collapse_vars : !false_by_default,
|
collapse_vars : !false_by_default,
|
||||||
reduce_vars : false,
|
reduce_vars : !false_by_default,
|
||||||
cascade : !false_by_default,
|
cascade : !false_by_default,
|
||||||
side_effects : !false_by_default,
|
side_effects : !false_by_default,
|
||||||
pure_getters : false,
|
pure_getters : false,
|
||||||
@@ -187,8 +187,8 @@ merge(Compressor.prototype, {
|
|||||||
if (node instanceof AST_SymbolRef) {
|
if (node instanceof AST_SymbolRef) {
|
||||||
var d = node.definition();
|
var d = node.definition();
|
||||||
d.references.push(node);
|
d.references.push(node);
|
||||||
if (!d.modified && (d.orig.length > 1 || isModified(node, 0))) {
|
if (d.fixed && (d.orig.length > 1 || isModified(node, 0))) {
|
||||||
d.modified = true;
|
d.fixed = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Call && node.expression instanceof AST_Function) {
|
if (node instanceof AST_Call && node.expression instanceof AST_Function) {
|
||||||
@@ -205,7 +205,7 @@ merge(Compressor.prototype, {
|
|||||||
this.walk(tw);
|
this.walk(tw);
|
||||||
|
|
||||||
function reset_def(def) {
|
function reset_def(def) {
|
||||||
def.modified = false;
|
def.fixed = true;
|
||||||
def.references = [];
|
def.references = [];
|
||||||
def.should_replace = undefined;
|
def.should_replace = undefined;
|
||||||
if (unsafe && def.init) {
|
if (unsafe && def.init) {
|
||||||
@@ -1148,11 +1148,14 @@ merge(Compressor.prototype, {
|
|||||||
def(AST_Statement, function(){
|
def(AST_Statement, function(){
|
||||||
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
|
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
|
||||||
});
|
});
|
||||||
|
// XXX: AST_Accessor and AST_Function both inherit from AST_Scope,
|
||||||
|
// which itself inherits from AST_Statement; however, they aren't
|
||||||
|
// really statements. This could bite in other places too. :-(
|
||||||
|
// Wish JS had multiple inheritance.
|
||||||
|
def(AST_Accessor, function(){
|
||||||
|
throw def;
|
||||||
|
});
|
||||||
def(AST_Function, function(){
|
def(AST_Function, function(){
|
||||||
// XXX: AST_Function inherits from AST_Scope, which itself
|
|
||||||
// inherits from AST_Statement; however, an AST_Function
|
|
||||||
// isn't really a statement. This could byte in other
|
|
||||||
// places too. :-( Wish JS had multiple inheritance.
|
|
||||||
throw def;
|
throw def;
|
||||||
});
|
});
|
||||||
function ev(node, compressor) {
|
function ev(node, compressor) {
|
||||||
@@ -1180,7 +1183,9 @@ merge(Compressor.prototype, {
|
|||||||
for (var i = 0, len = this.properties.length; i < len; i++) {
|
for (var i = 0, len = this.properties.length; i < len; i++) {
|
||||||
var prop = this.properties[i];
|
var prop = this.properties[i];
|
||||||
var key = prop.key;
|
var key = prop.key;
|
||||||
if (key instanceof AST_Node) {
|
if (key instanceof AST_Symbol) {
|
||||||
|
key = key.name;
|
||||||
|
} else if (key instanceof AST_Node) {
|
||||||
key = ev(key, compressor);
|
key = ev(key, compressor);
|
||||||
}
|
}
|
||||||
if (typeof Object.prototype[key] === 'function') {
|
if (typeof Object.prototype[key] === 'function') {
|
||||||
@@ -1258,7 +1263,7 @@ merge(Compressor.prototype, {
|
|||||||
this._evaluating = true;
|
this._evaluating = true;
|
||||||
try {
|
try {
|
||||||
var d = this.definition();
|
var d = this.definition();
|
||||||
if (compressor.option("reduce_vars") && !d.modified && d.init) {
|
if (compressor.option("reduce_vars") && d.fixed && d.init) {
|
||||||
if (compressor.option("unsafe")) {
|
if (compressor.option("unsafe")) {
|
||||||
if (d.init._evaluated === undefined) {
|
if (d.init._evaluated === undefined) {
|
||||||
d.init._evaluated = ev(d.init, compressor);
|
d.init._evaluated = ev(d.init, compressor);
|
||||||
@@ -2189,7 +2194,7 @@ merge(Compressor.prototype, {
|
|||||||
// here because they are only used in an equality comparison later on.
|
// here because they are only used in an equality comparison later on.
|
||||||
self.condition = negated;
|
self.condition = negated;
|
||||||
var tmp = self.body;
|
var tmp = self.body;
|
||||||
self.body = self.alternative || make_node(AST_EmptyStatement);
|
self.body = self.alternative || make_node(AST_EmptyStatement, self);
|
||||||
self.alternative = tmp;
|
self.alternative = tmp;
|
||||||
}
|
}
|
||||||
if (is_empty(self.body) && is_empty(self.alternative)) {
|
if (is_empty(self.body) && is_empty(self.alternative)) {
|
||||||
@@ -2454,7 +2459,7 @@ merge(Compressor.prototype, {
|
|||||||
case "Boolean":
|
case "Boolean":
|
||||||
if (self.args.length == 0) return make_node(AST_False, self);
|
if (self.args.length == 0) return make_node(AST_False, self);
|
||||||
if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
|
if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
|
||||||
expression: make_node(AST_UnaryPrefix, null, {
|
expression: make_node(AST_UnaryPrefix, self, {
|
||||||
expression: self.args[0],
|
expression: self.args[0],
|
||||||
operator: "!"
|
operator: "!"
|
||||||
}),
|
}),
|
||||||
@@ -2850,7 +2855,7 @@ merge(Compressor.prototype, {
|
|||||||
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
|
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
|
||||||
return make_node(AST_Seq, self, {
|
return make_node(AST_Seq, self, {
|
||||||
car: self.left,
|
car: self.left,
|
||||||
cdr: make_node(AST_False)
|
cdr: make_node(AST_False, self)
|
||||||
}).optimize(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
if (ll.length > 1 && ll[1]) {
|
if (ll.length > 1 && ll[1]) {
|
||||||
@@ -3041,7 +3046,7 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
if (compressor.option("evaluate") && compressor.option("reduce_vars")) {
|
if (compressor.option("evaluate") && compressor.option("reduce_vars")) {
|
||||||
var d = self.definition();
|
var d = self.definition();
|
||||||
if (!d.modified && d.init) {
|
if (d.fixed && d.init) {
|
||||||
if (d.should_replace === undefined) {
|
if (d.should_replace === undefined) {
|
||||||
var init = d.init.evaluate(compressor);
|
var init = d.init.evaluate(compressor);
|
||||||
if (init.length > 1) {
|
if (init.length > 1) {
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ function push_uniq(array, el) {
|
|||||||
|
|
||||||
function string_template(text, props) {
|
function string_template(text, props) {
|
||||||
return text.replace(/\{(.+?)\}/g, function(str, p){
|
return text.replace(/\{(.+?)\}/g, function(str, p){
|
||||||
return props[p];
|
return props && props[p];
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"homepage": "http://lisperator.net/uglifyjs",
|
"homepage": "http://lisperator.net/uglifyjs",
|
||||||
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
||||||
"license": "BSD-2-Clause",
|
"license": "BSD-2-Clause",
|
||||||
"version": "2.8.1",
|
"version": "2.8.3",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.8.0"
|
"node": ">=0.8.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -337,6 +337,32 @@ unsafe_object_repeated: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe_object_accessor: {
|
||||||
|
options = {
|
||||||
|
evaluate: true,
|
||||||
|
reduce_vars: true,
|
||||||
|
unsafe: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
var a = {
|
||||||
|
get b() {},
|
||||||
|
set b() {}
|
||||||
|
};
|
||||||
|
return {a:a};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
var a = {
|
||||||
|
get b() {},
|
||||||
|
set b() {}
|
||||||
|
};
|
||||||
|
return {a:a};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unsafe_function: {
|
unsafe_function: {
|
||||||
options = {
|
options = {
|
||||||
evaluate : true,
|
evaluate : true,
|
||||||
|
|||||||
@@ -182,4 +182,13 @@ describe("minify", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("Compressor", function() {
|
||||||
|
it("should be backward compatible with ast.transform(compressor)", function() {
|
||||||
|
var ast = Uglify.parse("function f(a){for(var i=0;i<a;i++)console.log(i)}");
|
||||||
|
ast.figure_out_scope();
|
||||||
|
ast = ast.transform(Uglify.Compressor());
|
||||||
|
assert.strictEqual(ast.print_to_string(), "function f(a){for(var i=0;i<a;i++)console.log(i)}");
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user