@@ -104,21 +104,26 @@ export async function viteBuild(opts: StaticBuildOptions) {
104
104
// For static builds, the SSR output won't be needed anymore after page generation.
105
105
// We keep track of the names here so we only remove these specific files when finished.
106
106
const ssrOutputChunkNames : string [ ] = [ ] ;
107
+ const ssrOutputAssetNames : string [ ] = [ ] ;
107
108
for ( const output of ssrOutputs ) {
108
109
for ( const chunk of output . output ) {
109
110
if ( chunk . type === 'chunk' ) {
110
111
ssrOutputChunkNames . push ( chunk . fileName ) ;
111
112
}
113
+ if ( chunk . type === 'asset' ) {
114
+ ssrOutputAssetNames . push ( chunk . fileName ) ;
115
+ }
112
116
}
113
117
}
114
118
115
- return { internals, ssrOutputChunkNames, contentFileNames } ;
119
+ return { internals, ssrOutputChunkNames, ssrOutputAssetNames , contentFileNames } ;
116
120
}
117
121
118
122
export async function staticBuild (
119
123
opts : StaticBuildOptions ,
120
124
internals : BuildInternals ,
121
125
ssrOutputChunkNames : string [ ] ,
126
+ ssrOutputAssetNames : string [ ] ,
122
127
contentFileNames ?: string [ ] ,
123
128
) {
124
129
const { settings } = opts ;
@@ -131,7 +136,7 @@ export async function staticBuild(
131
136
settings . timer . start ( 'Server generate' ) ;
132
137
await generatePages ( opts , internals ) ;
133
138
await cleanStaticOutput ( opts , internals ) ;
134
- await ssrMoveAssets ( opts ) ;
139
+ await ssrMoveAssets ( opts , ssrOutputAssetNames ) ;
135
140
settings . timer . end ( 'Server generate' ) ;
136
141
}
137
142
}
@@ -412,33 +417,26 @@ export async function copyFiles(fromFolder: URL, toFolder: URL, includeDotfiles
412
417
) ;
413
418
}
414
419
415
- async function ssrMoveAssets ( opts : StaticBuildOptions ) {
420
+ async function ssrMoveAssets ( opts : StaticBuildOptions , ssrOutputAssetNames : string [ ] ) {
416
421
opts . logger . info ( 'build' , 'Rearranging server assets...' ) ;
417
422
const serverRoot =
418
423
opts . settings . buildOutput === 'static'
419
424
? opts . settings . config . build . client
420
425
: opts . settings . config . build . server ;
421
426
const clientRoot = opts . settings . config . build . client ;
422
- const assets = opts . settings . config . build . assets ;
423
- const serverAssets = new URL ( `./${ assets } /` , appendForwardSlash ( serverRoot . toString ( ) ) ) ;
424
- const clientAssets = new URL ( `./${ assets } /` , appendForwardSlash ( clientRoot . toString ( ) ) ) ;
425
- const files = await glob ( `**/*` , {
426
- cwd : fileURLToPath ( serverAssets ) ,
427
- } ) ;
428
-
429
- if ( files . length > 0 ) {
430
- await Promise . all (
431
- files . map ( async function moveAsset ( filename ) {
432
- const currentUrl = new URL ( filename , appendForwardSlash ( serverAssets . toString ( ) ) ) ;
433
- const clientUrl = new URL ( filename , appendForwardSlash ( clientAssets . toString ( ) ) ) ;
434
- const dir = new URL ( path . parse ( clientUrl . href ) . dir ) ;
435
- // It can't find this file because the user defines a custom path
436
- // that includes the folder paths in `assetFileNames
437
- if ( ! fs . existsSync ( dir ) ) await fs . promises . mkdir ( dir , { recursive : true } ) ;
438
- return fs . promises . rename ( currentUrl , clientUrl ) ;
439
- } ) ,
440
- ) ;
441
- removeEmptyDirs ( fileURLToPath ( serverAssets ) ) ;
427
+ if ( ssrOutputAssetNames . length > 0 ) {
428
+ await Promise . all (
429
+ ssrOutputAssetNames . map ( async function moveAsset ( filename ) {
430
+ const currentUrl = new URL ( filename , appendForwardSlash ( serverRoot . toString ( ) ) ) ;
431
+ const clientUrl = new URL ( filename , appendForwardSlash ( clientRoot . toString ( ) ) ) ;
432
+ const dir = new URL ( path . parse ( clientUrl . href ) . dir ) ;
433
+ // It can't find this file because the user defines a custom path
434
+ // that includes the folder paths in `assetFileNames`
435
+ if ( ! fs . existsSync ( dir ) ) await fs . promises . mkdir ( dir , { recursive : true } ) ;
436
+ return fs . promises . rename ( currentUrl , clientUrl ) ;
437
+ } ) ,
438
+ ) ;
439
+ removeEmptyDirs ( fileURLToPath ( serverRoot ) ) ;
442
440
}
443
441
}
444
442
0 commit comments