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.
jsxtakestrue, a runtime name ("preserve","classic","automatic") or{ runtime, importSource, pragma, pragmaFrag }. The top-levelpragmaandpragmaFragfrom #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 aTypeError, as does an unknown field (so oxc'sdevelopmentorpurefails loudly). - The automatic runtime matches
ts.transpileModulewithjsx: "react-jsx":_jsxor_jsxs,childrenin the props,keyas the third argument,_createElementfrom the import source for akeyafter a spread. Every line is preserved.@jsxRuntimeand@jsxImportSourcecomments 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 akeyafter 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.mjsruns each case through erase mode and downlevel mode's three runtimes, whole and chunked. The automatic runtime is judged againstjsx: "react-jsx", with its imports compared as a hoisted set. Preserve is judged against ts-blank-space, sincetranspileModule'spreserveprinter writeskey={}askey=. 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.tsxsuites: 333/333. The 1,603-file TSX sample and all 9,368.tsxfiles 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
_jsxto_jsx_1when the file declares_jsx; this doesn't.jsx: "react-jsxdev"is not supported.
Stacked on #2.