Skip to content

Commit 66f1360

Browse files
committed
refactor(rsc-mf): infer callback bootstrap from local import graph
1 parent 03b1c97 commit 66f1360

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

tests/integration/rsc-mf/remote/src/runtime/createRscExposeDefinitions.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const LOCAL_MODULE_SPECIFIER_PATTERN = /^\.{1,2}\//;
1010
const SOURCE_DIRECTIVE_PATTERN = /^\s*['"]use (?:client|server)['"]\s*;?/m;
1111
const EXPORT_FROM_SPECIFIER_PATTERN =
1212
/export\s+(?:\*\s+from|\{[^}]*\}\s+from)\s*['"]([^'"]+)['"]/g;
13+
const IMPORT_FROM_SPECIFIER_PATTERN =
14+
/import\s+(?:type\s+)?(?:[^'";]+?\s+from\s+)?['"]([^'"]+)['"]/g;
1315
const SOURCE_ENTRY_EXTENSIONS = [
1416
'.ts',
1517
'.tsx',
@@ -195,19 +197,26 @@ const referencesCallbackCapableSourceModule = (importPath: string) => {
195197
return true;
196198
}
197199

198-
const exportFromMatches = sourceText.matchAll(
199-
EXPORT_FROM_SPECIFIER_PATTERN,
200-
);
201-
for (const match of exportFromMatches) {
202-
const moduleSpecifier = match[1];
203-
if (!LOCAL_MODULE_SPECIFIER_PATTERN.test(moduleSpecifier)) {
200+
const localSpecifierMatches = [
201+
...sourceText.matchAll(EXPORT_FROM_SPECIFIER_PATTERN),
202+
...sourceText.matchAll(IMPORT_FROM_SPECIFIER_PATTERN),
203+
];
204+
for (const match of localSpecifierMatches) {
205+
const [moduleSpecifier] = match.slice(1);
206+
if (
207+
typeof moduleSpecifier !== 'string' ||
208+
!LOCAL_MODULE_SPECIFIER_PATTERN.test(moduleSpecifier)
209+
) {
204210
continue;
205211
}
206212
const childModuleFilePath = resolveUserlandImportPathToFile(
207213
moduleSpecifier,
208214
filePath,
209215
);
210-
if (childModuleFilePath && hasCallbackDirective(childModuleFilePath)) {
216+
if (!childModuleFilePath) {
217+
continue;
218+
}
219+
if (hasCallbackDirective(childModuleFilePath)) {
211220
callbackDirectiveBySourceFile.set(filePath, true);
212221
return true;
213222
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,24 @@ describe('createRscExposeDefinitions', () => {
359359
});
360360
});
361361

362+
it('infers callback bootstrap from local import graph use client directives', () => {
363+
const { createRscExposeDefinitions, CALLBACK_BOOTSTRAP_MODULE } =
364+
loadCreateRscExposeDefinitions();
365+
expect(
366+
createRscExposeDefinitions({
367+
'./customRemoteServerCard': './src/components/RemoteServerCard.tsx',
368+
}),
369+
).toEqual({
370+
'./customRemoteServerCard': {
371+
import: [
372+
CALLBACK_BOOTSTRAP_MODULE,
373+
'./src/components/RemoteServerCard.tsx',
374+
],
375+
layer: 'react-server-components',
376+
},
377+
});
378+
});
379+
362380
it('keeps non-callback source modules free of callback bootstrap import', () => {
363381
const { createRscExposeDefinitions } = loadCreateRscExposeDefinitions();
364382
expect(

0 commit comments

Comments
 (0)