compress object literals (#3546)

This commit is contained in:
Alex Lam S.L
2019-10-29 16:53:48 +08:00
committed by GitHub
parent 8a191c0a84
commit bad664c632
3 changed files with 281 additions and 20 deletions

View File

@@ -74,6 +74,7 @@ function Compressor(options, false_by_default) {
keep_infinity : false,
loops : !false_by_default,
negate_iife : !false_by_default,
objects : !false_by_default,
passes : 1,
properties : !false_by_default,
pure_getters : !false_by_default && "strict",
@@ -6981,6 +6982,41 @@ merge(Compressor.prototype, {
return self;
});
OPT(AST_Object, function(self, compressor) {
if (!compressor.option("objects") || compressor.has_directive("use strict")) return self;
var props = self.properties;
var keys = new Dictionary();
var values = [];
self.properties.forEach(function(prop) {
if (typeof prop.key != "string") {
flush();
values.push(prop);
return;
}
if (prop.value.has_side_effects(compressor)) {
flush();
}
keys.add(prop.key, prop.value);
});
flush();
if (self.properties.length != values.length) {
return make_node(AST_Object, self, {
properties: values
});
}
return self;
function flush() {
keys.each(function(expressions, key) {
values.push(make_node(AST_ObjectKeyVal, self, {
key: key,
value: make_sequence(self, expressions)
}));
});
keys = new Dictionary();
}
});
OPT(AST_Return, function(self, compressor) {
if (self.value && is_undefined(self.value, compressor)) {
self.value = null;