@@ -197,7 +197,6 @@ export type ResolvedVitePluginConfig = Pick<
197
197
buildDirectory : string ;
198
198
manifest : boolean ;
199
199
serverBuildFile : string ;
200
- serverBundleId ?: string ;
201
200
serverBundles ?: ServerBundlesFunction ;
202
201
ssr : boolean ;
203
202
} ;
@@ -207,6 +206,18 @@ export type ServerBundleBuildConfig = {
207
206
serverBundleId : string ;
208
207
} ;
209
208
209
+ type BuildContext =
210
+ | {
211
+ isSsrBuild : false ;
212
+ getBrowserManifest ?: never ;
213
+ serverBundleId ?: never ;
214
+ }
215
+ | {
216
+ isSsrBuild : true ;
217
+ getBrowserManifest : ( ) => Promise < BrowserManifest > ;
218
+ serverBundleId : string | undefined ;
219
+ } ;
220
+
210
221
let serverBuildId = VirtualModule . id ( "server-build" ) ;
211
222
let serverManifestId = VirtualModule . id ( "server-manifest" ) ;
212
223
let browserManifestId = VirtualModule . id ( "browser-manifest" ) ;
@@ -405,15 +416,20 @@ const getServerBundleBuildConfig = (
405
416
return viteUserConfig . __remixServerBundleBuildConfig as ServerBundleBuildConfig ;
406
417
} ;
407
418
408
- export let getServerBuildDirectory = ( remixConfig : ResolvedVitePluginConfig ) =>
419
+ let getServerBuildDirectory = (
420
+ remixConfig : ResolvedVitePluginConfig ,
421
+ { serverBundleId } : Pick < BuildContext , "serverBundleId" >
422
+ ) =>
409
423
path . join (
410
424
remixConfig . buildDirectory ,
411
425
"server" ,
412
- ...( typeof remixConfig . serverBundleId === "string"
413
- ? [ remixConfig . serverBundleId ]
414
- : [ ] )
426
+ ...( typeof serverBundleId === "string" ? [ serverBundleId ] : [ ] )
415
427
) ;
416
428
429
+ export let getServerBuildRootDirectory = (
430
+ remixConfig : ResolvedVitePluginConfig
431
+ ) => getServerBuildDirectory ( remixConfig , { serverBundleId : undefined } ) ;
432
+
417
433
let getClientBuildDirectory = ( remixConfig : ResolvedVitePluginConfig ) =>
418
434
path . join ( remixConfig . buildDirectory , "client" ) ;
419
435
@@ -424,9 +440,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
424
440
let viteConfig : Vite . ResolvedConfig | undefined ;
425
441
426
442
let cssModulesManifest : Record < string , string > = { } ;
427
- let ssrBuildContext :
428
- | { isSsrBuild : false }
429
- | { isSsrBuild : true ; getBrowserManifest : ( ) => Promise < BrowserManifest > } ;
443
+ let buildContext : BuildContext ;
430
444
431
445
let viteChildCompiler : Vite . ViteDevServer | null = null ;
432
446
@@ -507,10 +521,8 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
507
521
508
522
// For server bundle builds, override the relevant config. This lets us run
509
523
// multiple server builds with each one targeting a subset of routes.
510
- let serverBundleId : string | undefined = undefined ;
511
524
if ( serverBundleBuildConfig ) {
512
525
routes = serverBundleBuildConfig . routes ;
513
- serverBundleId = serverBundleBuildConfig . serverBundleId ;
514
526
}
515
527
516
528
let resolvedRemixConfig : ResolvedVitePluginConfig = {
@@ -525,7 +537,6 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
525
537
rootDirectory,
526
538
routes,
527
539
serverBuildFile,
528
- serverBundleId,
529
540
serverBundles,
530
541
serverModuleFormat,
531
542
ssr,
@@ -534,6 +545,22 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
534
545
return resolvedRemixConfig ;
535
546
} ;
536
547
548
+ let resolveBuildContext = ( viteConfigEnv : Vite . ConfigEnv ) : BuildContext => {
549
+ let buildContext : BuildContext =
550
+ viteConfigEnv . isSsrBuild && viteCommand === "build"
551
+ ? {
552
+ isSsrBuild : true ,
553
+ getBrowserManifest : createBrowserManifestForBuild ,
554
+ serverBundleId :
555
+ getServerBundleBuildConfig ( viteUserConfig ) ?. serverBundleId ,
556
+ }
557
+ : {
558
+ isSsrBuild : false ,
559
+ } ;
560
+
561
+ return buildContext ;
562
+ } ;
563
+
537
564
let getServerEntry = async ( ) => {
538
565
return `
539
566
import * as entryServer from ${ JSON . stringify (
@@ -708,6 +735,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
708
735
viteCommand = viteConfigEnv . command ;
709
736
710
737
remixConfig = await resolvePluginConfig ( ) ;
738
+ buildContext = resolveBuildContext ( viteConfigEnv ) ;
711
739
712
740
Object . assign (
713
741
process . env ,
@@ -796,7 +824,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
796
824
ssrEmitAssets : true ,
797
825
copyPublicDir : false , // Assets in the public directory are only used by the client
798
826
manifest : true , // We need the manifest to detect SSR-only assets
799
- outDir : getServerBuildDirectory ( remixConfig ) ,
827
+ outDir : getServerBuildDirectory ( remixConfig , buildContext ) ,
800
828
rollupOptions : {
801
829
...viteUserConfig . build ?. rollupOptions ,
802
830
preserveEntrySignatures : "exports-only" ,
@@ -816,16 +844,6 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
816
844
817
845
viteConfig = resolvedViteConfig ;
818
846
819
- ssrBuildContext =
820
- viteConfig . build . ssr && viteCommand === "build"
821
- ? {
822
- isSsrBuild : true ,
823
- getBrowserManifest : createBrowserManifestForBuild ,
824
- }
825
- : {
826
- isSsrBuild : false ,
827
- } ;
828
-
829
847
// We load the same Vite config file again for the child compiler so
830
848
// that both parent and child compiler's plugins have independent state.
831
849
// If we re-used the `viteUserConfig.plugins` array for the child
@@ -846,7 +864,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
846
864
{
847
865
command : viteConfig . command ,
848
866
mode : viteConfig . mode ,
849
- isSsrBuild : ssrBuildContext . isSsrBuild ,
867
+ isSsrBuild : buildContext . isSsrBuild ,
850
868
} ,
851
869
viteConfig . configFile
852
870
) ;
@@ -1010,15 +1028,18 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
1010
1028
// After the SSR build is finished, we inspect the Vite manifest for
1011
1029
// the SSR build and move server-only assets to client assets directory
1012
1030
async handler ( ) {
1013
- if ( ! ssrBuildContext . isSsrBuild ) {
1031
+ if ( ! buildContext . isSsrBuild ) {
1014
1032
return ;
1015
1033
}
1016
1034
1017
1035
invariant ( viteConfig ) ;
1018
1036
1019
1037
let { serverBuildFile, rootDirectory } = remixConfig ;
1020
- let serverBuildDirectory = getServerBuildDirectory ( remixConfig ) ;
1021
1038
let clientBuildDirectory = getClientBuildDirectory ( remixConfig ) ;
1039
+ let serverBuildDirectory = getServerBuildDirectory (
1040
+ remixConfig ,
1041
+ buildContext
1042
+ ) ;
1022
1043
1023
1044
let ssrViteManifest = await loadViteManifest ( serverBuildDirectory ) ;
1024
1045
let clientViteManifest = await loadViteManifest ( clientBuildDirectory ) ;
@@ -1105,8 +1126,8 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
1105
1126
return await getServerEntry ( ) ;
1106
1127
}
1107
1128
case VirtualModule . resolve ( serverManifestId ) : {
1108
- let browserManifest = ssrBuildContext . isSsrBuild
1109
- ? await ssrBuildContext . getBrowserManifest ( )
1129
+ let browserManifest = buildContext . isSsrBuild
1130
+ ? await buildContext . getBrowserManifest ( )
1110
1131
: await getBrowserManifestForDev ( ) ;
1111
1132
1112
1133
return `export default ${ jsesc ( browserManifest , { es6 : true } ) } ;` ;
0 commit comments