fix(sdk): handle node builtins in ESM remote loader#4816
Conversation
🦋 Changeset detectedLatest commit: a8e5ff2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 46 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for module-federation-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Thanks for opening the draft PR. This looks like it may address the issue. Since it is still in draft, we'll wait until you mark it as ready for review, then take a closer look. |
|
@2heal1 Thank you for your response. I’ve marked the PR as ready for review. Please take a look when you have a chance. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5fc8b29b22
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
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".
| function createImportMetaUrl(url: string): string { | ||
| return `file:///__module_federation_remote__${encodeRemoteModulePath(url)}`; |
There was a problem hiding this comment.
Base synthetic import.meta.url where require can resolve
When an HTTP remote calls createRequire(import.meta.url) and then requires any non-builtin dependency, this fake file:///__module_federation_remote__/... location makes Node search from /__module_federation_remote__ and then /node_modules, not from the consuming app's project. That means bundled ESM remotes with externalized packages such as require('react') still fail even though the CJS path above intentionally bases createRequire on process.cwd() for non-file remotes.
Useful? React with 👍 / 👎.
| globalThis.fetch = originalFetch; | ||
| }); | ||
|
|
||
| it('loads node: builtin imports without fetching them as remote chunks', async () => { |
There was a problem hiding this comment.
Make the new SDK spec discoverable by Jest
This new spec is not exercised by pnpm --filter @module-federation/sdk test: I checked packages/sdk/jest.config.cjs, and its testMatch is <rootDir>__tests__/**/**.spec.[jt]s?(x), which expands to packages/sdk__tests__ rather than this packages/sdk/__tests__ directory; because the package script also uses --passWithNoTests, CI can pass without running these regression cases.
Useful? React with 👍 / 👎.
@module-federation/devtools
@module-federation/cli
create-module-federation
@module-federation/dts-plugin
@module-federation/enhanced
@module-federation/error-codes
@module-federation/esbuild
@module-federation/managers
@module-federation/manifest
@module-federation/metro
@module-federation/metro-plugin-rnc-cli
@module-federation/metro-plugin-rnef
@module-federation/metro-plugin-rock
@module-federation/modern-js
@module-federation/modern-js-v3
@module-federation/native-federation-tests
@module-federation/native-federation-typescript
@module-federation/nextjs-mf
@module-federation/node
@module-federation/observability-plugin
@module-federation/retry-plugin
@module-federation/rsbuild-plugin
@module-federation/rspack
@module-federation/rspress-plugin
@module-federation/runtime
@module-federation/runtime-core
@module-federation/runtime-tools
@module-federation/sdk
@module-federation/storybook-addon
@module-federation/third-party-dts-extractor
@module-federation/treeshake-frontend
@module-federation/treeshake-server
@module-federation/typescript
@module-federation/utilities
@module-federation/webpack-bundler-runtime
@module-federation/bridge-react
@module-federation/bridge-react-webpack-plugin
@module-federation/bridge-shared
@module-federation/bridge-vue3
@module-federation/inject-external-runtime-core-plugin
commit: |
Bundle Size Report16 package(s) changed, 25 unchanged. Package dist + ESM entry
Bundle targets
Consumer scenarios
Total dist (raw): 6.83 MB (+17.0 kB (+0.2%)) Bundle sizes are generated with rslib (Rspack). Package-root metrics preserve the historical report. Tracked subpath exports such as |
Description
Handle Node.js built-in module imports in the SDK Node ESM remote loader.
This PR updates the
vm.SourceTextModuleloading path used byloadScriptNode(..., { type: 'module' })so that:node:url,node:module, and bare builtins such aspathare loaded with native Node import and returned to the linker asvm.SyntheticModuleinstances.http:andhttps:module URLs; unsupported non-http specifiers fail with a clear error instead of being routed throughfetch.import.meta.url, which allows patterns such ascreateRequire(import.meta.url).The main issue this PR is meant to address is the
node:*builtin path. Theimport.meta.urland dynamic import pieces are adjacent ESM loader fixes found while covering the same Node ESM remote loader flow. I opened this as a draft so maintainers can confirm whether they prefer keeping those together or splitting them.Related Issue
Related #4815
Types of changes
Checklist
Test Plan
NODE_OPTIONS=--experimental-vm-modules corepack pnpm --filter @module-federation/sdk testcorepack pnpm --filter @module-federation/sdk run lintcorepack pnpm --filter @module-federation/sdk run buildNote:
buildcompletes successfully, with an existing rolldown warning about the pre-existingeval('require')CJS path inpackages/sdk/src/node.ts.