@@ -6,10 +6,6 @@ const CALLBACK_BOOTSTRAP_PREFIX = './src/runtime/';
66const USERLAND_EXPOSE_PREFIX = './' ;
77const SOURCE_ENTRY_EXTENSION_PATTERN = / \. [ c m ] ? [ j t ] s x ? $ / i;
88const RSC_LAYER = 'react-server-components' ;
9- const CALLBACK_BOOTSTRAP_EXPOSE_KEY_PATTERN =
10- / ^ \. \/ (?: R e m o t e C l i e n t [ \w - ] * | a c t i o n s | n e s t e d A c t i o n s | d e f a u l t A c t i o n | a c t i o n B u n d l e ) $ / ;
11- const CALLBACK_BOOTSTRAP_IMPORT_PATH_PATTERN =
12- / \/ (?: R e m o t e C l i e n t [ \w - ] * | a c t i o n s | n e s t e d A c t i o n s | d e f a u l t A c t i o n | a c t i o n B u n d l e ) \. [ c m ] ? [ j t ] s x ? $ / ;
139const LOCAL_MODULE_SPECIFIER_PATTERN = / ^ \. { 1 , 2 } \/ / ;
1410const SOURCE_DIRECTIVE_PATTERN = / ^ \s * [ ' " ] u s e (?: c l i e n t | s e r v e r ) [ ' " ] \s * ; ? / m;
1511const EXPORT_FROM_SPECIFIER_PATTERN =
@@ -25,6 +21,7 @@ const SOURCE_ENTRY_EXTENSIONS = [
2521 '.cjs' ,
2622] as const ;
2723const REMOTE_PROJECT_ROOT = path . resolve ( __dirname , '../..' ) ;
24+ const callbackDirectiveBySourceFile = new Map < string , boolean > ( ) ;
2825
2926type ExposeImportInput = string | string [ ] ;
3027export type ExposeDefinitionInput =
@@ -134,15 +131,9 @@ const createRscExpose = (
134131 layer : RSC_LAYER ,
135132 } ) as const ;
136133
137- const shouldInjectCallbackBootstrap = (
138- exposeKey : string ,
139- importPaths : string [ ] ,
140- ) =>
141- CALLBACK_BOOTSTRAP_EXPOSE_KEY_PATTERN . test ( exposeKey ) ||
142- importPaths . some (
143- importPath =>
144- CALLBACK_BOOTSTRAP_IMPORT_PATH_PATTERN . test ( importPath ) ||
145- referencesCallbackCapableSourceModule ( importPath ) ,
134+ const shouldInjectCallbackBootstrap = ( importPaths : string [ ] ) =>
135+ importPaths . some ( importPath =>
136+ referencesCallbackCapableSourceModule ( importPath ) ,
146137 ) ;
147138
148139const readSourceFile = ( filePath : string ) => {
@@ -186,15 +177,21 @@ const referencesCallbackCapableSourceModule = (importPath: string) => {
186177
187178 const visitedFiles = new Set < string > ( ) ;
188179 const hasCallbackDirective = ( filePath : string ) : boolean => {
180+ const cachedResult = callbackDirectiveBySourceFile . get ( filePath ) ;
181+ if ( typeof cachedResult !== 'undefined' ) {
182+ return cachedResult ;
183+ }
189184 if ( visitedFiles . has ( filePath ) ) {
190185 return false ;
191186 }
192187 visitedFiles . add ( filePath ) ;
193188 const sourceText = readSourceFile ( filePath ) ;
194189 if ( ! sourceText ) {
190+ callbackDirectiveBySourceFile . set ( filePath , false ) ;
195191 return false ;
196192 }
197193 if ( SOURCE_DIRECTIVE_PATTERN . test ( sourceText ) ) {
194+ callbackDirectiveBySourceFile . set ( filePath , true ) ;
198195 return true ;
199196 }
200197
@@ -211,10 +208,12 @@ const referencesCallbackCapableSourceModule = (importPath: string) => {
211208 filePath ,
212209 ) ;
213210 if ( childModuleFilePath && hasCallbackDirective ( childModuleFilePath ) ) {
211+ callbackDirectiveBySourceFile . set ( filePath , true ) ;
214212 return true ;
215213 }
216214 }
217215
216+ callbackDirectiveBySourceFile . set ( filePath , false ) ;
218217 return false ;
219218 } ;
220219
@@ -343,10 +342,7 @@ export const createRscExposeDefinitions = (
343342 createRscExpose (
344343 normalizedDefinition . importPaths ,
345344 normalizedDefinition . exposeOverrides ,
346- shouldInjectCallbackBootstrap (
347- exposeKey ,
348- normalizedDefinition . importPaths ,
349- ) ,
345+ shouldInjectCallbackBootstrap ( normalizedDefinition . importPaths ) ,
350346 ) ,
351347 ] ) ,
352348 ) ;
0 commit comments