esbuild.keepNames causes typescript closure/function to not be tree shaken #9164
Closed
7 tasks done
Labels
p3-minor-bug
An edge case that only affects very specific usage (priority)
Describe the bug
When
esbuild.keepNames
is enabled tree shaking doesn't work with closure/functions written in typescript.In my reproduction case I setup 4 functions
devOnlyClosure
that starts withif (import.meta.env.PROD) return;
devOnlyFunction
that starts withif (import.meta.env.PROD) return;
thisClosureIsNeverCalled
that is never calledthisFunctionIsNeverCalled
that is never calledFrom my main file, I import the first two functions can call them.
When I make a production build I expect no JavaScript bundle as both of the called functions are effectively empty and remaining closure/function is never called nor imported.
But what I end up getting is this:
What is unexpected:
devOnlyClosure
has been correctly transformed into an empty closure but still remainsdevOnlyClosure
hasn't been removeddevOnlyFunction
has been correctly transformed into an empty function but still remainsthisFunctionIsNeverCalled
is never called but still remains.name
assigned twiceWhat is strange:
devOnlyFunction
was removed, butdevOnlyClosure
wasn'tthisClosureIsNeverCalled
was removed, butthisFunctionIsNeverCalled
wasn't.name
assignment, but closures only get onefunctions.ts
is renamed to be a javascript filefunctions.js
, then production build returns empty correctlyesbuild.keepNames
is turned off, then production build returns empty correctlyWhat I suspect is happening is the typescript file is fed through esbuild transform, then after rollup has created the bundle, it is fed through esbuild again to minify. This is why there is a double
.name
assignment.Reproduction
https://stackblitz.com/edit/vitejs-vite-79hhqf?file=dist/assets/index.70f295b1.js
System Info
System: OS: Linux 5.0 undefined CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: 1.0 - /bin/jsh Binaries: Node: 16.14.2 - /usr/local/bin/node Yarn: 1.22.10 - /bin/yarn npm: 7.17.0 - /bin/npm npmPackages: vite: 3.0.0 => 3.0.0
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: