Repository navigation
fix(miniflare): resolve conditional exports and require() from CommonJS packages - #68
Conversation
…onJS packages
- add the `import`/`require` export condition (from `X-Resolve-Method`)
- redirect bare specifiers to the file's real path, so a package has one
instance and its relative imports and `require()` calls resolve from where
it lives
- serve `require("node:*")` through an ESM shim importing the built-in
- stop polyfilling `node:*` modules with an installed `unenv`: workerd loads
its own built-in or reports it missing, as in production
Closes #67
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (3)
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughThe Miniflare fallback resolver now applies import or require conditions to package resolution. It redirects Node built-in ChangesMiniflare module resolution
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~25 minutes Change: Bug fix · Severity of issue fixed: Medium Sequence Diagram(s)sequenceDiagram
participant Miniflare
participant FallbackService
participant Exsolve
participant Workerd
Miniflare->>FallbackService: Send specifier and import or require method
FallbackService->>Exsolve: Resolve bare package with method-specific conditions
Exsolve-->>FallbackService: Return resolved path
FallbackService-->>Miniflare: Redirect and preserve query string
Miniflare->>Workerd: Load redirected module
Suggested reviewers: Merge Risk: ⚪ Minimal · up to The reviewed resolution paths appear consistent with the intended behavior. No issue identified here needs to be fixed before merging. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to This changes which package code the local worker loads for imports and CommonJS requires. The review found no new public access path or confirmed security weakness, but some redirect behavior across runtime versions remains unverified. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches 💡 2📝 Generate docstrings 💡
⚔️ Resolve merge conflicts 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Apply the real-path redirect to paths a plugin's `resolveId` returns too, and reuse a redirect's resolution for workerd's re-request so plugins run once.
Fixes #67.
Conditional exports
exsolve only matches the conditions it's given, and the fallback passed just
exportConditions(["workerd", "worker"]). Withoutimport, resolving a dual package failed and fell back torequire.resolve, which picks Node'srequiretarget. The fallback now addsimportorrequirefrom workerd'sX-Resolve-Methodheader, so the issue's example resolves todefault.mjs(import) /default.cjs(require).require()from CommonJS innode_modulesBare specifiers were served under the importer's directory plus the package name (
src/deep/cjs-pkg), so the CommonJS file becamesrc/deep/index.cjs?__cjsand itsrequire()calls resolved from there. It also meant a package imported from two directories loaded twice, and arequire("dual-pkg")inside a package could hit the cached ESM shim namedsrc/deep/dual-pkg.resolveIdreturns (e.g.node:process→ an unenv file), now redirect (301) to the file's real path, so module names match real locations. workerd's re-request of the location reuses the resolution, so there's no loop and plugin hooks run once.require()asks the fallback fornode:processbefore its own built-in, and fails withNo such moduleon a 404.require("node:*")now redirects tonode:*?__require, an ESM shim that imports the built-in. workerd also caches the shim under the requested name, so if the built-in is missing, the shim's import resolves back to itself. The shim detects that case and throws workerd's usualNo such moduleerror.No implicit
unenvpolyfillsThe fallback used to resolve
unenv/node/<name>from the importer when it asked for anode:*module, so whether a built-in missing from workerd worked in dev depended on whetherunenvhappened to be installed. It now returns 404, so workerd loads its own built-in or reports it missing, as in production. Frameworks that want polyfills can alias them in their own resolution.Tests
test/miniflare.test.ts: a dual package imported from a nested dir, a CommonJS package requiringnode:process, a dual package and a relative file (with a same-basename decoy next to the importer), plus single-instance loading. Fails onmainwith the error from the issue.require("node:process")to a file with relative imports (the issue'sInvalid module specifiercase), with the hook called once.node:built-ins fail withNo such modulefor bothimportandrequire(), even with anunenvpackage installed.Only verified on Linux with miniflare v5. Windows paths reuse
toWorkerdPath()and rely on the Windows CI job.🤖 Generated with AI assistant
Summary by CodeRabbit
require()calls, including conditional package exports and redirects that preserve query parameters.require()calls to Node built-ins through a compatibility shim.node:modules now fail consistently for both imports andrequire()calls, including when polyfills are present.