-
-
Notifications
You must be signed in to change notification settings - Fork 419
fix(sdk): handle node builtins in ESM remote loader #4816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ScriptedAlchemy
merged 11 commits into
module-federation:main
from
dmchoi77:fix/node-esm-builtin-loader
Jul 3, 2026
+394
−19
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3aae1d7
fix(sdk): handle node builtins in esm loader
dmchoi77 5fc8b29
chore: add changeset for sdk esm loader fix
dmchoi77 e9f0106
Merge branch 'main' into fix/node-esm-builtin-loader
dmchoi77 2ff9f11
Merge branch 'main' into fix/node-esm-builtin-loader
dmchoi77 a8e5ff2
test(sdk): enable vm modules for jest
dmchoi77 d94e36a
fix(sdk): harden node esm remote loader
ScriptedAlchemy b14d02a
Merge remote-tracking branch 'origin/main' into fix/node-esm-builtin-…
ScriptedAlchemy dd1e17b
refactor(sdk): simplify node esm loader tests
ScriptedAlchemy a370d5b
fix(sdk): preserve remote url search in import meta
ScriptedAlchemy 004e2de
test(sdk): format node esm fixture sources
ScriptedAlchemy ff457b1
refactor(sdk): clarify source text module variable
ScriptedAlchemy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@module-federation/sdk": patch | ||
| --- | ||
|
|
||
| Handle Node.js built-in imports in the Node ESM remote loader. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { jest } from '@jest/globals'; | ||
|
|
||
| const createResponse = (body: string): Response => | ||
| ({ | ||
| ok: true, | ||
| status: 200, | ||
| statusText: 'OK', | ||
| text: jest.fn().mockResolvedValue(body), | ||
| }) as unknown as Response; | ||
|
|
||
| const loadNodeEsmScript = (url = 'http://example.com/remoteEntry.js') => | ||
| new Promise((resolve, reject) => { | ||
| import('../src/node').then(({ createScriptNode }) => { | ||
| createScriptNode( | ||
| url, | ||
| (error, scriptContext) => { | ||
| if (error) { | ||
| reject(error); | ||
| return; | ||
| } | ||
|
|
||
| resolve(scriptContext); | ||
| }, | ||
| { type: 'module' }, | ||
| ); | ||
| }, reject); | ||
| }); | ||
|
|
||
| describe('Node ESM builtin loading', () => { | ||
| const originalFetch = globalThis.fetch; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetModules(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| globalThis.fetch = originalFetch; | ||
| }); | ||
|
|
||
| it('loads node: builtin imports without fetching them as remote chunks', async () => { | ||
| const fetchMock = jest.fn(async (url: string) => { | ||
| if (url === 'node:url') { | ||
| throw new Error('node:url should not be fetched'); | ||
| } | ||
|
|
||
| return createResponse( | ||
| "import { pathToFileURL } from 'node:url'; export const marker = pathToFileURL('/tmp/module-federation').href; export default {};", | ||
| ); | ||
| }); | ||
| globalThis.fetch = fetchMock as unknown as typeof fetch; | ||
|
|
||
| const scriptContext = (await loadNodeEsmScript()) as { | ||
| marker: string; | ||
| }; | ||
|
|
||
| expect(scriptContext.marker).toBe('file:///tmp/module-federation'); | ||
| expect(fetchMock).toHaveBeenCalledWith('http://example.com/remoteEntry.js'); | ||
| expect(fetchMock).not.toHaveBeenCalledWith('node:url'); | ||
| }); | ||
|
|
||
| it('initializes import.meta.url for remote entries that create a Node.js require function', async () => { | ||
| const remoteEntryUrl = 'http://example.com/server/remoteEntry.js'; | ||
| const fetchMock = jest.fn(async (url: string) => { | ||
| if (url !== remoteEntryUrl) { | ||
| throw new Error(`${url} should not be fetched`); | ||
| } | ||
|
|
||
| return createResponse( | ||
| "import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); const path = require('node:path'); export const separator = path.sep; export const metaUrl = import.meta.url; export default {};", | ||
| ); | ||
| }); | ||
| globalThis.fetch = fetchMock as unknown as typeof fetch; | ||
|
|
||
| const scriptContext = (await loadNodeEsmScript(remoteEntryUrl)) as { | ||
| metaUrl: string; | ||
| separator: string; | ||
| }; | ||
|
|
||
| expect(scriptContext.separator).toBe('/'); | ||
| expect(scriptContext.metaUrl).toMatch(/^file:\/\/\//); | ||
| expect(fetchMock).toHaveBeenCalledWith(remoteEntryUrl); | ||
| }); | ||
|
|
||
| it('evaluates dynamically imported ESM chunks before their namespace is consumed', async () => { | ||
| const remoteEntryUrl = 'http://example.com/server/remoteEntry.js'; | ||
| const chunkUrl = 'http://example.com/server/chunk.mjs'; | ||
| const fetchMock = jest.fn(async (url: string) => { | ||
| if (url === remoteEntryUrl) { | ||
| return createResponse( | ||
| "export const chunkValuePromise = import('./chunk.mjs').then((chunk) => chunk.value); export default {};", | ||
| ); | ||
| } | ||
|
|
||
| if (url === chunkUrl) { | ||
| return createResponse("export const value = 'loaded chunk';"); | ||
| } | ||
|
|
||
| throw new Error(`${url} should not be fetched`); | ||
| }); | ||
| globalThis.fetch = fetchMock as unknown as typeof fetch; | ||
|
|
||
| const scriptContext = (await loadNodeEsmScript(remoteEntryUrl)) as { | ||
| chunkValuePromise: Promise<string>; | ||
| }; | ||
|
|
||
| await expect(scriptContext.chunkValuePromise).resolves.toBe('loaded chunk'); | ||
| expect(fetchMock).toHaveBeenCalledWith(remoteEntryUrl); | ||
| expect(fetchMock).toHaveBeenCalledWith(chunkUrl); | ||
| }); | ||
| }); | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.