question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve auto-import for JSX (the `jsxInject` option)

See original GitHub issue

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:

// vite.config.js
export default {
  esbuild: {
    jsxInject: `import React from 'react'`
  }
}

But it just places this snippet to the header of codes by simply concatenating string:

https://github.com/vitejs/vite/blob/a0d922e7d9790f998c246f8122bc339717b6088f/packages/vite/src/node/plugins/esbuild.ts#L125

If some codes have already import React for something like:

import React from 'react';

export default function Button(props: React.ButtonHTMLAttributes<HTMLElement>) {
    return <div {...props} />
}

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:

// react-shim.js
export * as React from 'react'

Then use esbuild’s inject feature to inject this into each file:

esbuild app.jsx --bundle --inject:./react-shim.js --outfile=out.js

In my test, I create a file with:

import React from 'react';

export default function Test() {
    return <div>Test</div>
}

Then use esbuild’s inject feature by --inject:./react-shim.js, esbuild will output:

import * as React from "react";
import React2 from "react";
export default function Test() {
  return /* @__PURE__ */ React2.createElement("div", null, "Test");
}

Although there are duplicate variables React, but at least this is compatible with the old code.

So I hope vite can provide inject config of esbuild.

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
aleclarsoncommented, May 23, 2021

Now that vite-react-jsx exists, the esbuild.jsxInject option will probably be deprecated.

4reactions
VottusCodecommented, May 22, 2021

Considering the following snippet: https://github.com/vitejs/vite/blob/a0d922e7d9790f998c246f8122bc339717b6088f/packages/vite/src/node/plugins/esbuild.ts#L125

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Features | Vite
You can inject the JSX helpers using jsxInject (which is a Vite-only option) to avoid manual imports: js // vite.config.js import { defineConfig...
Read more >
Auto Import of React Components in Visual Studio Code [closed]
Missing modules will show up with a Code Action (AKA "Quick Fix") with an option to import. You can click on the lightbulb...
Read more >
vite-plugin-jsx-inject - npm
A workaround for vite.config.js#esbuild.jsxInject option, which is going to deprecated in the future. // vite.config.js export ...
Read more >
API - ESBuild
The transform API can take the following options: Simple options: ... For example, you can auto-import the react package to provide functions such...
Read more >
Auto Import - ES6, TS, JSX, TSX - Visual Studio Marketplace
1.2.2. Fix for imports not being merged. 1.2.1. Added optional auto completion for all known imports ( enabled by default ). Improved scanning ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found