Vite 8 preamble error with transformWithOxc and bundledDev #21941
Replies: 4 comments
-
|
The preamble error happens because The issue is your Fix: let the React plugin handle JSX in .js files instead of doing it yourself.
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [
react({
include: /\.(jsx|tsx|js)$/,
bundledDev: true,
}),
dsv(),
],
});This tells the React plugin to also process If for some reason you need to keep the custom plugin, change the const transformJsxInJs = (): PluginOption => ({
name: 'transform-jsx-in-js',
async transform(code, id) {
if (!id.match(/.*\.js$/)) return null;
const result = await transformWithOxc(code, id, {
lang: 'jsx',
jsx: { runtime: 'classic' }, // avoid conflict with automatic runtime preamble
});
return result ? { code: result.code, map: result.map } : null;
},
});But the first approach (expanding |
Beta Was this translation helpful? Give feedback.
-
|
Already tried that include, same error: |
Beta Was this translation helpful? Give feedback.
-
|
The root issue is the custom That means JSX is already transformed before React plugin handles dev runtime injection, which causes: @vitejs/plugin-react can't detect preamble. Something is wrong.And after adding Plugin: vite:oxc
Help: JSX syntax is disabled and should be enabled via the parser optionsRecommended fixRemove the custom import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import dsv from "@rollup/plugin-dsv";
export default defineConfig({
esbuild: {
loader: "jsx",
include: /src\/.*\.js$/,
exclude: [],
},
optimizeDeps: {
esbuildOptions: {
loader: {
".js": "jsx",
},
},
},
plugins: [
react({
include: /\.(js|jsx|ts|tsx)$/,
bundledDev: true,
}),
dsv(),
],
});Why this works
For Vite 8, I would avoid |
Beta Was this translation helpful? Give feedback.
-
|
Nothing was working, so I renamed all 400 js react files to jsx, removed the transformer and still getting the same error. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In our project we still have a lot of old js files with react components, so we use a transform plugin adapted from a working solution in vite 7:
Enabling bundledDev worked great for a few Vite 8 betas but now I get this preamble errors:
InformationCircleIcon.tsx:37 Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong.Using:
"@rollup/plugin-dsv": "3.0.5"
"@vitejs/plugin-react": "6.0.1"
"vite": "8.0.1"
Disabling bundledDev fixes the issue, but we have thousands of files and this was the feature I was most eager to test.
Beta Was this translation helpful? Give feedback.
All reactions