-
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
Inline values which are used only once #3568
Comments
If you put it in a function, it will be minified further: function f() {
let x = false;
let y = x;
const boolean = y;
var frag = $.template(`<p contenteditable="${boolean}">hello world</p>`);
console.log(frag)
} That turns into this: function f(){const e=!1;var l=$.template(`<p contenteditable="${e}">hello world</p>`);console.log(l)} One reason that this only happens within a function in this case is because when minifying a string of raw JavaScript, esbuild doesn't assume that top-level variables (i.e. And one reason that Edit: I investigated this. It looks like the single-use expression substitution allows reordering a side-effect past a primitive but not a primitive past a side-effect. I think the second part is an oversight. Adding that case makes this work. |
I think I can get this to turn into the following code: function f(){let e=$.template('<p contenteditable="false">hello world</p>');console.log(e)} But reordering |
Thank you so much both for the improvement and informative sharing of details! |
playgound link
If you have:
When minimizing, it gets compiled to:
It could be simplified to:
While this code is written in an obviously dumb way for reproduction purposes, there's lots of places where this is encountered in less dumb ways. E.g. a number of Vite plugins may produce code that could be simplified in this manner. While we could invest effort into individually improving each plugin, those changes aren't necessarily easy, and so it could be a better use of effort to address it just a single time here.
The text was updated successfully, but these errors were encountered: