vite icon indicating copy to clipboard operation
vite copied to clipboard

React-Plugin does not respect the jsxImportSource from TSConfig

Open seivan opened this issue 1 year ago • 3 comments

Description

A user shouldn't have to define jsxImportSource twice, if it's in a TSConfig then it's already defined. ESBuild (and hopefully Vite) already respects that as far as I know for: jsxFactory jsxFragmentFactory

I think the plugin should do that for jsxImportSource as well.

Suggested solution

Read TSConfig.compilerOptions.jsxImportSource ?? options?.jsxImportSource ?? "react"

Alternative

Building a shim for the auto-import and let ESBuild the whole thing instead of using the plugin.

Additional context

No response

Validations

seivan avatar Jul 07 '22 22:07 seivan

Notes:

  • [ ] TSConfing "jsx": "react" is opt out of automatic runtime.
  • [ ] TSConfig defined "jsxImportSource": "react" is default to current settings.
  • [ ] Anything else, read pass on as is.

Did a go at it.

const jsxImportSrc = tsBuildConfig.compilerOptions.jsxImportSource
const jsxOption = tsBuildConfig.compilerOptions.jsx === "react"
  ? {}
  : jsxImportSrc === "react"
    ? {}
    : { jsxRuntime: "automatic" as const, jsxImportSrc, }


ReactPlugin({ ...jsxOption, })

Worked perfectly with others, like @emotion/react

    "jsx": "react-jsx",
    "jsxImportSource": "@emotion/react",

and

    "jsx": "react-jsx",
    "jsxImportSource": "react",

seivan avatar Jul 09 '22 21:07 seivan

Does esbuild support jsxImportSource? According to https://esbuild.github.io/content-types/#tsconfig-json only a few fields are supported which Vite passes. Maybe this is related to plugin-react and it's babel transform, but regardless it would be great if you can submit the PR so it's easier to view the proposed changes.

bluwy avatar Jul 10 '22 14:07 bluwy

All the heavy lifting is already done by the React plugin, this is purely a convenience feature. Making sure that we don't repeat configs and keep one single source of truth.

seivan avatar Jul 10 '22 14:07 seivan

Yes, esbuild does support jsxImportSource.

alex-kinokon avatar Aug 21 '22 09:08 alex-kinokon