Skip to content

Commit 215cccc

Browse files
feat: initRawContainer method (#2011)
1 parent f3c43f0 commit 215cccc

File tree

7 files changed

+78
-10
lines changed

7 files changed

+78
-10
lines changed

.github/workflows/build-and-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ jobs:
4848
run: npx nx affected -t test --parallel=3 --exclude='*,!tag:package'
4949

5050
- name: E2E Test for 3000-home
51-
run: pnpm run app:next:dev & echo "done" && sleep 15 && npx nx run-many --target=test:e2e --projects=3000-home && lsof -ti tcp:3000,3001,3002 | xargs kill
51+
run: pnpm run app:next:dev & echo "done" && sleep 20 && npx nx run-many --target=test:e2e --projects=3000-home && lsof -ti tcp:3000,3001,3002 | xargs kill
5252

5353
- name: E2E Test for 3001-shop
54-
run: pnpm run app:next:dev & echo "done" && sleep 15 && npx nx run-many --target=test:e2e --projects=3001-shop && lsof -ti tcp:3000,3001,3002 | xargs kill
54+
run: pnpm run app:next:dev & echo "done" && sleep 20 && npx nx run-many --target=test:e2e --projects=3001-shop && lsof -ti tcp:3000,3001,3002 | xargs kill
5555

5656
# - name: E2E Test for 3002-checkout
5757
# run: pnpm run app:next:dev & echo "done" && sleep 15 && npx nx run-many --target=test:e2e --projects=3002-checkout && lsof -ti tcp:3000,3001,3002 | xargs kill

apps/node-host/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "node-host",
33
"private": true,
44
"dependencies": {
5-
"@module-federation/node": "workspace:*"
5+
"@module-federation/node": "workspace:*",
6+
"@module-federation/runtime": "workspace:*"
67
}
78
}

apps/node-host/runtimePlugin.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { FederationRuntimePlugin } from '@module-federation/runtime/types';
2+
3+
export default function () {
4+
return {
5+
name: 'node-internal-plugin',
6+
beforeInit(args) {
7+
const { userOptions } = args;
8+
if (userOptions.remotes) {
9+
userOptions.remotes.forEach((remote) => {
10+
const { alias } = remote;
11+
if (alias) {
12+
remote.name = remote.alias;
13+
}
14+
});
15+
}
16+
return args;
17+
},
18+
init(args) {
19+
return args;
20+
},
21+
beforeRequest(args) {
22+
if (args.id.startsWith('__webpack_require__')) {
23+
const regex =
24+
/__webpack_require__\.federation\.instance\.moduleCache\.get\(([^)]+)\)/;
25+
const match = args.id.match(regex);
26+
if (match !== null) {
27+
const req = args.id.replace(match[0], '');
28+
const remoteID = match[1].replace(/["']/g, '');
29+
args.id = [remoteID, req].join('');
30+
}
31+
}
32+
return args;
33+
},
34+
};
35+
}

apps/node-host/webpack.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ module.exports = composePlugins(withNx(), async (config) => {
1414
new UniversalFederationPlugin({
1515
isServer: true,
1616
name: 'node_host',
17+
runtimePlugins: [require.resolve('./runtimePlugin.ts')],
1718
remotes: {
1819
node_local_remote:
1920
'commonjs ../../node-local-remote/dist/remoteEntry.js',
20-
node_remote: 'node_remote@http://localhost:3002/remoteEntry.js',
21+
// node_local_remote: '__webpack_require__.federation.instance.moduleCache.get("node_local_remote")',
22+
node_remote:
23+
'__webpack_require__.federation.instance.moduleCache.get("node_remote")@http://localhost:3002/remoteEntry.js',
24+
// node_remote: 'node_remote@http://localhost:3002/remoteEntry.js',
2125
},
2226
}),
2327
);

packages/node/src/plugins/webpackChunkUtilities.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,19 +273,30 @@ export function generateLoadScript(runtimeTemplate: any): string {
273273
return Template.asString([
274274
'// load script equivalent for server side',
275275
`${RuntimeGlobals.loadScript} = ${runtimeTemplate.basicFunction(
276-
'url,callback,chunkId',
276+
'url, callback, chunkId',
277277
[
278278
Template.indent([
279279
`async function executeLoad(url, callback, name) {
280280
if (!name) {
281281
throw new Error('__webpack_require__.l name is required for ' + url);
282282
}
283-
return ${RuntimeGlobals.require}.federation.runtime.loadScriptNode(url, {attrs: {}}).then(function(res){
284-
globalThis[name] = res[name] || res;
285-
callback(globalThis[name]);
286-
}).catch(callback)
283+
if (name.startsWith('__webpack_require__')) {
284+
const regex = /__webpack_require__\\.federation\\.instance\\.moduleCache\\.get\\(([^)]+)\\)/;
285+
const match = name.match(regex);
286+
if (match) {
287+
name = match[1].replace(/["']/g, '');
288+
}
289+
}
290+
try {
291+
const federation = ${RuntimeGlobals.require}.federation;
292+
const res = await ${RuntimeGlobals.require}.federation.runtime.loadScriptNode(url, { attrs: {} });
293+
const enhancedRemote = await federation.instance.initRawContainer(name, url, res);
294+
callback(enhancedRemote);
295+
} catch (error) {
296+
callback(error);
297+
}
287298
}`,
288-
`executeLoad(url,callback,chunkId)`,
299+
`executeLoad(url, callback, chunkId);`,
289300
]),
290301
],
291302
)}`,

packages/runtime/src/core.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,20 @@ export class FederationHost {
482482
);
483483
}
484484

485+
initRawContainer(
486+
name: string,
487+
url: string,
488+
container: RemoteEntryExports,
489+
): Module {
490+
const remoteInfo = getRemoteInfo({ name, entry: url });
491+
const module = new Module({ host: this, remoteInfo });
492+
493+
module.remoteEntryExports = container;
494+
this.moduleCache.set(name, module);
495+
496+
return module;
497+
}
498+
485499
private async _getRemoteModuleAndOptions(id: string): Promise<{
486500
module: Module;
487501
moduleOptions: ModuleOptions;

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)