swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
30.92k stars 1.21k forks source link

Minifier doesn't compress UpdateExpression and AssignmentExpression nodes if rhs is a literal #9277

Open levi-nz opened 1 month ago

levi-nz commented 1 month ago

Describe the feature

Expressions like ++[0][0], [0][0] += 1 should be compressed to their literal value, e.g. 1, if the argument is a literal

https://play.swc.rs/?version=1.7.0&code=H4sIAAAAAAAAA0vT0NaONogFIk1rLq40DQhbQdtWwRAoAACKdnIZHgAAAA%3D%3D&config=H4sIAAAAAAAAA32UO5LbMAyG%2Bz2FR3WKjIsUe4B0OQOHFkGZDkloCNBrzY7vHujlddaQOgkffoAEQHy%2BHQ7Nhdrm%2FfApn%2FLT20JQHv9ioSGzvYmlgTZZakvoufmx0guNyNtIMJnuM2nYlg54UtHx5%2FG4KJqISLAqFlsKOfjhOWeLqS9A9GQTq4SsCTLT%2F%2FqFFfwYAZf6bD8hRrB5hxhLJmSGDooWuMUYbU9grrYoUcaT2hIItRQjrAzO9AV7lWcXOGCWnK%2FUgXWmRQcKCgVaDlfQZJJLZJnkesp9JuzgVLtu6vM3NVxtrJaVnHCbWiKnVaKeMRAbX7NWwhlu1GCGS3G%2FK4M3BbiW%2FKq7YMgbPfkLIBWIlijbBFrcycPLPG2p%2Fa4yZC8jy4PCZb61W2bopKgmBK9UdqwMFA5aNwu42sJY2VY7zoI3ykfBgQHvZVaU0PQRuD1rSXnoAb0CpL%2FWa1M1A%2FN4hRt8fBA7%2BLfckvUBWzyS5fM2pSGdMO4kSMBndDsO0grGbVxkS9z6bV6zAxkNcKpLpQm8LgF5AIwmTvvyZTbkeUhE00U8fa2JxeH%2B2MPJ5i7CM72%2FLbxJ6OrElh0%2FtnfezL%2BaL6d1Ca8HaAL9WYXzfr%2F%2FA1SPPyYvBgAA

Babel plugin or link to the feature description

No response

Additional context

No response

magic-akari commented 1 month ago

This expression doesn't look like it was handwritten by a human. It's also not handled in Terser.

Is there data showing it often appears in the JS build output? We don't want to introduce logic for code structures in SWC that we might not encounter.

levi-nz commented 1 month ago

This expression doesn't look like it was handwritten by a human.

It's also not handled in Terser.

Is there data showing it often appears in the JS build output?

We don't want to introduce logic for code structures in SWC that we might not encounter.

I can't imagine any human would write it. Code like this is usually found in scripts that are obfuscated, at least partially, as they use code like []+[] to create literals. So this is more for people using swc for reverse engineering, e.g. if you're reverse engineering malware.

I think handling this could also potentially handle the following:

var x = 1;
f(x++);

but I'm unsure.

magic-akari commented 1 month ago

I don't think SWC's minification can be used for reverse engineering; it goes against its original purpose. However, there is a tool specifically designed for this, you might want to check out https://github.com/pionxzh/wakaru

levi-nz commented 1 month ago

I don't think SWC's minification can be used for reverse engineering; it goes against its original purpose. However, there is a tool specifically designed for this, you might want to check out https://github.com/pionxzh/wakaru

The minifier can't be, but the underlying functions it uses can be, e.g. cast_to_bool can be used to replace !0, !!0 etc with boolean literals.

Adding this feature should be a relatively small change, but if you don't think it should be added we can close this issue. Visitors can use cast_to_number themselves if they want to replace these expressions anyway, so it doesn't matter too much regardless.