You can change esbuild configuration to treat all js files as jsx with the loader
option:
// vite.config.ts
import {defineConfig} from 'vite'
// https://vitejs.dev/config/
export default defineConfig(() => ({
esbuild: {
loader: "tsx", // OR "jsx"
include: [
// Add this for business-as-usual behaviour for .jsx and .tsx files
"src/**/*.jsx",
"src/**/*.tsx",
"node_modules/**/*.jsx",
"node_modules/**/*.tsx",
// Add the specific files you want to allow JSX syntax in
"src/LocalJsxInJsComponent.js",
"node_modules/bad-jsx-in-js-component/index.js",
"node_modules/bad-jsx-in-js-component/js/BadJSXinJS.js",
"node_modules/bad-jsx-in-js-component/ts/index.ts",
"node_modules/bad-jsx-in-js-component/ts/BadTSXinTS.ts",
// --- OR ---
// Add these lines to allow all .js files to contain JSX
"src/**/*.js",
"node_modules/**/*.js",
// Add these lines to allow all .ts files to contain JSX
"src/**/*.ts",
"node_modules/**/*.ts",
],
},
}));
Note: there is a performance penalty for loading .js files with the .jsx loader.
Answer taken from this discussion in Vite's GitHub, Which marks the incorrect (and older) answer as the "correct" one.
Update March 2023
The original answer did not work correctly for vite build
, only for vite dev
. The current version works for both with vite@^4.0.0
Here is an example repo you can clone and test the solution.