Babel plugins that read AST `loc` produce wrong positions when running after OXC

未关闭
#1,193 12 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
babel, react, typescript

调研方向

从链接的 vite-babel-order-repro 开始,跟踪 @vitejs/plugin-react 的 OXC 转换以及 @rolldown/plugin-babel 的后处理路径。比较 pre 和 post 排序下的 Babel AST loc 值与 OXC jsxDEV 源信息。完成标准是自定义 Babel 插件能够接收到原始源位置,同时不改变 OXC 的位置。

由索引模型根据 Issue 内容生成。

描述

documentation plugin: react
Related plugins
Describe the bug

When a Babel plugin runs after @vitejs/plugin-react's OXC transform (using @rolldown/plugin-babel with enforce: 'post' or a custom plugin), the Babel AST loc values refer to the OXC-transformed code, not the original source. Any Babel plugin that reads loc.start.line / loc.start.column to embed source positions in the output will produce incorrect values.

This was not an issue with @vitejs/plugin-react v5 where Babel handled both JSX transform and custom plugins in a single pass — loc always pointed to the original source.

Example

We have a Babel plugin that inserts ComponentName.__debugSourceDefine = { fileName, lineNumber, columnNumber } after each React function component, using the function body's loc:

FunctionDeclaration(path, state) {
  const name = path.node.id.name;
  const loc = path.node.body.loc;  // ← wrong after OXC transform
  // inserts: Foo.__debugSourceDefine = { fileName: "...", lineNumber: loc.start.line, ... }
  path.insertAfter(createDebugInfo(name, state.file.opts.filename, loc));
}

With v5 (react({ babel: { plugins: [myPlugin] } })), loc.start.line referred to the original .tsx file. With v6, OXC transforms JSX first, changing line numbers. Babel sees the transformed code and its loc values are offset.

The dilemma — no configuration produces correct results

With enforce: 'pre' (default) — Babel runs before OXC:

  • ✅ __debugSourceDefine line numbers are correct (Babel sees original source)
  • ❌ OXC's jsxDEV() source info is wrong — every JSX element in the app gets shifted line numbers because Babel inserted __debugSourceDefine blocks (4+ lines each) before OXC processed the file

With enforce: 'post' — Babel runs after OXC:

  • ❌ __debugSourceDefine line numbers are wrong (Babel AST loc refers to OXC-transformed code, not the original source)
  • ✅ OXC's jsxDEV() source info is correct (OXC saw the original source)
Reproduction

https://github.com/Artur-/vite-babel-order-repro

Steps to reproduce
npm install
npx vite

Open in browser — the page automatically verifies source positions.

System Info
System:
    OS: macOS 26.3.1
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 24.14.0
    npm: 11.9.0
Used Package Manager

npm

Logs

No response

Validations
主要语言
TypeScript
星标
1.2k
派生
287
平均合并
1 天 17 小时
30 天内合并 PR
19

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

vitejs/vite-plugin-react 的其他 Issue

查看 vitejs/vite-plugin-react 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。