Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ jobs:
run: npx nx affected -t test --parallel=3 --exclude='*,!tag:package'

- name: E2E Test for 3000-home
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
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

- name: E2E Test for 3001-shop
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
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

# - name: E2E Test for 3002-checkout
# 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
Expand Down
3 changes: 2 additions & 1 deletion apps/node-host/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "node-host",
"private": true,
"dependencies": {
"@module-federation/node": "workspace:*"
"@module-federation/node": "workspace:*",
"@module-federation/runtime": "workspace:*"
}
}
35 changes: 35 additions & 0 deletions apps/node-host/runtimePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { FederationRuntimePlugin } from '@module-federation/runtime/types';

export default function () {
return {
name: 'node-internal-plugin',
beforeInit(args) {
const { userOptions } = args;
if (userOptions.remotes) {
userOptions.remotes.forEach((remote) => {
const { alias } = remote;
if (alias) {
remote.name = remote.alias;
}
});
}
return args;
},
init(args) {
return args;
},
beforeRequest(args) {
if (args.id.startsWith('__webpack_require__')) {
const regex =
/__webpack_require__\.federation\.instance\.moduleCache\.get\(([^)]+)\)/;
const match = args.id.match(regex);
if (match !== null) {
const req = args.id.replace(match[0], '');
const remoteID = match[1].replace(/["']/g, '');
args.id = [remoteID, req].join('');
}
}
return args;
},
};
}
6 changes: 5 additions & 1 deletion apps/node-host/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ module.exports = composePlugins(withNx(), async (config) => {
new UniversalFederationPlugin({
isServer: true,
name: 'node_host',
runtimePlugins: [require.resolve('./runtimePlugin.ts')],
remotes: {
node_local_remote:
'commonjs ../../node-local-remote/dist/remoteEntry.js',
node_remote: 'node_remote@http://localhost:3002/remoteEntry.js',
// node_local_remote: '__webpack_require__.federation.instance.moduleCache.get("node_local_remote")',
node_remote:
'__webpack_require__.federation.instance.moduleCache.get("node_remote")@http://localhost:3002/remoteEntry.js',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using script global external modules in Webpack, it searches for containers in the internal module cache, as if it were a global variable.
image

// node_remote: 'node_remote@http://localhost:3002/remoteEntry.js',
},
}),
);
Expand Down
23 changes: 17 additions & 6 deletions packages/node/src/plugins/webpackChunkUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,19 +273,30 @@ export function generateLoadScript(runtimeTemplate: any): string {
return Template.asString([
'// load script equivalent for server side',
`${RuntimeGlobals.loadScript} = ${runtimeTemplate.basicFunction(
'url,callback,chunkId',
'url, callback, chunkId',
[
Template.indent([
`async function executeLoad(url, callback, name) {
if (!name) {
throw new Error('__webpack_require__.l name is required for ' + url);
}
return ${RuntimeGlobals.require}.federation.runtime.loadScriptNode(url, {attrs: {}}).then(function(res){
globalThis[name] = res[name] || res;
callback(globalThis[name]);
}).catch(callback)
if (name.startsWith('__webpack_require__')) {
const regex = /__webpack_require__\\.federation\\.instance\\.moduleCache\\.get\\(([^)]+)\\)/;
const match = name.match(regex);
if (match) {
name = match[1].replace(/["']/g, '');
}
}
try {
const federation = ${RuntimeGlobals.require}.federation;
const res = await ${RuntimeGlobals.require}.federation.runtime.loadScriptNode(url, { attrs: {} });
const enhancedRemote = await federation.instance.initRawContainer(name, url, res);
callback(enhancedRemote);
} catch (error) {
callback(error);
}
}`,
`executeLoad(url,callback,chunkId)`,
`executeLoad(url, callback, chunkId);`,
]),
],
)}`,
Expand Down
14 changes: 14 additions & 0 deletions packages/runtime/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,20 @@ export class FederationHost {
);
}

initRawContainer(
name: string,
url: string,
container: RemoteEntryExports,
): Module {
const remoteInfo = getRemoteInfo({ name, entry: url });
const module = new Module({ host: this, remoteInfo });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@2heal1, could you give me your input on this solution? I believe there are relevant use cases for when a user may sideload a container, and we need a way to convert it to a module instance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not encounter the case , but I think this solution is okay :D


module.remoteEntryExports = container;
this.moduleCache.set(name, module);

return module;
}

private async _getRemoteModuleAndOptions(id: string): Promise<{
module: Module;
moduleOptions: ModuleOptions;
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.