Skip to content
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

The function inside the closure will be renamed #11136

Closed
7 tasks done
zlargon opened this issue Dec 1, 2022 · 6 comments
Closed
7 tasks done

The function inside the closure will be renamed #11136

zlargon opened this issue Dec 1, 2022 · 6 comments
Labels
duplicate This issue or pull request already exists

Comments

@zlargon
Copy link

zlargon commented Dec 1, 2022

Describe the bug

The internal function will be renamed if its name is same to the external function.
This seems related to the compiler because it won't happen when vite is working with pure javascript.

function myFunc() {
  return function myFunc() {};
}

// expect the function name to be 'myFunc', but the actual name is 'myFunc2'
console.log(myFunc().name);

Can be reproduced with:

Combination Link
vite + typescript https://stackblitz.com/edit/vitejs-vite-7fmv91?file=src/main.ts
vite + react https://stackblitz.com/edit/vitejs-vite-w9neds?file=src/App.jsx
vite + typescript + react https://stackblitz.com/edit/vitejs-vite-bdguti?file=src/App.tsx

Cannot be reproduced with:

Combination Link
vite + javascript https://stackblitz.com/edit/vitejs-vite-7rhr1u?file=main.js
pure typescript https://stackblitz.com/edit/typescript-v3lbde?file=index.ts
create-react-app + javascript None
create-react-app + typescript None

Reproduction

https://stackblitz.com/edit/vitejs-vite-7fmv91?file=src/main.ts

Steps to reproduce

  • npm install
  • npm run dev

System Info

System:
  OS: Linux 5.0 undefined
  CPU: (8) 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.19 - /usr/local/bin/yarn
  npm: 7.17.0 - /usr/local/bin/npm

npmPackages:
  vite: ^3.2.4 => 3.2.4

Used Package Manager

npm

Logs

No response

Validations

@luo3house
Copy link
Contributor

If run vite build in cases above, all the name of functions will be changed due to esbuild minify.

You may let esbuild keep the function name by Object.defineProperty(fn, "name", ...), and finally got the original name.

// vite.config.js
export default defineConfig({
  esbuild: {
    keepNames: true
  }
})

There is another esbuild REPL for reproduction.

@zlargon
Copy link
Author

zlargon commented Dec 1, 2022

Ah, I see. Good to know that the function name will be changed by esbuild.

I try to add esbuild.keepNames in vite.config.js, but it doesn't work. Is something missing?

Not work

Combination Link
vite + typescript https://stackblitz.com/edit/vitejs-vite-7fmv91?file=vite.config.js
vite + react https://stackblitz.com/edit/vitejs-vite-w9neds?file=vite.config.js
vite + typescript + react https://stackblitz.com/edit/vitejs-vite-bdguti?file=vite.config.ts

Can work

It can work if I use Object.defineProperty without adding esbuild.keepNames in vite.config.js. However, I prefer the solution to config esbuild so that I don't need to refactor the huge code base.

function myFunc() {
  function myFunc() {}
  Object.defineProperty(myFunc, 'name', { value: 'myFunc' });
  return myFunc;
}

@luo3house
Copy link
Contributor

You are right. esbuild keepNames option are still set to false by Vite though it is presented in user config.

There is an early issue(#9164). It discovered the keepNames breaks the tree-shaking.

@zlargon
Copy link
Author

zlargon commented Dec 1, 2022

Got it. I think it would be better to update the document to mention some of the esbuild configs are disabled.
https://vitejs.dev/config/shared-options.html#esbuild

Does Vite has a plan to bring keepNames back in the future? I would like to keep tracking this if there is a relevant thread.

@lyleaigbedion
Copy link

+1 on this, vite renames a function in my app, breaking my feature completely. Is there an eta on when this will be re enabled?

@sapphi-red
Copy link
Member

Duplicate of #13727

@sapphi-red sapphi-red marked this as a duplicate of #13727 Feb 28, 2024
@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Feb 28, 2024
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Feb 28, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

4 participants