esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

keep-names to only be applied when name is changed

Open privatenumber opened this issue 2 years ago • 9 comments

I would like to request for --keep-names to only be applied when the name is changed (e.g. via minification).

Currently, it applies even if the name is unchanged: https://hyrious.me/esbuild-repl/?version=0.15.10&mode=transform&input=function+name%28%29+%7B%7D&options=--keep-names

This improvement should have the benefit of reducing transformation size and also preserve as much of the original code when possible (which can help with https://github.com/evanw/esbuild/issues/1438).

privatenumber avatar Oct 11 '22 12:10 privatenumber

I tried to have a stab at this because I was hoping it'd essentially be an extra if.

However, I'm a bit thrown off by the fact that the __name calls are introduced at parse time. Specifically, keepStmtSymbolName creates an EIdentifier from the provided ref. It looks like that will get turned into the final name by the JS printer, but that's not available yet when parsing, so there's nothing to compare.

Disclaimer: I know my way around Go but it's my first time hacking at esbuild, so there's a good chance I'm wrong. 🤞🏻

timdp avatar Apr 05 '23 12:04 timdp

I had script for Vue SFC compilation script that I used to run using tsx but I now get __name error. Bumping this for the visibility and possible solution.

jd-solanki avatar Aug 28 '23 12:08 jd-solanki

This bug seems to prevent tsx from working with puppeteer, bump for fix

pixelpax avatar Feb 27 '24 21:02 pixelpax

bumping as well... unable to use puppeteer and tsx. Please fix.

jackspirou avatar Feb 28 '24 00:02 jackspirou

This is also blocking us + would love to see this resolved 👍

connorgiles avatar Mar 04 '24 01:03 connorgiles

I was able to resolve this for puppeteer by just creating my own function:

await page.evaluateOnNewDocument(() => (window.__name = (func) => func));
// .. All the other evaluates you need

jim-alexander avatar Apr 12 '24 01:04 jim-alexander

^ If the above didn't work check out this solution: https://github.com/vuejs/core/issues/8303

cjgvalent avatar May 31 '24 14:05 cjgvalent

I was able to resolve this for puppeteer but just creating my own function:

await page.evaluateOnNewDocument(() => (window.__name = (func) => func));
// .. All the other evaluates you need

The following worked for me:

  await page.evaluate(() => {
    (window as any).__name = (func: Function) => func;
  });

DOCS-kdu3 avatar Jun 03 '24 20:06 DOCS-kdu3

await page.evaluate......

it works, thx very much!

pdsuwwz avatar Jul 18 '24 07:07 pdsuwwz