You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Specifying "jsxRuntime: classic" for a React vite app is not enough for "import React from 'react'" to be added to the source code in production build
#7586
Closed
7 tasks done
stevejay opened this issue
Apr 2, 2022
· 2 comments
· Fixed by #8676
I have created a vite app using the react-ts template. This adds the @vitejs/plugin-react plugin to the vite.config.ts file. By default the automatic jsx runtime is used by the plugin but it is possible to specify the classic jsx runtime, like so:
This should have the effect of adding import React from 'react'; to the start of any jsx/tsx file in the project, if it does not already import React. The file src/App.tsx does not import React but the import line does not get added to it when building the production version of the app (npm run build).
However, in a production build of the app (npm run build), it is possible for the subsequent shouldSkip condition to evaluate as true and for the function to return without doing anything with the updated value of code.
It seems that, in a production build of the vite app, it is only by also specifying in the @vitejs/plugin-react a babel configuration file or a list of babel plugins to apply that this condition will evaluate to false and the updated code value will be used.
Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
stevejay
changed the title
Specifying "jsxRuntime: classic" for a React vite app is not necessarily enough for "import React from 'react'" to be added to the source code
Specifying "jsxRuntime: classic" for a React vite app is not enough for "import React from 'react'" to be added to the source code in production build
Apr 3, 2022
+1, running into the same issue. Specifically, this happens ONY during vite build with the default mode of production. The issue cannot be reproduced with something like vite build --mode staging
Describe the bug
I have created a vite app using the react-ts template. This adds the
@vitejs/plugin-react
plugin to thevite.config.ts
file. By default the automatic jsx runtime is used by the plugin but it is possible to specify the classic jsx runtime, like so:This should have the effect of adding
import React from 'react';
to the start of any jsx/tsx file in the project, if it does not already importReact
. The filesrc/App.tsx
does not importReact
but the import line does not get added to it when building the production version of the app (npm run build
).It is on this line in packages/plugin-react/src/index.ts that the
import React from 'react';
line of code is added to the value ofcode
if required.However, in a production build of the app (
npm run build
), it is possible for the subsequent shouldSkip condition to evaluate astrue
and for the function to return without doing anything with the updated value ofcode
.It seems that, in a production build of the vite app, it is only by also specifying in the
@vitejs/plugin-react
a babel configuration file or a list of babel plugins to apply that this condition will evaluate tofalse
and the updatedcode
value will be used.Reproduction
https://github.com/stevejay/vite-jsxruntime-classic-repro
System Info
Used Package Manager
npm
Logs
Uncaught ReferenceError: React is not defined
Validations
The text was updated successfully, but these errors were encountered: