Repository navigation
fix(core): defer CommonJS source to the CJS loader instead of transforming twice - #822
Merged
Merged
Conversation
…rming twice On the module.registerHooks() path, the synchronous default load reads CommonJS files and hands their source to the load hook, which transformed them, discarded the output, and let the pirates hook transform them again. transform_output now defers commonjs results to the CommonJS machinery only when the source is byte-identical to the file on disk (what defaultLoadSync produces); hook-supplied or fileless-URL source is kept and transformed. load returns source:null only when oxcLoad produced none. Also treat module-typescript results as ES modules when transforming. Fixes #820
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Brooooooklyn
added a commit
that referenced
this pull request
Oct 12, 2026
…dules (#826) ## Summary Fixes #821. Adds an option to transform TypeScript dependencies under `node_modules` without `OXC_TRANSFORM_ALL`'s blanket coverage, on both module systems: - **`OXC_TRANSFORM_ALL=typescript`** — transforms `.ts`/`.mts`/`.cts`/`.tsx` dependencies; `node_modules` `.js` stays untouched. Existing grammar is preserved: absent, empty, `0`, `false` mean off; any other truthy value still means all. - **`setTransformDeps("typescript" | "all" | null)` + `getTransformDeps()` + `transformsDep(path)`** — the API a wrapper (e.g. Vite+) can call instead, so the choice never enters `process.env` and the user's script or child processes. A set mode takes precedence over the variable. The mode latches on the first transform or resolve — or at `register()` itself on the `module.register()` path, whose worker thread needs the value as a snapshot — and a later call with a different mode throws. Reproduction also showed the issue's claimed escape hatch does not cover `.ts` dependencies: `transformable_dependency()` in `create_resolve` deferred `.ts` under `node_modules` to Node's own type stripping, which rejects it, so `OXC_TRANSFORM_ALL=1` could not run such a file at all. Both the env mode and `OXC_TRANSFORM_ALL=1` now give TypeScript dependencies a real format so the loader transforms them. On the CommonJS side the `pirates` hook disables `ignoreNodeModules` and asks the binding per file (`transformsDep`), so `require()` of a TypeScript dependency follows the same mode and a late `setTransformDeps` is honored there up to the latch. Known caveat, documented in the README: an older `@oxc-node/core` copy registered in the same process reads `OXC_TRANSFORM_ALL=typescript` as transform-all (every non-`0`/`false` value meant that). `setTransformDeps` is invisible to older copies and avoids the skew entirely. This PR is stacked on #822, whose `transform_output`/`register.mjs` changes this builds on; the diff will shrink once that merges. ## Test plan - New integration specs in `integrate-vitest` cover the env mode, the setter API (including late-call rejection), `.js` deps staying untouched, and the `require()` path — `pnpm test` / `vp check` pass. - Manually verified on Node 24.21 (`module.register()`) and 26.11 (`registerHooks()`): a `node_modules` dep exporting `.ts` fails with `ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING` by default, imports fine under `OXC_TRANSFORM_ALL=typescript` or `setTransformDeps("typescript")`, and a decorator-using `.js` dep under `typescript` mode still runs through Node untouched.
Brooooooklyn
added a commit
that referenced
this pull request
Oct 13, 2026
…again (#830) ## Summary Fixes #827. Since #822, a file in a CommonJS package that uses ES module syntax was read from disk twice more before being transformed on the `registerHooks()` path: once by `source_matches_file`'s byte compare and once by `load_commonjs_esm`'s syntax check — yet the answer for such a file only depends on the source `defaultLoadSync` already handed the hook. `load_commonjs_esm` and `reject_cts_module_syntax` now take an `Option<&str>` and only touch the disk when the caller has no source. `transform_output` checks the in-hand source's syntax first and runs `source_matches_file` only for a file that stays CommonJS: - A CJS-package file with ESM syntax costs **zero** extra reads, as in v0.1.6. - A file that stays CommonJS costs **one** extra read (the compare) instead of two. - `.cts` still compares bytes before the syntax check, so a hook-supplied `.cts` source keeps the error ordering it had before this reorder. - Non-UTF-8 sources still reach the "Not UTF-8" deferral untouched. - As the issue notes, a hook-supplied source that flips now goes through `load_commonjs_esm`'s `Module::Esm` transform (matching the `module.register()` path) instead of `Preserve`; documented in the block comment, along with the parse-twice cost for the narrow differing-source-stays-CJS case. ## Test plan - DYLD `open()` interpose (repro script) went from 3 opens per ESM-syntax `.ts` file to 1 on Node 26.11 — parity with the `type: "module"` control. - `.cts` with `export` still rejected with the same error; `.cts` CommonJS entry still defers; `data:`/custom-scheme sources still keep their source. - `pnpm test`, `vp check`, `cargo clippy` pass; fixtures verified on Node 24.21 (`module.register()`) and 26.11 (`registerHooks()`).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #820.
On the
module.registerHooks()path (Node.js ≥ 26.2), the synchronous default load reads every CommonJS file and hands its source to theloadhook.oxcLoadran the full transform on it, the hook then discarded the result by returningsource: null, and thepirateshook transformed the file a second time when the CommonJS loader read it.The fix defers to the CommonJS machinery inside
transform_outputonly when the result's source is byte-identical to the file on disk — exactly whatdefaultLoadSyncproduces forfile:URLs. Acommonjsresult whose source differs (another hook supplied it, or the URL has no file —data:, custom schemes) keeps its source and is transformed and honored, which the previous behavior discarded unconditionally.register.mjs'sloadnow returnssource: nullonly whenoxcLoadproduced none itself, so hook-supplied source survives back to Node.Also treats Node's
module-typescriptformat (a.tsfile Node strips and runs as an ES module) as ESM intransform_outputinstead of parsing it in script mode with the top-level-await rewrite.packages/core/index.jsis regenerated binding-stub churn from rebuilding the native binding.Verification
.tsentry,import/import()of.cts/.ts/.jsin a CommonJS package, JSON imports, a CJS-package file that flips to an ES module, and a.ctswith module syntax (still rejected). Each CJS file now logs "Source is the file itself, deferring to the CommonJS loader" and is transformed once bypirates; output identical to before.pnpm testandvp checkpass; themodule.register()fallback on Node 24 is unchanged.