diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2019-10-29 16:53:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-29 16:53:48 +0800 |
commit | bad664c6322538f1a5eec7f76dc44f81fa05c9e0 (patch) | |
tree | c8bb72ced239cd86cdb36f2efdf668f7df8a56df /lib | |
parent | 8a191c0a8486bfd2a51562f0c072e00507abea6e (diff) | |
download | tracifyjs-bad664c6322538f1a5eec7f76dc44f81fa05c9e0.tar.gz tracifyjs-bad664c6322538f1a5eec7f76dc44f81fa05c9e0.zip |
compress object literals (#3546)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/compress.js b/lib/compress.js index 10161337..3058b678 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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; |