You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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).
While testing whether a component library built with esbuild is tree-shakeable, I noticed that esbuild doesn't add
/* @__PURE__ */
beforeReact.memo
andReact.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
andReact.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
The text was updated successfully, but these errors were encountered: