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

Lazily inject imports to the JSX runtime #12493

Merged

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Dec 12, 2020

Q                       A
Fixed Issues? Fixes #12467
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Injecting automatic imports to react on Program:enter has a problem: if a plugin later injects some JSX, the automatic imports logic cannot inject the necessary import.

This PR changes the injection to happen when transforming the JSX element, so that it works with injected JSX.

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories area: react labels Dec 12, 2020
const foo = /*#__PURE__*/undefined.jsx("p", {});
var _reactJsxRuntime = require("react/jsx-runtime");

const foo = /*#__PURE__*/_reactJsxRuntime.jsx("p", {});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixture shows that it's fixed.

@babel-bot
Copy link
Collaborator

babel-bot commented Dec 12, 2020

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/35275/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 12, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit e9daba1:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@nicolo-ribaudo nicolo-ribaudo changed the title Lazily inject imports to the React runtime Lazily inject imports to the JSX runtime Dec 12, 2020
Comment on lines +320 to +322
let reference = pass.get(
`@babel/plugin-react-jsx/imports/${importName}`,
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering - does keeping this in a state has any benefit here? i mean - wouldn't a closure state do the trick just fine? I don't expect any change here - just wondering if there is something I'm not thinking about

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin might be cached, so multiple runs of the plugin over the same file could have the same closure. Thus, we would still need to have a programNode -> imports WeakMap to avoid using imports attatched in a separate Babel run.

@nicolo-ribaudo nicolo-ribaudo merged commit 28d2cbc into babel:main Dec 15, 2020
@nicolo-ribaudo nicolo-ribaudo deleted the fix-react-automatic-undefined branch December 15, 2020 11:55
@JLHwung
Copy link
Contributor

JLHwung commented Dec 15, 2020

@nicolo-ribaudo Did this PR also fix #12468 (comment) ?

@nicolo-ribaudo
Copy link
Member Author

I don't think, there isn't a call to undefined in that code.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Mar 17, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: react outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

custom jsx transform replaces _jsxRuntime with undefined
5 participants