Skip to content

Commit a050645

Browse files
feat(node): use runtime script loader in node federation (#1978)
Co-authored-by: Zhang Hang <TwoHeal@163.com> Co-authored-by: ScriptedAlchemy <zackaryjackson@bytedance.com>
1 parent 5576c6b commit a050645

File tree

25 files changed

+156
-288
lines changed

25 files changed

+156
-288
lines changed

.changeset/brown-monkeys-shave.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@module-federation/runtime': patch
3+
'@module-federation/node': patch
4+
---
5+
6+
Expose node script loaders to bundler runtime. Replace require.loadScript from federation/node to use federation.runtime.loadScriptNode

.changeset/loud-taxis-fix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/nextjs-mf': patch
3+
---
4+
5+
remove tech debt leftover from before federation enhanced apis

.changeset/tame-monkeys-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/utilities': patch
3+
---
4+
5+
Remove delegate module code from utils

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

Lines changed: 5 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
@@ -67,3 +67,6 @@ jobs:
6767

6868
- name: Build Next.js Apps in Production Mode
6969
run: pnpm app:next:build
70+
71+
- name: E2E Node Federation
72+
run: npx nx run node-host-e2e:e2e

apps/3000-home/pages/dynamic-remote.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

apps/3002-checkout/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"devDependencies": {
3232
"@module-federation/nextjs-mf": "workspace:*",
3333
"@module-federation/sdk": "workspace:*",
34+
"@module-federation/runtime": "workspace:*",
3435
"@module-federation/utilities": "workspace:*"
3536
},
3637
"scripts": {

apps/3002-checkout/pages/_app.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { Suspense, lazy, useState } from 'react';
22
import App from 'next/app';
3-
import dynamic from 'next/dynamic';
43
import { Layout, version } from 'antd';
54
import Router, { useRouter } from 'next/router';
65

apps/3002-checkout/pages/checkout/exposed-pages.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useState, useEffect } from 'react';
2+
import { loadRemote } from '@module-federation/runtime';
23

34
import { injectScript } from '@module-federation/nextjs-mf/utils';
45

@@ -7,17 +8,15 @@ export default function ExposedPages() {
78
const [pageMapV2, setPageMapV2] = useState('');
89

910
useEffect(() => {
10-
injectScript('checkout')
11-
.then((container) => container.get('./pages-map'))
12-
.then((data) => {
13-
setPageMap(data);
14-
});
11+
loadRemote('checkout/pages-map').then((data) => {
12+
//@ts-ignore
13+
setPageMap(data);
14+
});
1515

16-
injectScript('checkout')
17-
.then((container) => container.get('./pages-map-v2'))
18-
.then((data) => {
19-
setPageMapV2(data);
20-
});
16+
loadRemote('checkout/pages-map-v2').then((data) => {
17+
//@ts-ignore
18+
setPageMapV2(data);
19+
});
2120
}, []);
2221

2322
return (

apps/node-host-e2e/project.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@
1111
"passWithNoTests": true
1212
}
1313
},
14+
"test:e2e": {
15+
"executor": "nx:run-commands",
16+
"options": {
17+
"parallel": true,
18+
"commands": [
19+
{
20+
"command": "nx run node-remote:serve",
21+
"forwardAllArgs": false
22+
},
23+
{
24+
"command": "nx run node-local-remote:serve",
25+
"forwardAllArgs": false
26+
},
27+
{
28+
"command": "nx run node-host:serve",
29+
"forwardAllArgs": false
30+
},
31+
{
32+
"command": "sleep 10 && nx run node-host-e2e:e2e",
33+
"forwardAllArgs": true
34+
}
35+
]
36+
}
37+
},
1438
"lint": {
1539
"executor": "@nx/linter:eslint",
1640
"outputs": ["{options.outputFile}"],

apps/node-host-e2e/src/node-host/node-host.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import axios from 'axios';
22

33
describe('GET /', () => {
44
it('should return a message', async () => {
5-
const res = await axios.get(`/`);
5+
const res = await axios.get(`/api`);
66

77
expect(res.status).toBe(200);
8-
expect(res.data).toEqual({ message: 'Hello API' });
8+
expect(res.data).toEqual({
9+
message: 'Welcome to node-host!',
10+
remotes: {
11+
node_remote: 'module from node-remote',
12+
node_local_remote: 'module from node-local-remote',
13+
},
14+
});
915
});
1016
});

0 commit comments

Comments
 (0)