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
Wrap hoisted JSX elements into IIAFE so pure annotation can work #11105
Conversation
{_ref3} | ||
<AppItem /> | ||
</div>; | ||
{_ref4} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The <AppItem />
here is unnecessarily hoisted to the same scope of _ref
because the Path Hoister can not neglect the introduced IIAFE. Note that when the JSX element is not hoisted to the top level, its children may still have opportunity hoisted to top level. But we can't know which scope the element will be hoisted to until we have run path.hoist()
, after that the AST is changed and it's too later to determine whether such hoisting is favourable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we added a new parameter to path.hoist()
, to say "hoist this path but only if it can go at least up to this scope"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we can do it but due to the semver requirements we can not ship it in patch/minor releases unless we are doing dirty version checks.
Do we have plans to change semver to minor-version compatibilities in Babel 8?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone uses an old @babel/traverse
version, their JSX elements will be hoisted singularly because the "minimum scope" parameter is ignored. It's not optimal, but it will still perfectly work.
This seems like it adds overhead to the startup path, which is bad. However, the current transform is already non-ideal in that sense (it adds startup overhead with no lazyness). We couldn't get it working at FB so I don't think we care strongly about how it works. |
Actually, adding a |
Closing it in favor of #11126 |
@babel/plugin-transform-react-constant-elements
adds pure annotation to the hoisted JSX elements.However, in reality it never work as expected with bundlers/minifiers because
terser
only supports pure annotation on call expression (terser/terser#513 (comment)). This PR wraps the hoisted elements into an IIAFE so Rollup can remove unused constant elements.Note that this PR will cause unnecessary hoisting under certain scenarios, (see comments below). To mitigate this side effect we may have babel-traverse exposed the Path Hoister API, but it could not be done in v7 due to our semver requirement.
cc @babel/react