-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Improve auto-import for JSX (the jsxInject
option)
#2369
Comments
jsxInject
option)
The dev-server of |
Considering the following snippet:
Why not just make jsxInject a function that you pass result.code into? What I'm proposing is something like: {
esbuild: {
jsxInject: (str: string) => !str.includes('import React') ? "import React from 'react'" : ""
}
} The refactoring changes would be minimal, the old way of directly passing a string can still be supported and it adds more flexibility to the configuration IMO. |
Now that |
@aleclarson I notice it use babel to transform jsx, how about performance? |
I guess I should make the readme clearer. It only uses Babel when |
@aleclarson That's Cool! I will try it. |
**Motivation:** The comment [here](vitejs#2369 (comment)) The mentioned plugin is really helpful (and is maintained by a member of the Vite team) and could help users finding a (better ?) solution instead of the proposed way of just injecting the `import` statement in `jsxInject` which seems hacky.
Whatever the way to go forward is, please don't forget |
The following workaround seems to work for me. // vite.config.js
export default {
esbuild: {
jsxFactory: '_jsx',
jsxFragment: '_jsxFragment',
jsxInject: `import { createElement as _jsx, Fragment as _jsxFragment } from 'react'`,
}
} |
Nice seeing you here, thought you're stuck with Nette :D Anyways, yeah, this works for me too. |
See: https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html For the moment, the best way to support this in Vite (`esbuild`, really) is via this configuration: vitejs/vite#2369 (comment) so that's what we use. There's a Vite plugin that addresses this here: https://github.com/alloc/vite-react-jsx. However, that plugin has a performance impact on production builds because has to use Babel, The simpler `jsxInject` workaround described here: vitejs/vite#2369 is not quite as foolproof as the workaround we're using.
closing at the new react plugin improved auto import. |
Is your feature request related to a problem? Please describe.
According to the document of Vite, we can do an
auto-import
for JSX by:But it just places this snippet to the header of codes by simply concatenating string:
vite/packages/vite/src/node/plugins/esbuild.ts
Line 125 in a0d922e
If some codes have already import React for something like:
that will cause conflict and throw an error:
Uncaught SyntaxError: Identifier 'React' has already been declared
.I know some codemod can migrate the React imports, but I don't want to change that codes for some reason.
Describe the solution you'd like
According to the document of ESBuild:
First, create a file called react-shim.js that re-exports everything from the react package in an export called React:
Then use esbuild's inject feature to inject this into each file:
In my test, I create a file with:
Then use esbuild's inject feature by
--inject:./react-shim.js
, esbuild will output:Although there are duplicate variables
React
, but at least this is compatible with the old code.So I hope
vite
can provideinject
config ofesbuild
.Describe alternatives you've considered
Or handle duplicated import by
vite
itself.Additional context
Some
codemod
can migrate the React importing:https://github.com/reactjs/react-codemod#update-react-imports
The text was updated successfully, but these errors were encountered: