Skip to content

Commit 1d9bb77

Browse files
authored
feat(enhanced): add support for virtual runtime entrypoints (#2860)
1 parent ccaeb8e commit 1d9bb77

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

.changeset/warm-dryers-suffer.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@module-federation/enhanced': minor
3+
'@module-federation/sdk': minor
4+
---
5+
6+
Add support for using Virtual Runtime Entrypoints instead of writing a temporary file to disk

apps/website-new/docs/en/configure/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ type ModuleFederationOptions = {
2626
dev?: boolean | PluginDevOptions;
2727
// control types
2828
dts?: boolean | PluginDtsOptions;
29+
// Use a virtual runtime entrypoint instead of writing a temporary file to disk
30+
virtualRuntimeEntry?: boolean;
2931
};
3032
```

packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import fs from 'fs';
1212
import path from 'path';
1313
import { TEMP_DIR } from '../constant';
1414
import type { moduleFederationPlugin } from '@module-federation/sdk';
15+
import pBtoa from 'btoa';
1516

1617
const { RuntimeGlobals, Template } = require(
1718
normalizeWebpackPath('webpack'),
@@ -86,7 +87,7 @@ class FederationRuntimePlugin {
8687
`const pluginsToAdd = [`,
8788
Template.indent(
8889
runtimePluginNames.map(
89-
(item) => `${item} ? ${item}() : false,`,
90+
(item) => `${item} ? (${item}.default || ${item})() : false,`,
9091
),
9192
),
9293
`].filter(Boolean);`,
@@ -131,11 +132,20 @@ class FederationRuntimePlugin {
131132
return '';
132133
}
133134

134-
this.entryFilePath = FederationRuntimePlugin.getFilePath(
135-
this.options.name!,
136-
this.options.runtimePlugins!,
137-
this.bundlerRuntimePath,
138-
);
135+
if (!this.options?.virtualRuntimeEntry) {
136+
this.entryFilePath = FederationRuntimePlugin.getFilePath(
137+
this.options.name!,
138+
this.options.runtimePlugins!,
139+
this.bundlerRuntimePath,
140+
);
141+
} else {
142+
this.entryFilePath = `data:text/javascript;charset=utf-8;base64,${pBtoa(
143+
FederationRuntimePlugin.getTemplate(
144+
this.options.runtimePlugins!,
145+
this.bundlerRuntimePath,
146+
),
147+
)}`;
148+
}
139149
return this.entryFilePath;
140150
}
141151

@@ -159,7 +169,9 @@ class FederationRuntimePlugin {
159169
}
160170

161171
prependEntry(compiler: Compiler) {
162-
this.ensureFile();
172+
if (!this.options?.virtualRuntimeEntry) {
173+
this.ensureFile();
174+
}
163175
const entryFilePath = this.getFilePath();
164176

165177
modifyEntry({

packages/sdk/src/types/plugins/ModuleFederationPlugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ export interface ModuleFederationPluginOptions {
227227
dev?: boolean | PluginDevOptions;
228228
dts?: boolean | PluginDtsOptions;
229229
async?: boolean | AsyncBoundaryOptions;
230+
virtualRuntimeEntry?: boolean;
230231
}
231232
/**
232233
* Modules that should be exposed by this container. Property names are used as public paths.

0 commit comments

Comments
 (0)