-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
feat!: transform jsx with esbuild instead of babel #9590
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
Changes from all commits
c96f6ed
c549d86
6844436
746f5b8
9fac441
e2ef295
a610b0f
6bed321
e3e4d99
74481c7
faa13a6
595af8c
ee396ba
3c97078
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import path from 'node:path' | ||
| import type { ParserOptions, TransformOptions, types as t } from '@babel/core' | ||
| import * as babel from '@babel/core' | ||
| import { createFilter, normalizePath } from 'vite' | ||
| import { createFilter } from 'vite' | ||
| import type { Plugin, PluginOption, ResolvedConfig } from 'vite' | ||
| import MagicString from 'magic-string' | ||
| import type { SourceMap } from 'magic-string' | ||
|
|
@@ -12,8 +11,6 @@ import { | |
| runtimeCode, | ||
| runtimePublicPath | ||
| } from './fast-refresh' | ||
| import { babelImportToRequire } from './jsx-runtime/babel-import-to-require' | ||
| import { restoreJSX } from './jsx-runtime/restore-jsx' | ||
|
|
||
| export interface Options { | ||
| include?: string | RegExp | Array<string | RegExp> | ||
|
|
@@ -40,11 +37,6 @@ export interface Options { | |
| * @default true | ||
| */ | ||
| jsxPure?: boolean | ||
| /** | ||
| * Toggles whether or not to throw an error if an XML namespaced tag name is used. | ||
| * @default true | ||
| */ | ||
| jsxThrowIfNamespace?: boolean | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw this option was added in #9571 This is not a breaking change as esbuild supports JSX namespaces: evanw/esbuild@71240d4 So Vite would not throw transforming JSX namespaces and thus this option is no longer needed |
||
| /** | ||
| * Babel configuration applied in both dev and prod. | ||
| */ | ||
|
|
@@ -100,7 +92,6 @@ const prependReactImportCode = "import React from 'react'; " | |
| export default function viteReact(opts: Options = {}): PluginOption[] { | ||
| // Provide default values for Rollup compat. | ||
| let devBase = '/' | ||
| let resolvedCacheDir: string | ||
| let filter = createFilter(opts.include, opts.exclude) | ||
| let needHiresSourcemap = false | ||
| let isProduction = true | ||
|
|
@@ -127,21 +118,37 @@ 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.
rtsao marked this conversation as 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, | ||
| jsxSideEffects: opts.jsxPure === false | ||
| } | ||
| } | ||
| } else { | ||
| return { | ||
| esbuild: { | ||
| jsxDev: !isProduction, | ||
| jsx: 'automatic', | ||
| jsxImportSource: opts.jsxImportSource, | ||
| jsxSideEffects: opts.jsxPure === false | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| configResolved(config) { | ||
| devBase = config.base | ||
| projectRoot = config.root | ||
| resolvedCacheDir = normalizePath(path.resolve(config.cacheDir)) | ||
| filter = createFilter(opts.include, opts.exclude, { | ||
| resolve: projectRoot | ||
| }) | ||
|
|
@@ -231,39 +238,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] { | |
| let ast: t.File | null | undefined | ||
| let prependReactImport = false | ||
| if (!isProjectFile || isJSX) { | ||
| if (useAutomaticRuntime) { | ||
| // By reverse-compiling "React.createElement" calls into JSX, | ||
| // React elements provided by dependencies will also use the | ||
| // automatic runtime! | ||
| // Avoid parsing the optimized react-dom since it will never | ||
| // contain compiled JSX and it's a pretty big file (800kb). | ||
| const isOptimizedReactDom = | ||
| id.startsWith(resolvedCacheDir) && id.includes('/react-dom.js') | ||
| const [restoredAst, isCommonJS] = | ||
| !isProjectFile && !isJSX && !isOptimizedReactDom | ||
| ? await restoreJSX(babel, code, id) | ||
| : [null, false] | ||
|
|
||
| if (isJSX || (ast = restoredAst)) { | ||
| plugins.push([ | ||
|
rtsao marked this conversation as resolved.
|
||
| await loadPlugin( | ||
| '@babel/plugin-transform-react-jsx' + | ||
| (isProduction ? '' : '-development') | ||
| ), | ||
| { | ||
| runtime: 'automatic', | ||
| importSource: opts.jsxImportSource, | ||
| pure: opts.jsxPure !== false, | ||
| throwIfNamespace: opts.jsxThrowIfNamespace | ||
| } | ||
| ]) | ||
|
|
||
| // Avoid inserting `import` statements into CJS modules. | ||
| if (isCommonJS) { | ||
| plugins.push(babelImportToRequire) | ||
| } | ||
| } | ||
| } else if (isProjectFile) { | ||
| if (!useAutomaticRuntime && isProjectFile) { | ||
| // These plugins are only needed for the classic runtime. | ||
| if (!isProduction) { | ||
| plugins.push( | ||
|
|
||
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.