-
Fix minifier correctness bug with single-use substitutions (#2619)
When minification is enabled, esbuild will attempt to eliminate variables that are only used once in certain cases. For example, esbuild minifies this code:
function getEmailForUser(name) { let users = db.table('users'); let user = users.find({ name }); let email = user?.get('email'); return email; }
into this code:
function getEmailForUser(e){return db.table("users").find({name:e})?.get("email")}
However, this transformation had a bug where esbuild did not correctly consider the "read" part of binary read-modify-write assignment operators. For example, it's incorrect to minify the following code into
bar += fn()
because the call tofn()
might modifybar
:const foo = fn(); bar += foo;
In addition to fixing this correctness bug, this release also improves esbuild's output in the case where all values being skipped over are primitives:
function toneMapLuminance(r, g, b) { let hdr = luminance(r, g, b) let decay = 1 / (1 + hdr) return 1 - decay }
Previous releases of esbuild didn't substitute these single-use variables here, but esbuild will now minify this to the following code starting with this release:
function toneMapLuminance(e,n,a){return 1-1/(1+luminance(e,n,a))}