smarter function eval/inline
https://kermanx.github.io/tree-shaker/#N4IglgdgDgrgLiAXCAZjCBjOYD2EAEcApgM5wAUAlPsADoH4BORcMjBtIAhj73153oBfeqMx4SOADZEAdFJwBzcsTLkAjJUr0ME6XIXLVFTdvERJM+UpWkTWnXquHba044v7rRuxofnLAxtjPzNdT2dg33cArxcQmPDA71d7MKcgnzd/JLio7LMxehAAGhAAExwAFWYiAGUACy4AayIkOEYYIjLKgFlIMBQATyQULikSIiEgA===
For this case, the optimized result is larger than the unoptimized version.
compare to swc minify https://play.swc.rs/?version=1.7.28&code=H4sIAAAAAAAAA%2B2KOwqAMBAF%2Bz3Fw8o0ikcKcf1A2JXNBgvx7oqNF7B0qmGYvsduqzPUsMVyS9KRsbAx0VQl%2BaoC5%2BJtwEGAsVcTNPGloZOIkkrRzF3WuX3%2BIYQ%2FfhwvkphZV68BAAA%3D&config=H4sIAAAAAAAAA41VS27jMAzd9xSB113MLKYY9ADdzRkExaIcZWTRECk3QZG7D63YadrQxmyCmI8f8emR%2Bnja7Zojtc3r7kP%2BysdgM0G%2BfYuFzontSSwNtL2lNoeBm%2BcFPdIEeRsJqulyRRq2uQOuUfRrdm8iIoHYOBeYTX1IwZ%2Fv67XYDxmI7mxilXSlh8S0VHu%2BxzK%2B05e81b5HjGDTBmIsmZAYOsha4hZjtAOBGW1WskwntTkQaiUmsDA4M2QcVDy5wAGT1HxEHVhnWnSgQCFDy2EELUxqSVgiaU%2Fpp8IO9qXr6h1%2Fi4bRxmJZqQmneiVyWiXrAQOx8SVpFF7BFQ6u4Ezu98jgTQYuOT3GHTGklTv5CyAMREuUbA9a3urhRU9r0X4zMiQvkuWzgou8tS4TdEKqCcErzE7MQOag3WYGV1qYmG2148zwCn0UHBjwXrSipKb3wO1BK8rnAdArgNyv9ZqqroC5TeEKPg3EBvwmXbIusNmjt3xYR%2Bnc7zFuFOiBD%2Bg2HOQqGNfhLFviNKzjJTkQaYBTXQpV4HEJyAAwmlh35YM2ZDwko%2Bki7j%2FXxOxwue3g3qauzvvdvmQcIowQ12T8HyOyicrZxknVy2Q%2FTC%2F81kRpZfDCzx9fngxp5Wn5rU01Pbpy11DV5PUpeWk%2BnZaXY2GtCfRnCaw8Xf4BLcgrK98GAAA%3D
Interesting thing, terser has same strategy as tree-shaker, they always inline function for this case.
for swc, it only inlines the function result when the function is called only once.
Currently, there is a max_simple_string_length config, strings longer than this value won't be inlined.
I can also think of some other strategies:
- Option 1: Compare the literal's length to the
spanwidth of the node to be replaced - Option 2: If a string is going to be inlined multiple times, then append
var unique_name="ABCDE"at the end of the program and useunique_nameinstead.
And there is also a problem of
export function f() {
effect()
}
export const x = f()
being tree-shaked into
export function f() {
effect();
}
export const x = (f(), undefined);
This is not hard to solve but will lead to more lines of code - every transform function should return whether the value is still reserved. Not sure if there is a better strategy.
It seems that duplicating literals only has a very small effect on the gzipped size. And I found it cost a noticeable performance to avoid the duplication🤔
Yeah, this issue just found when I tried to use tree-shaker, if the improvement is trivial, maybe we should skip this optimization.