feat: allow react/jsx-dev-runtime imports#7246
feat: allow react/jsx-dev-runtime imports#7246aleclarson wants to merge 3 commits intovitejs:mainfrom
react/jsx-dev-runtime imports#7246Conversation
bluwy
left a comment
There was a problem hiding this comment.
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.
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.
Linking to the PR opened after this comment since GitHub didn't link them #7673.
There was a problem hiding this comment.
@aleclarson is this PR still valid given @bluwy's message above?
There was a problem hiding this comment.
Perhaps we can optimize
react/jsx-runtimeandreact/jsx-dev-runtimedirectly?
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.
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.
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.
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.
It already works that way :)
Of course, the virtual module complicates it a little more.
There was a problem hiding this comment.
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-runtimeandreact/jsx-dev-runtimeare now resolved with the same module ID and globally deduplicatedreact/jsx-runtime.jsmodule (which usesprocess.env.NODE_ENVchecking) in favor of manual resolution using theisProductionlocal variablereact/cjs/react-jsx-dev-runtime.development.jsandreact/cjs/react-jsx-runtime.production.min.jsare processed by Vite's optimizerAdditional context
Fixes #6215
What is the purpose of this pull request?