Add the automatic JSX runtime, and shape the jsx option after oxc's #3

Closed natemoo.re
targeting bench-tsx from jsx-automatic
Closed opened by natemoo.re
targeting bench-tsx from jsx-automatic

Adds the automatic JSX runtime to downlevel mode, and reshapes the jsx option after oxc's JsxOptions, so a Vite or Rolldown oxc.jsx config carries over.

transformSync(src, { mode: "downlevel", jsx: true }); // automatic runtime, react/jsx-runtime
transformSync(src, { mode: "downlevel", jsx: "classic" });
transformSync(src, { mode: "downlevel", jsx: "preserve" }); // JSX kept, the rest down-leveled
transformSync(src, { mode: "downlevel", jsx: { runtime: "classic", pragma: "h", pragmaFrag: "Fragment" } });
transformSync(src, { mode: "downlevel", jsx: { importSource: "preact" } });

Behavior #

  • Options. jsx takes true, a runtime name ("preserve", "classic", "automatic") or { runtime, importSource, pragma, pragmaFrag }. The top-level pragma and pragmaFrag from #1 move into the object; #1 is unreleased, so no released API changes. Downlevel mode defaults to "automatic", as oxc and Babel 8 do. Erase mode only keeps JSX, and any other runtime there throws a TypeError, as does an unknown field (so oxc's development or pure fails loudly).
  • The automatic runtime matches ts.transpileModule with jsx: "react-jsx": _jsx or _jsxs, children in the props, key as the third argument, _createElement from the import source for a key after a spread. Every line is preserved. @jsxRuntime and @jsxImportSource comments are honored, last one wins, as TypeScript does.
  • runtime: "preserve" in downlevel mode keeps JSX exactly as erase mode does, for pipelines that transform it later.

Engine #

The automatic runtime decides three things after the output they change. Each uses machinery downlevel mode already has:

  • Callee. An element pushes an anchor, which holds its output back from the host, and writes its tag with no callee. A second child (or a spread child) inserts _jsxs( and [; the element's end, or a key after a spread, inserts the callee otherwise. An element holds output only until its second child starts.
  • Key. Written in place under the held anchor, cut onto a stack, and written back after the props. A key written across lines is moved onto one line, and its // comments become /* */.
  • Imports. Written at the end of the file, for only the names used. Imports are hoisted, so the module's bindings are the same; the runtime evaluates after the file's other imports rather than first, and nothing waits for the whole file.

Every anchor push and pop, and every key cut, happens after the step's lookahead commits, so a retried step never repeats one. ABI: init bit 2 selects the automatic runtime and bit 3 preserve; pragma gains the import source's length.

Cost #

#1 this PR
erase.wasm gzip 13,648 B 13,666 B
downlevel.wasm gzip 22,205 B 23,537 B

TSX to the automatic runtime runs at 67.4 MB/s, 1.35× oxc-transform's 50.0, and the classic runtime's throughput is unchanged.

Verification #

  • test/jsx.mjs runs each case through erase mode and downlevel mode's three runtimes, whole and chunked. The automatic runtime is judged against jsx: "react-jsx", with its imports compared as a hoisted set. Preserve is judged against ts-blank-space, since transpileModule's preserve printer writes key={} as key=. The self-test proves the judge rejects a wrong callee, a key left in the props, a one-child array and a wrong import source.
  • corpus/jsx.jsonl: 65/65, with 14 new automatic-runtime cases. TypeScript's .tsx suites: 333/333. The 1,603-file TSX sample and all 9,368 .tsx files in Sentry and Excalidraw pass.

Open questions #

  • Imports at the end change module evaluation order relative to TypeScript. Is that acceptable, or should the output wait for the whole file?
  • TypeScript renames _jsx to _jsx_1 when the file declares _jsx; this doesn't. jsx: "react-jsxdev" is not supported.

Stacked on #2.

Commits 3

Benchmark TSX to the automatic JSX runtime
Reshape the jsx option after oxc's and add the automatic runtime to it
Write JSX for the automatic runtime in the downlevel engine
Sign up or Sign in to add to the discussion.