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

feat!: transform jsx with esbuild instead of babel #9590

Merged
merged 14 commits into from
Nov 8, 2022
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@typescript-eslint/parser": "^5.33.0",
"conventional-changelog-cli": "^2.2.2",
"cross-env": "^7.0.3",
"esbuild": "^0.14.47",
"esbuild": "^0.14.53",
"eslint": "^8.21.0",
"eslint-define-config": "^1.6.0",
"eslint-plugin-import": "^2.26.0",
Expand Down
37 changes: 17 additions & 20 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ export interface Options {
* @default "react"
*/
jsxImportSource?: string
/**
* Set this to `true` to annotate the JSX factory with `\/* @__PURE__ *\/`.
* This option is ignored when `jsxRuntime` is not `"automatic"`.
* @default true
*/
jsxPure?: boolean
rtsao marked this conversation as resolved.
Show resolved Hide resolved
/**
* Babel configuration applied in both dev and prod.
*/
Expand Down Expand Up @@ -122,13 +116,28 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
const viteBabel: Plugin = {
name: 'vite:react-babel',
enforce: 'pre',
config() {
config(_, { mode }) {
// Copied from https://github.com/vitejs/vite/blob/4e9bdd4fb3654a9d43917e1cb682d3d2bad25115/packages/vite/src/node/config.ts#L488-L490
const isProduction =
rtsao marked this conversation as resolved.
Show resolved Hide resolved
rtsao marked this conversation as resolved.
Show resolved Hide resolved
(process.env.NODE_ENV || process.env.VITE_USER_NODE_ENV || mode) ===
'production'

if (opts.jsxRuntime === 'classic') {
return {
esbuild: {
logOverride: {
'this-is-undefined-in-esm': 'silent'
}
},
jsx: 'transform',
jsxImportSource: opts.jsxImportSource
}
}
} else {
return {
esbuild: {
jsxDev: !isProduction,
jsx: 'automatic',
jsxImportSource: opts.jsxImportSource
}
}
}
Expand Down Expand Up @@ -240,18 +249,6 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
: [null, false]

if (isJSX || (ast = restoredAst)) {
plugins.push([
rtsao marked this conversation as resolved.
Show resolved Hide resolved
await loadPlugin(
'@babel/plugin-transform-react-jsx' +
(isProduction ? '' : '-development')
),
{
runtime: 'automatic',
importSource: opts.jsxImportSource,
pure: opts.jsxPure !== false
}
])

// Avoid inserting `import` statements into CJS modules.
if (isCommonJS) {
plugins.push(babelImportToRequire)
Expand Down
Loading