Repository navigation
fix(api): load CommonJS route dependencies with native require in the compiled binary - #5484
Conversation
… compiled binary The compiled binary rewrote CommonJS dependency imports with regexes into calls to __vf_interopDefault, a helper the injected shim never exported, and loaded dynamic imports of CommonJS packages such as pg as ES modules. Both failed at runtime. Each CommonJS dependency a route imports now bundles as a one-line module that loads it through the runtime's createRequire, so esbuild's interop gives default, named, namespace and dynamic imports their usual meaning. ES module dependencies keep loading from file:// URLs. The require shim resolves with createRequire and keeps route-level requires inside the project root by realpath. The regex rewrite and its helpers are removed.
Codex Review Summary: ✅ CompletedThis 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. |
📦 Client bundle boundary
A server module in a client graph aborts hydration in the browser. New leaks fail CI; known leaks are tracked in |
|
Score: 82/100. This replaces the hand-rolled CJS loader in compiled binaries with the runtime's own
No blockers. I'd like the containment-scope decision confirmed before merge. I did not run the test suite in this review. Generated by Claude Code |
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
|



Summary
In a compiled binary, API routes now load the project's CommonJS dependencies with the runtime's own
createRequire. Two bugs on main go away:__vf_interopDefault is not defined: the regex rewrite emitted calls to a helper the injected shim never exported.require is not definedforimport("pg"): dynamic imports of CommonJS packages loaded as ES modules.Each CommonJS dependency a route imports (bare name or subpath) now bundles as a one-line module,
module.exports = __vf_native_require("<spec>"). esbuild's interop then gives default, named, namespace and dynamic imports their usual meaning. The dependency's own files are not bundled or rewritten. ES module dependencies still load fromfile://URLs. Node, Bun and the Deno source run keep their existing paths.The compiled-binary require shim now resolves with
createRequireand keeps route-level requires inside the project root by realpath. The regex CommonJS rewrite,__vf_interopDefaultandtoCjsDestructureBindingsare removed.This replaces #5477, which reimplemented Node's CommonJS loader.
createRequireloads CommonJS from disk in Deno 2.9deno compilebinaries.Closes #5471
Tests
loader.test.ts: a bundle built withcreateNativeCommonJSPluginand the emitted shim runs default, named, namespace, subpath and dynamic imports of a CommonJS package, and the package source is not in the bundle.loader.test.ts: the emitted shim loads a bare package and refuses real files outside the project (absolute path, prefix-sibling directory,..escape, symlinked entry).compiled-binary-e2e.suite.ts: an API route imports a pg-shaped CommonJS package (nested dependency, built-inevents, instance default export, subpath, dynamic import). Passed against a locally built binary.pg@8.13.1checked by hand in a locally built binary: static, named, namespace and dynamic imports work, and a query to a closed port returnsECONNREFUSED.