-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not treeshaking &&
variables
#598
Comments
That's correct, esbuild won't remove this as dead code. That's because it doesn't currently do constant detection and inlining like this. I haven't done this yet because doing that is actually pretty hard and can sometimes be impossible. For example: // index.ts
logInfo("Media query was changed");
// debug.ts
var DEBUG = true;
function logInfo(...args) {
DEBUG && console.info(...args);
} In this case it would be incorrect for esbuild to replace Instead, the intended way to do this with esbuild is with the define feature. Remove the |
Mhm, I understand mutable variables are indeed hard to parse. Not sure why Rollup then removed it, best assumption is that they don't replace the top-level variables to const DEBUG = false; Which is immutable and therefore safe assume to remove it.
I've tried it and that indeed works! Thanks for your explanation! So I had little question, not sure if you want it into a new issue or not so. Regards, |
Accessing this variable before it's defined is technically supposed to throw an exception, so the behavior is still different than replacing it with
The This is force-enabled both because of the Safari performance issue and because of some upcoming changes to the bundler. I'm planning on making modules lazily-initialized in some cases related to code splitting. To do that and still keep the performance benefits of ESM's static binding semantics, you need to defer the initialization of top-level constants. That means they can't be There is also a benefit for minification, not just because |
Aha, I understand. That actually make sense now thinking about it.
I'm looking forward to see those changes and that is some great logic you explained here. Thank you for clearing certain things up. I'm looking to see the changes you mentioned coming into ESBuild and thanks for the project! Regards, |
Hey!
So, I've recently hacked around
myproject dark reader to switch from rollup to ESBuild and try to lose no features over rollup other than it's slow bundling, the only thing that is missing isinstanbul's code-coverage
alongsidekarma
but that isn't the point now.So we use 2 functions
logWarn
andlogInfo
to output debug information when theDEBUG
variable istrue
, we currently do this dynamically by defining__DEBUG__
to the debug flag in our bundling process. Rollup always removed any instances to those 2 function when theDEBUG
variable was false. As it wouldn't run anyway.However, I've noticed with ESBuild's treeshaking this isn't the case and no
dead code elimination
seems to be done while treeshaking is enabled. As I'm following this project for a while I know that a simple repo will come in handy https://github.com/Gusted/esbuild-declare/ a simple and clean repo. At first glance I thought that defining variables didn't work with thedead code elimination
and also created a second build for-correct
that didn't implement any defining however the result was the same and nodead code elimination
was in place.As rollup will be a big hassle to implement with typescript compiling it wasn't added, but this was rather for a comparison that Rollup does this.
Rollup treeshakes it down to outputting nothing, or when other relevant code that will run is added. Minifying the functions to
And removing the
DEBUG
variable and leave all calls to logInfo/logWarn intact.Esbuild currently doesn't do anything and will output:
Even though at first glance you can see nothing will ever happen other than evaluating some variable.
Regards,
Gusted
The text was updated successfully, but these errors were encountered: