aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorkzc <zaxxon2011@gmail.com>2017-02-18 19:12:57 +0800
committeralexlamsl <alexlamsl@gmail.com>2017-02-21 13:29:58 +0800
commit6ffbecb72b515d6e9e6dee1f76d8f27e4b014854 (patch)
treea8a375aa8a790d291ccc7dfdde63217fd5c3d0d2 /lib
parentf0ff6189be3a75cd4ccb1c38051ec27f9b30d67f (diff)
downloadtracifyjs-6ffbecb72b515d6e9e6dee1f76d8f27e4b014854.tar.gz
tracifyjs-6ffbecb72b515d6e9e6dee1f76d8f27e4b014854.zip
smarter const replacement taking name length into account
closes #1459
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js12
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/compress.js b/lib/compress.js
index b66c5582..1f710f10 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2784,6 +2784,18 @@ merge(Compressor.prototype, {
}
}
}
+ if (compressor.option("evaluate") && !isLHS(self, compressor.parent())) {
+ var d = self.definition();
+ if (d && d.constant && d.init && d.init.is_constant(compressor)) {
+ var original_as_string = self.print_to_string();
+ var const_node = make_node_from_constant(compressor, d.init.constant_value(compressor), self);
+ var const_node_as_string = const_node.print_to_string();
+ var per_const_overhead = d.global || !d.references.length ? 0
+ : (d.name.length + 2 + const_node_as_string.length) / d.references.length;
+ if (const_node_as_string.length <= original_as_string.length + per_const_overhead)
+ return const_node;
+ }
+ }
return self;
});