Skip to content

Commit 879ad87

Browse files
feat(runtime): pass all loader hooks to the fetch and script functions (#3174)
1 parent 4eb09e7 commit 879ad87

File tree

5 files changed

+49
-43
lines changed

5 files changed

+49
-43
lines changed

.changeset/lucky-buckets-add.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@module-federation/runtime': minor
3+
'@module-federation/sdk': minor
4+
---
5+
6+
pass all loaderHooks to loadEntry and fetch functions instead of just createScript

packages/runtime/src/remote/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class RemoteHandler {
145145
loadEntry: new AsyncHook<
146146
[
147147
{
148-
createScriptHook: FederationHost['loaderHook']['lifecycle']['createScript'];
148+
loaderHook: FederationHost['loaderHook'];
149149
remoteInfo: RemoteInfo;
150150
remoteEntryExports?: RemoteEntryExports;
151151
},

packages/runtime/src/utils/load.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ async function loadEntryScript({
6868
name,
6969
globalName,
7070
entry,
71-
createScriptHook,
71+
loaderHook,
7272
}: {
7373
name: string;
7474
globalName: string;
7575
entry: string;
76-
createScriptHook: FederationHost['loaderHook']['lifecycle']['createScript'];
76+
loaderHook: FederationHost['loaderHook'];
7777
}): Promise<RemoteEntryExports> {
7878
const { entryExports: remoteEntryExports } = getRemoteEntryExports(
7979
name,
@@ -87,7 +87,7 @@ async function loadEntryScript({
8787
return loadScript(entry, {
8888
attrs: {},
8989
createScriptHook: (url, attrs) => {
90-
const res = createScriptHook.emit({ url, attrs });
90+
const res = loaderHook.lifecycle.createScript.emit({ url, attrs });
9191

9292
if (!res) return;
9393

@@ -127,11 +127,11 @@ async function loadEntryScript({
127127
async function loadEntryDom({
128128
remoteInfo,
129129
remoteEntryExports,
130-
createScriptHook,
130+
loaderHook,
131131
}: {
132132
remoteInfo: RemoteInfo;
133133
remoteEntryExports?: RemoteEntryExports;
134-
createScriptHook: FederationHost['loaderHook']['lifecycle']['createScript'];
134+
loaderHook: FederationHost['loaderHook'];
135135
}) {
136136
const { entry, entryGlobalName: globalName, name, type } = remoteInfo;
137137
switch (type) {
@@ -141,16 +141,16 @@ async function loadEntryDom({
141141
case 'system':
142142
return loadSystemJsEntry({ entry, remoteEntryExports });
143143
default:
144-
return loadEntryScript({ entry, globalName, name, createScriptHook });
144+
return loadEntryScript({ entry, globalName, name, loaderHook });
145145
}
146146
}
147147

148148
async function loadEntryNode({
149149
remoteInfo,
150-
createScriptHook,
150+
loaderHook,
151151
}: {
152152
remoteInfo: RemoteInfo;
153-
createScriptHook: FederationHost['loaderHook']['lifecycle']['createScript'];
153+
loaderHook: FederationHost['loaderHook'];
154154
}) {
155155
const { entry, entryGlobalName: globalName, name, type } = remoteInfo;
156156
const { entryExports: remoteEntryExports } = getRemoteEntryExports(
@@ -164,16 +164,18 @@ async function loadEntryNode({
164164

165165
return loadScriptNode(entry, {
166166
attrs: { name, globalName, type },
167-
createScriptHook: (url, attrs) => {
168-
const res = createScriptHook.emit({ url, attrs });
167+
loaderHook: {
168+
createScriptHook: (url: string, attrs: Record<string, any> = {}) => {
169+
const res = loaderHook.lifecycle.createScript.emit({ url, attrs });
169170

170-
if (!res) return;
171+
if (!res) return;
171172

172-
if ('url' in res) {
173-
return res;
174-
}
173+
if ('url' in res) {
174+
return res;
175+
}
175176

176-
return;
177+
return;
178+
},
177179
},
178180
})
179181
.then(() => {
@@ -220,9 +222,11 @@ export async function getRemoteEntry({
220222
if (!globalLoading[uniqueKey]) {
221223
const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;
222224
const createScriptHook = origin.loaderHook.lifecycle.createScript;
225+
const loaderHook = origin.loaderHook;
226+
223227
globalLoading[uniqueKey] = loadEntryHook
224228
.emit({
225-
createScriptHook,
229+
loaderHook,
226230
remoteInfo,
227231
remoteEntryExports,
228232
})
@@ -231,8 +235,8 @@ export async function getRemoteEntry({
231235
return res;
232236
}
233237
return isBrowserEnv()
234-
? loadEntryDom({ remoteInfo, remoteEntryExports, createScriptHook })
235-
: loadEntryNode({ remoteInfo, createScriptHook });
238+
? loadEntryDom({ remoteInfo, remoteEntryExports, loaderHook })
239+
: loadEntryNode({ remoteInfo, loaderHook });
236240
});
237241
}
238242

packages/sdk/src/node.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CreateScriptHookNode } from './types';
1+
import { CreateScriptHookNode, FetchHook } from './types';
22

33
function importNodeModule<T>(name: string): Promise<T> {
44
if (!name) {
@@ -22,12 +22,10 @@ const loadNodeFetch = async (): Promise<typeof fetch> => {
2222
const lazyLoaderHookFetch = async (
2323
input: RequestInfo | URL,
2424
init?: RequestInit,
25+
loaderHook?: any,
2526
): Promise<Response> => {
26-
// @ts-ignore
27-
const loaderHooks = __webpack_require__.federation.instance.loaderHook;
28-
2927
const hook = (url: RequestInfo | URL, init: RequestInit) => {
30-
return loaderHooks.lifecycle.fetch.emit(url, init);
28+
return loaderHook.lifecycle.fetch.emit(url, init);
3129
};
3230

3331
const res = await hook(input, init || {});
@@ -44,10 +42,13 @@ export function createScriptNode(
4442
url: string,
4543
cb: (error?: Error, scriptContext?: any) => void,
4644
attrs?: Record<string, any>,
47-
createScriptHook?: CreateScriptHookNode,
45+
loaderHook?: {
46+
createScriptHook?: CreateScriptHookNode;
47+
fetch?: FetchHook;
48+
},
4849
) {
49-
if (createScriptHook) {
50-
const hookResult = createScriptHook(url);
50+
if (loaderHook?.createScriptHook) {
51+
const hookResult = loaderHook.createScriptHook(url);
5152
if (hookResult && typeof hookResult === 'object' && 'url' in hookResult) {
5253
url = hookResult.url;
5354
}
@@ -63,20 +64,9 @@ export function createScriptNode(
6364
}
6465

6566
const getFetch = async (): Promise<typeof fetch> => {
66-
//@ts-ignore
67-
if (typeof __webpack_require__ !== 'undefined') {
68-
try {
69-
//@ts-ignore
70-
const loaderHooks = __webpack_require__.federation.instance.loaderHook;
71-
if (loaderHooks.lifecycle.fetch) {
72-
return lazyLoaderHookFetch;
73-
}
74-
} catch (e) {
75-
console.warn(
76-
'federation.instance.loaderHook.lifecycle.fetch failed:',
77-
e,
78-
);
79-
}
67+
if (loaderHook?.fetch) {
68+
return (input: RequestInfo | URL, init?: RequestInit) =>
69+
lazyLoaderHookFetch(input, init, loaderHook);
8070
}
8171

8272
return typeof fetch === 'undefined' ? loadNodeFetch() : fetch;
@@ -162,7 +152,9 @@ export function loadScriptNode(
162152
url: string,
163153
info: {
164154
attrs?: Record<string, any>;
165-
createScriptHook?: CreateScriptHookNode;
155+
loaderHook?: {
156+
createScriptHook?: CreateScriptHookNode;
157+
};
166158
},
167159
) {
168160
return new Promise<void>((resolve, reject) => {
@@ -181,7 +173,7 @@ export function loadScriptNode(
181173
}
182174
},
183175
info.attrs,
184-
info.createScriptHook,
176+
info.loaderHook,
185177
);
186178
});
187179
}

packages/sdk/src/types/hooks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ export type CreateScriptHook = (
2323
url: string,
2424
attrs?: Record<string, any> | undefined,
2525
) => CreateScriptHookReturn;
26+
27+
export type FetchHook = (
28+
args: [string, RequestInit],
29+
) => Promise<Response> | void | false;

0 commit comments

Comments
 (0)