Skip to content

Commit 8baab1f

Browse files
committed
refactor(rsc-mf): validate expose keys and protect bootstrap module from exposure
1 parent 6a9e740 commit 8baab1f

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

tests/integration/rsc-mf/remote/module-federation.config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ const remoteExposeImports: Record<string, string> = {
3535
'./actionBundle': './src/components/actionBundle.ts',
3636
'./infoBundle': './src/components/infoBundle.ts',
3737
};
38+
const invalidExposeKeys = Object.keys(remoteExposeImports).filter(
39+
exposeKey => !exposeKey.startsWith('./'),
40+
);
41+
if (invalidExposeKeys.length > 0) {
42+
throw new Error(
43+
`Remote expose keys must be module-federation paths starting with "./". Invalid keys: ${invalidExposeKeys.join(', ')}`,
44+
);
45+
}
3846
const COMPONENT_EXPOSE_PREFIX = './src/components/';
3947
const nonComponentExposeEntries = Object.entries(remoteExposeImports).filter(
4048
([, importPath]) => !importPath.startsWith(COMPONENT_EXPOSE_PREFIX),
@@ -46,6 +54,16 @@ if (nonComponentExposeEntries.length > 0) {
4654
.join(', ')}`,
4755
);
4856
}
57+
const callbackExposeEntries = Object.entries(remoteExposeImports).filter(
58+
([, importPath]) => importPath === CALLBACK_BOOTSTRAP_IMPORT,
59+
);
60+
if (callbackExposeEntries.length > 0) {
61+
throw new Error(
62+
`Callback bootstrap module (${CALLBACK_BOOTSTRAP_IMPORT}) must remain internal-only and cannot be exposed. Invalid entries: ${callbackExposeEntries
63+
.map(([exposeKey]) => exposeKey)
64+
.join(', ')}`,
65+
);
66+
}
4967

5068
const sharedByScope = [
5169
{

tests/integration/rsc-mf/tests/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ function runTests({ mode }: TestConfig) {
589589
expect(moduleFederationConfigSource).toContain(
590590
'nonComponentExposeEntries',
591591
);
592+
expect(moduleFederationConfigSource).toContain('invalidExposeKeys');
593+
expect(moduleFederationConfigSource).toContain('callbackExposeEntries');
592594
expect(moduleFederationConfigSource).toContain("shareScope: 'default'");
593595
expect(moduleFederationConfigSource).toContain("shareScope: 'ssr'");
594596
expect(moduleFederationConfigSource).toContain("shareScope: 'rsc'");

0 commit comments

Comments
 (0)