Skip to content

fix(react): respect tsconfig jsxImportSource (fix #1448) - #1450

Open
cpruijsen wants to merge 1 commit into
vitejs:mainfrom
cpruijsen:fix/issue-1448
Open

fix(react): respect tsconfig jsxImportSource (fix #1448)#1450
cpruijsen wants to merge 1 commit into
vitejs:mainfrom
cpruijsen:fix/issue-1448

Conversation

@cpruijsen

Copy link
Copy Markdown

Description

Fixes #1448.

react({ compiler: true }) lowers JSX in vite:react-compiler with oxc-transform-react. That transform defaults jsx.importSource to "react" when the option is unset. Vite's oxc transform never sees the original JSX, so the per-file tsconfig inference restored in #726 does not apply.

With no plugin-level jsxImportSource, the compiler transform now reads compilerOptions.jsxImportSource from the file's tsconfig using the same resolveTsconfig Vite uses for oxc JSX. An explicit jsxImportSource option still overrides every file.

Reproduction from the issue (two TSX files, no pragmas):

Plugin options file with jsxImportSource: "@emotion/react" default React file
{ compiler: false } @emotion/react/jsx-runtime react/jsx-runtime
{ compiler: true } (before) react/jsx-runtime react/jsx-runtime
{ compiler: true } (after) @emotion/react/jsx-runtime react/jsx-runtime
{ compiler: true, jsxImportSource: "@emotion/react" } @emotion/react/jsx-runtime @emotion/react/jsx-runtime

What I chose: fill jsx.importSource from the file's tsconfig inside the compiler plugin.

Alternative: set jsx: "preserve" in the compiler plugin so Vite's oxc transform keeps owning JSX, Fast Refresh, and tsconfig inference.

Why: #1419 put JSX and Fast Refresh in the compiler plugin so the compiler sees the original AST. This change only supplies the missing import source and stays reversible. Happy to switch to preserve-JSX if that is preferred.

resolveTsconfig is a hidden rolldown export currently used by Vite. It is loaded through Vite's installation so this package does not gain a new runtime dependency. If you would rather have a public Vite helper, this call site is the consumer.

Checklist

  • Read the Contributing Guidelines.
  • Check that there isn't already a PR that solves the problem the same way.
  • Documentation already states that jsxImportSource is inferred from tsconfig; no extra docs.
  • Tests fail without this change and pass with it (packages/plugin-react unit tests).

The native compiler lowered JSX before Vite could apply per-file
tsconfig inference. Use the same tsconfig resolver as Vite's oxc
transform so compiler: true preserves jsxImportSource.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

react({ compiler: true }) ignores tsconfig jsxImportSource

1 participant