Most of a function is being unexpectedly optimised out #16560
Labels
bug: upstream
Bug in a dependency of Vite
p4-important
Violate documented behavior or significantly improves performance (priority)
Describe the bug
On vite 5 (currently testing on 5.2.10). We have this function in our codebase:
Which is being compiled to:
As you can see, everything after the first
if
statement is dropped. AFAICS this should not be happening for any reason I can find in the vite/rollup docs.My suspicion was raised by reading about
treeshake.correctVarValueBeforeDeclaration
in the rollup docs, but there seems to be no reason this would affect this here, assite
is an argument to the function that is passed in (and is always some object except in rare runtime scenarios). (And there is no othersite
variable in the relevant file.) However the issue still occurs whenrollupOptions.treeshake.correctVarValueBeforeDeclaration
is set totrue
.The calls that invoke function elsewhere in the codebase look like this:
and
None of which seems to provide grounds for the compiler/optimiser to assume that
site
would be falsey in theif
statement in the original function.For comparison on vite 4.5.3, the function is compiled/minified, as expected, to:
I've tried to create a minimal repro of the issue but haven't been able to do so, when removed from the codebase vite 5.2.10 compiles this function just fine, not making the faulty code removal. Below is a link to my attempt to reproduce, but this works perfectly. (If anyone has any thoughts on how to go about reproducing this I would be very interested.)The linked repro repo now reproduces the issue.Reproduction
https://github.com/bhallstein/vite-code-removal-repro
Steps to reproduce
npm i
npm run build
System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: