@@ -185,6 +185,37 @@ function determineFrameworkSettings(workPath: string) {
185185 return REMIX_FRAMEWORK_SETTINGS ;
186186}
187187
188+ interface HandlerOptions {
189+ rootDir : string ;
190+ serverBuildPath : string ;
191+ serverEntryPoint ?: string ;
192+ serverSourcePromise : Promise < string > ;
193+ sourceSearchValue : string ;
194+ }
195+
196+ async function determineHandler ( {
197+ rootDir,
198+ serverBuildPath,
199+ serverEntryPoint,
200+ serverSourcePromise,
201+ sourceSearchValue,
202+ } : HandlerOptions ) {
203+ let handler = relative ( rootDir , serverBuildPath ) ;
204+ let handlerPath = join ( rootDir , handler ) ;
205+ if ( ! serverEntryPoint ) {
206+ const baseServerBuildPath = basename ( serverBuildPath , '.js' ) ;
207+ handler = join ( dirname ( handler ) , `server-${ baseServerBuildPath } .mjs` ) ;
208+ handlerPath = join ( rootDir , handler ) ;
209+
210+ const serverSource = await serverSourcePromise ;
211+ await fs . writeFile (
212+ handlerPath ,
213+ serverSource . replace ( sourceSearchValue , `./${ baseServerBuildPath } .js` )
214+ ) ;
215+ }
216+ return { handler, handlerPath } ;
217+ }
218+
188219export const build : BuildV2 = async ( {
189220 entrypoint,
190221 workPath,
@@ -477,23 +508,13 @@ async function createRenderReactRouterFunction(
477508) : Promise < EdgeFunction | NodejsLambda > {
478509 const isEdgeFunction = config . runtime === 'edge' ;
479510
480- let handler = relative ( rootDir , serverBuildPath ) ;
481- let handlerPath = join ( rootDir , handler ) ;
482- if ( ! serverEntryPoint ) {
483- const baseServerBuildPath = basename ( serverBuildPath , '.js' ) ;
484- handler = join ( dirname ( handler ) , `server-${ baseServerBuildPath } .mjs` ) ;
485- handlerPath = join ( rootDir , handler ) ;
486-
487- // Copy the `server-react-router.mjs` file into the "build" directory
488- const reactRouterServerSrc = await reactRouterServerSrcPromise ;
489- await fs . writeFile (
490- handlerPath ,
491- reactRouterServerSrc . replace (
492- REACT_ROUTER_FRAMEWORK_SETTINGS . sourceSearchValue ,
493- `./${ baseServerBuildPath } .js`
494- )
495- ) ;
496- }
511+ const { handler, handlerPath } = await determineHandler ( {
512+ rootDir,
513+ serverBuildPath,
514+ serverEntryPoint,
515+ serverSourcePromise : reactRouterServerSrcPromise ,
516+ sourceSearchValue : REACT_ROUTER_FRAMEWORK_SETTINGS . sourceSearchValue ,
517+ } ) ;
497518
498519 // Trace the handler with `@vercel/nft`
499520 let conditions : NodeFileTraceOptions [ 'conditions' ] ;
@@ -554,23 +575,13 @@ async function createRenderNodeFunction(
554575 frameworkVersion : string ,
555576 config : /*TODO: ResolvedNodeRouteConfig*/ any
556577) : Promise < NodejsLambda > {
557- let handler = relative ( rootDir , serverBuildPath ) ;
558- let handlerPath = join ( rootDir , handler ) ;
559- if ( ! serverEntryPoint ) {
560- const baseServerBuildPath = basename ( serverBuildPath , '.js' ) ;
561- handler = join ( dirname ( handler ) , `server-${ baseServerBuildPath } .mjs` ) ;
562- handlerPath = join ( rootDir , handler ) ;
563-
564- // Copy the `server-node.mjs` file into the "build" directory
565- const nodeServerSrc = await nodeServerSrcPromise ;
566- await fs . writeFile (
567- handlerPath ,
568- nodeServerSrc . replace (
569- REMIX_FRAMEWORK_SETTINGS . sourceSearchValue ,
570- `./${ baseServerBuildPath } .js`
571- )
572- ) ;
573- }
578+ const { handler, handlerPath } = await determineHandler ( {
579+ rootDir,
580+ serverBuildPath,
581+ serverEntryPoint,
582+ serverSourcePromise : nodeServerSrcPromise ,
583+ sourceSearchValue : REMIX_FRAMEWORK_SETTINGS . sourceSearchValue ,
584+ } ) ;
574585
575586 // Trace the handler with `@vercel/nft`
576587 const trace = await nodeFileTrace ( [ handlerPath ] , {
@@ -607,23 +618,13 @@ async function createRenderEdgeFunction(
607618 frameworkVersion : string ,
608619 config : /* TODO: ResolvedEdgeRouteConfig*/ any
609620) : Promise < EdgeFunction > {
610- let handler = relative ( rootDir , serverBuildPath ) ;
611- let handlerPath = join ( rootDir , handler ) ;
612- if ( ! serverEntryPoint ) {
613- const baseServerBuildPath = basename ( serverBuildPath , '.js' ) ;
614- handler = join ( dirname ( handler ) , `server-${ baseServerBuildPath } .mjs` ) ;
615- handlerPath = join ( rootDir , handler ) ;
616-
617- // Copy the `server-edge.mjs` file into the "build" directory
618- const edgeServerSrc = await edgeServerSrcPromise ;
619- await fs . writeFile (
620- handlerPath ,
621- edgeServerSrc . replace (
622- REMIX_FRAMEWORK_SETTINGS . sourceSearchValue ,
623- `./${ baseServerBuildPath } .js`
624- )
625- ) ;
626- }
621+ const { handler, handlerPath } = await determineHandler ( {
622+ rootDir,
623+ serverBuildPath,
624+ serverEntryPoint,
625+ serverSourcePromise : edgeServerSrcPromise ,
626+ sourceSearchValue : REMIX_FRAMEWORK_SETTINGS . sourceSearchValue ,
627+ } ) ;
627628
628629 // Trace the handler with `@vercel/nft`
629630 const trace = await nodeFileTrace ( [ handlerPath ] , {
0 commit comments