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

Tree shaking doesn't work for react components wrapped in memo or forwardRef #3492

Closed
SpaNb4 opened this issue Nov 15, 2023 · 2 comments
Closed

Comments

@SpaNb4
Copy link

SpaNb4 commented Nov 15, 2023

While testing whether a component library built with esbuild is tree-shakeable, I noticed that esbuild doesn't add /* @__PURE__ */ before React.memo and React.forwardRef functions, preventing tree shaking of unused components.

According to the documentation (https://esbuild.github.io/api/#pure), esbuild automatically annotates some expressions. However, it seems that React.memo and React.forwardRef are not included in this list.

Looks like Babel had the same issue (babel/babel#11399), which has since been resolved (babel/babel#11428)

Reproduction:
https://esbuild.github.io/try/#YgAwLjE5LjUAZW50cnkuanMgLS1idW5kbGUgLS1leHRlcm5hbDpyZWFjdCAtLWZvcm1hdD1lc20AAGNvbXBvbmVudHMuanN4AGltcG9ydCBSZWFjdCBmcm9tICdyZWFjdCc7CgpleHBvcnQgY29uc3QgQnV0dG9uV2l0aE1lbW8gPSBSZWFjdC5tZW1vKCgpID0+IHsKICByZXR1cm4gPGJ1dHRvbiAvPjsKfSk7CgpleHBvcnQgY29uc3QgTGFiZWxXaXRoRm9yd2FyZFJlZiA9IFJlYWN0LmZvcndhcmRSZWYoKCkgPT4gewogIHJldHVybiA8bGFiZWwgLz47Cn0pOwoKZXhwb3J0IGNvbnN0IEJ1dHRvbiA9KCgpID0+IHsKICByZXR1cm4gPGJ1dHRvbiAvPjsKfSk7CgpleHBvcnQgY29uc3QgTGFiZWwgPSAoKCkgPT4gewogIHJldHVybiA8bGFiZWwgLz47Cn0pOwoAAGVudHJ5LmpzAGltcG9ydCB7IEJ1dHRvbldpdGhNZW1vLCBMYWJlbFdpdGhGb3J3YXJkUmVmLCBCdXR0b24sIExhYmVsIH0gZnJvbSAiLi9jb21wb25lbnRzIjsKCg

@evanw
Copy link
Owner

evanw commented Nov 15, 2023

Pure annotations are currently only automatically added for certain global function calls and for JSX expressions. Neither of these things are specific to React. There is no logic within esbuild itself to add pure annotations for function calls of specific libraries, and I don't think esbuild's code is the right place to put the code to do that. The web ecosystem has an extremely large set of JavaScript libraries (with more being made all the time) and hard-coding all of that into esbuild doesn't make sense. These annotations should either be manually authored, or done as a preprocessing step (which would be equivalent to the Babel plugin you linked to), or done via some form of general-purpose mechanism that isn't React-specific (e.g. something like #3149, except that's not easy to implement given esbuild's current approach to tree-shaking).

@evanw
Copy link
Owner

evanw commented Dec 19, 2023

Closing this issue as out of scope.

@evanw evanw closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants