Skip to content

fix(core): defer CommonJS source to the CJS loader instead of transforming twice - #822

Merged
Brooooooklyn merged 2 commits into
mainfrom
fix/820-cjs-double-transform
Oct 12, 2026
Merged

Brooooooklyn merged 2 commits into
mainfrom
fix/820-cjs-double-transform

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

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 the load hook. oxcLoad ran the full transform on it, the hook then discarded the result by returning source: null, and the pirates hook transformed the file a second time when the CommonJS loader read it.

The fix defers to the CommonJS machinery inside transform_output only when the result's source is byte-identical to the file on disk — exactly what defaultLoadSync produces for file: URLs. A commonjs result 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's load now returns source: null only when oxcLoad produced none itself, so hook-supplied source survives back to Node.

Also treats Node's module-typescript format (a .ts file Node strips and runs as an ES module) as ESM in transform_output instead of parsing it in script mode with the top-level-await rewrite.

packages/core/index.js is regenerated binding-stub churn from rebuilding the native binding.

Verification

  • Node 26.11.0: a CommonJS .ts entry, import/import() of .cts/.ts/.js in a CommonJS package, JSON imports, a CJS-package file that flips to an ES module, and a .cts with module syntax (still rejected). Each CJS file now logs "Source is the file itself, deferring to the CommonJS loader" and is transformed once by pirates; output identical to before.
  • pnpm test and vp check pass; the module.register() fallback on Node 24 is unchanged.

…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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-11T16:56:49.319597Z 699199e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Brooooooklyn
Brooooooklyn merged commit c61aea5 into main Oct 12, 2026
67 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/820-cjs-double-transform branch October 12, 2026 02:57
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()`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CommonJS modules are transformed twice on the registerHooks() path

1 participant