-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
feat: allow react/jsx-dev-runtime
imports
#7246
Conversation
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.
Looks like the lockfile needs an update for the CI to pass. Code lgtm though.
packages/plugin-react/src/index.ts
Outdated
const devEntry = 'react/cjs/react-jsx-dev-runtime.development.js' | ||
const prodEntry = 'react/cjs/react-jsx-runtime.production.min.js' |
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.
With react 18.0.0, it has an exports field that is blocking importing this path, so I'm not sure this works anymore. Perhaps we can optimize react/jsx-runtime
and react/jsx-dev-runtime
directly? Since the process.env.NODE_ENV
should be available in prebundling too.
I also noticed a bug with this Vite code 🤔
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.
Linking to the PR opened after this comment since GitHub didn't link them #7673.
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.
@aleclarson is this PR still valid given @bluwy's message above?
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.
Perhaps we can optimize
react/jsx-runtime
andreact/jsx-dev-runtime
directly?
Can't do that because we redirect them to a virtual module:
https://github.com/aleclarson/vite/blob/1069536cb481a8c1c7835d75b0ebeae840fe9bd0/packages/plugin-react/src/index.ts#L374-L378
I think resolving devEntry
and prodEntry
to absolute paths should work, since that will sidestep the Node.js resolution algorithm (where pkg.exports
is enforced).
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.
Is there a reason we need to provide a virtual module for them? I tested optimizing react/jsx-runtime
locally, and I notice that esbuild builds the final output as
var react_jsx_runtime_default = require_jsx_runtime();
export {
react_jsx_runtime_default as default
};
which breaks named imports, but Vite handles that with the needsInterop
flag so it should still work. Unless plugin-react is handling more too?
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 motivation is to reduce production bundle size, and we use the same method in development to avoid any production-only surprises.
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.
I see. It feels weird to resolve to a different module in dev/prod since jsx-dev-runtime
and jsx-runtime
have different exports. Not sure how often that is an issue though.
I guess this is fine then. I also wonder if it's simpler then to use resolveId
to resolve react/jsx-dev-runtime
=> react/jsx-runtime
in prod; vice-versa in dev. But either ways they should work the same.
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.
It already works that way :)
Of course, the virtual module complicates it a little more.
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 my concern is that the virtual module trick adds another layer of complexity, and it's tightly coupled to the named exports. I'll see if the resolveId
flow I mentioned above works.
Not sure how those failing tests could possibly be related to this PR 🤔 |
Closing as we have now merged #8546, and test are still failing here. @aleclarson if you think we should continue to discuss the dedupe part of this PR, let's check it in a new PR against main. |
Description
This PR contains the following changes:
react/jsx-runtime
andreact/jsx-dev-runtime
are now resolved with the same module ID and globally deduplicatedreact/jsx-runtime.js
module (which usesprocess.env.NODE_ENV
checking) in favor of manual resolution using theisProduction
local variablereact/cjs/react-jsx-dev-runtime.development.js
andreact/cjs/react-jsx-runtime.production.min.js
are processed by Vite's optimizerAdditional context
Fixes #6215
What is the purpose of this pull request?