Ways to configure Vite to support using JSX syntax in JS files
P粉571233520
2023-08-24 19:55:15
<p>Vite does not allow the use of JSX syntax in .js files by default. </p>
<p>I have renamed my files to <code>.jsx</code> (or <code>.tsx</code>), but I have some external dependencies that cannot be renamed. </p>
<p>Vite error example:</p>
<pre class="brush:php;toolbar:false;">✘ [ERROR] JSX syntax extension is not currently enabled
node_modules/somelib/src/someFile.js:122:11:
122 │ return <div/></pre>
<p>How to configure Vite to support JSX expressions in all .js files? </p>
You can change the esbuild configuration by treating all js files as jsx using the
loader
option:Note: Using the .jsx loader to load .js files will cause performance losses.
The answer comes from this discussion on Vite's GitHub, marking the incorrect (old) answer as "correct".
Updated in March 2023
Original answer does not work properly in
vite build
, only invite dev
. The current version works with both invite@^4.0.0
.A sample repository where you can clone and test your solution.