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

オープン
#1,193 コメント 12 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
babel, react, typescript

調査の方向性

リンクされている vite-babel-order-repro から始め、@vitejs/plugin-react の OXC transform と @rolldown/plugin-babel の post-processing path を追跡します。pre と post の ordering における Babel AST の loc 値と OXC の jsxDEV source information を比較します。カスタム Babel plugins が元のソース位置を受け取り、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時間
マージ済み PR(30日)
19

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

vitejs/vite-plugin-react のほかの issue

vitejs/vite-plugin-react の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。