fix: avoid Babel parsing raw HTML under Vite 8 bundledDev#518
Conversation
In Vite 8 with `experimental.bundledDev: true`, rolldown invokes filterless `transform` hooks on every module, including `index.html` — which reaches the plugin before `vite:build-html` converts it into a JS proxy. The plugin then forwards the raw HTML into `getCodeWithWebComponent`, where Babel `parse()` throws on HTML syntax. - `packages/vite/src/index.ts`: early-return for raw `.html` ids (no query) before calling `getCodeWithWebComponent`. - `packages/core/src/server/use-client.ts`: guard the inject path with `isJsTypeFile(file)` so `addImportToEntry`/`addNextEmptyElementToEntry` never feed non-JS sources into Babel.
Review Summary by QodoAvoid Babel parsing raw HTML under Vite 8 bundledDev
WalkthroughsDescription• Prevent Babel parsing raw HTML in Vite 8 bundledDev mode • Add early-return for .html files without query parameters • Guard inject path with isJsTypeFile() check • Avoid "Unexpected token" errors from Babel on HTML syntax Diagramflowchart LR
A["Vite 8 bundledDev<br/>transform hook"] -->|raw HTML| B["Check file type<br/>and query"]
B -->|.html no query| C["Early return<br/>skip processing"]
B -->|JS file| D["getCodeWithWebComponent"]
D -->|inject needed| E["isJsTypeFile guard"]
E -->|JS type| F["addImportToEntry<br/>Babel parse"]
E -->|non-JS| G["Skip injection<br/>avoid error"]
File Changes1. packages/vite/src/index.ts
|
Code Review by Qodo
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #518 +/- ##
=======================================
Coverage ? 99.97%
=======================================
Files ? 23
Lines ? 3693
Branches ? 984
=======================================
Hits ? 3692
Misses ? 1
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I can't reproduct your issue. Could you please share a repository? |
| // invokes filterless `transform` hooks on every module including `index.html` | ||
| // before `vite:build-html` converts it to a JS proxy. Passing raw HTML into | ||
| // `getCodeWithWebComponent` may trigger Babel `parse()` on HTML and throw. | ||
| if (filePath.endsWith('.html') && !query) { |
There was a problem hiding this comment.
.html has already been excluded in isTargetFileToInject
There was a problem hiding this comment.
In the Vite 8 bundledDev, the logic seems not to be like that; without this, it indeed goes to the HTML parse.
|
我之前误解了你的意思。实际上这个问题应该通过 consumer 侧的 |
|
之前误解了你的意思,抱歉。这个问题的根因是 Vite 8 的 |
Summary
experimental.bundledDev: trueis enabled in Vite 8, rolldown invokes filterlesstransformhooks on every module, including the rawindex.htmlentry — beforevite:build-htmlconverts it into a JS proxy.transformhook has no id filter, so raw HTML reachesgetCodeWithWebComponent, where Babelparse()is invoked on the.htmlsource and throwsSyntaxError: Unexpected token.Fix
packages/vite/src/index.ts: early-return for raw.htmlids (no query) before callinggetCodeWithWebComponent.packages/core/src/server/use-client.ts: guard the inject path withisJsTypeFile(file)soaddImportToEntry/addNextEmptyElementToEntrynever feed non-JS sources into Babel.Test plan
experimental.bundledDev: true+codeInspectorPlugin. Without the fix, dev server throwsBuild error ... Unexpected tokenwhile transformingindex.html. With the fix, the server boots and the bundled output contains correctdata-inspector-pathattributes pointing to the source.tsxfiles (e.g.src/App.tsx:7:5:div).bundledDev: false).