Skip to content

Commit 2fa5593

Browse files
author
David Castro
committed
updated js and ts project examples for node dynamic remote loading
1 parent 908450d commit 2fa5593

37 files changed

+29540
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
node_modules
3+
host/dist
4+
remote/dist
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v20.12.0
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Dynamic Remotes on Node w/ Javascript
2+
3+
Similar to browser side dynamic remotes.
4+
5+
This allows you to dynamically load remote containers on the server.
6+
7+
`pnpm run start` will initiate a build and http server, then the node will load the remotes. The host will poll for changes on the remote and reload when they are detected.
8+
9+
NOTE: HMR is not supported on the server side yet, so we can't get notified of changes in the remote. We have to poll for changes.

dynamic-remotes-node-javascript/host/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const { loadRemote, init } = require('@module-federation/runtime');
2+
const {performReload, revalidate} = require('@module-federation/node/utils');
3+
4+
console.log('hello from host');
5+
6+
let instance;
7+
let loadedString;
8+
let loadedClass;
9+
let loadedClassInstance;
10+
11+
async function initAndLoad() {
12+
await performReload(true)
13+
14+
instance = init({
15+
name: 'host',
16+
remotes: [
17+
{
18+
name: 'remote',
19+
entry: 'http://localhost:3002/remoteEntry.js',
20+
},
21+
],
22+
});
23+
24+
25+
loadRemote('remote/string').then(value => {
26+
loadedString = value;
27+
console.log('loaded string', loadedString);
28+
});
29+
30+
loadRemote('remote/class').then(async value => {
31+
loadedClass = value;
32+
loadedClassInstance = new value();
33+
34+
console.log('current loaded class', loadedClass);
35+
console.log('current loaded class test value', loadedClassInstance.getTestValue());
36+
console.log('current loaded class run test', await loadedClassInstance.runTest());
37+
});
38+
}
39+
40+
initAndLoad();
41+
42+
setInterval(async () => {
43+
console.log('host(): checking remote for updates');
44+
45+
// NOTE: this is called the first time an update is detected on the remote and never again
46+
// NOTE: had to patch hot-reload.js to get this to not throw an error
47+
// we automatically reset require cache, so the reload callback is only if you need to do something else
48+
const shouldReload = await revalidate();
49+
50+
// do something extra after revalidation
51+
if (shouldReload) {
52+
// reload the server
53+
console.log('host(): should reload');
54+
initAndLoad();
55+
} else {
56+
console.log('host(): should not reload');
57+
}
58+
}, 5000);

dynamic-remotes-node-javascript/host/src/noop.js

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { UniversalFederationPlugin } = require('@module-federation/node');
2+
const webpack = require('webpack');
3+
4+
module.exports = {
5+
entry: './src/index.js',
6+
mode: 'development',
7+
target: 'async-node',
8+
externals: [],
9+
output: {
10+
publicPath: 'http://localhost:3001/',
11+
library: { type: 'commonjs-module' },
12+
},
13+
plugins: [
14+
new webpack.HotModuleReplacementPlugin(),
15+
new UniversalFederationPlugin({
16+
remoteType: 'script',
17+
isServer: true,
18+
name: 'host',
19+
useRuntimePlugin: true,
20+
exposes: {
21+
'./noop': './src/noop.js',
22+
},
23+
}),
24+
],
25+
};

0 commit comments

Comments
 (0)