@@ -33,7 +33,7 @@ import { RESOLVED_RENDERERS_MODULE_ID } from './plugins/plugin-renderers.js';
33
33
import { RESOLVED_SPLIT_MODULE_ID , RESOLVED_SSR_VIRTUAL_MODULE_ID } from './plugins/plugin-ssr.js' ;
34
34
import { ASTRO_PAGE_EXTENSION_POST_PATTERN } from './plugins/util.js' ;
35
35
import type { StaticBuildOptions } from './types.js' ;
36
- import { encodeName , getTimeStat } from './util.js' ;
36
+ import { encodeName , getTimeStat , viteBuildReturnToRollupOutputs } from './util.js' ;
37
37
38
38
export async function viteBuild ( opts : StaticBuildOptions ) {
39
39
const { allPages, settings } = opts ;
@@ -103,7 +103,9 @@ export async function viteBuild(opts: StaticBuildOptions) {
103
103
// Run client build first, so the assets can be fed into the SSR rendered version.
104
104
const clientOutput = await clientBuild ( opts , internals , clientInput , container ) ;
105
105
106
- await runPostBuildHooks ( container , ssrOutput , clientOutput ) ;
106
+ const ssrOutputs = viteBuildReturnToRollupOutputs ( ssrOutput ) ;
107
+ const clientOutputs = viteBuildReturnToRollupOutputs ( clientOutput ?? [ ] ) ;
108
+ await runPostBuildHooks ( container , ssrOutputs , clientOutputs ) ;
107
109
108
110
settings . timer . end ( 'Client build' ) ;
109
111
@@ -113,23 +115,38 @@ export async function viteBuild(opts: StaticBuildOptions) {
113
115
teardown ( ) ;
114
116
}
115
117
116
- return { internals } ;
118
+ // For static builds, the SSR output output won't be needed anymore after page generation.
119
+ // We keep track of the names here so we only remove these specific files when finished.
120
+ const ssrOutputChunkNames : string [ ] = [ ] ;
121
+ for ( const output of ssrOutputs ) {
122
+ for ( const chunk of output . output ) {
123
+ if ( chunk . type === 'chunk' ) {
124
+ ssrOutputChunkNames . push ( chunk . fileName ) ;
125
+ }
126
+ }
127
+ }
128
+
129
+ return { internals, ssrOutputChunkNames } ;
117
130
}
118
131
119
- export async function staticBuild ( opts : StaticBuildOptions , internals : BuildInternals ) {
132
+ export async function staticBuild (
133
+ opts : StaticBuildOptions ,
134
+ internals : BuildInternals ,
135
+ ssrOutputChunkNames : string [ ]
136
+ ) {
120
137
const { settings } = opts ;
121
138
switch ( true ) {
122
139
case settings . config . output === 'static' : {
123
140
settings . timer . start ( 'Static generate' ) ;
124
141
await generatePages ( opts , internals ) ;
125
- await cleanServerOutput ( opts ) ;
142
+ await cleanServerOutput ( opts , ssrOutputChunkNames ) ;
126
143
settings . timer . end ( 'Static generate' ) ;
127
144
return ;
128
145
}
129
146
case isServerLikeOutput ( settings . config ) : {
130
147
settings . timer . start ( 'Server generate' ) ;
131
148
await generatePages ( opts , internals ) ;
132
- await cleanStaticOutput ( opts , internals ) ;
149
+ await cleanStaticOutput ( opts , internals , ssrOutputChunkNames ) ;
133
150
opts . logger . info ( null , `\n${ bgMagenta ( black ( ' finalizing server assets ' ) ) } \n` ) ;
134
151
await ssrMoveAssets ( opts ) ;
135
152
settings . timer . end ( 'Server generate' ) ;
@@ -324,10 +341,10 @@ async function clientBuild(
324
341
325
342
async function runPostBuildHooks (
326
343
container : AstroBuildPluginContainer ,
327
- ssrReturn : Awaited < ReturnType < typeof ssrBuild > > ,
328
- clientReturn : Awaited < ReturnType < typeof clientBuild > >
344
+ ssrOutputs : vite . Rollup . RollupOutput [ ] ,
345
+ clientOutputs : vite . Rollup . RollupOutput [ ]
329
346
) {
330
- const mutations = await container . runPostHook ( ssrReturn , clientReturn ) ;
347
+ const mutations = await container . runPostHook ( ssrOutputs , clientOutputs ) ;
331
348
const config = container . options . settings . config ;
332
349
const build = container . options . settings . config . build ;
333
350
for ( const [ fileName , mutation ] of mutations ) {
@@ -347,7 +364,11 @@ async function runPostBuildHooks(
347
364
* For each statically prerendered page, replace their SSR file with a noop.
348
365
* This allows us to run the SSR build only once, but still remove dependencies for statically rendered routes.
349
366
*/
350
- async function cleanStaticOutput ( opts : StaticBuildOptions , internals : BuildInternals ) {
367
+ async function cleanStaticOutput (
368
+ opts : StaticBuildOptions ,
369
+ internals : BuildInternals ,
370
+ ssrOutputChunkNames : string [ ]
371
+ ) {
351
372
const allStaticFiles = new Set ( ) ;
352
373
for ( const pageData of eachPageData ( internals ) ) {
353
374
if ( pageData . route . prerender ) {
@@ -361,10 +382,8 @@ async function cleanStaticOutput(opts: StaticBuildOptions, internals: BuildInter
361
382
const out = ssr
362
383
? opts . settings . config . build . server
363
384
: getOutDirWithinCwd ( opts . settings . config . outDir ) ;
364
- // The SSR output is all .mjs files, the client output is not.
365
- const files = await glob ( '**/*.mjs' , {
366
- cwd : fileURLToPath ( out ) ,
367
- } ) ;
385
+ // The SSR output chunks for Astro are all .mjs files
386
+ const files = ssrOutputChunkNames . filter ( ( f ) => f . endsWith ( '.mjs' ) ) ;
368
387
369
388
if ( files . length ) {
370
389
await eslexer . init ;
@@ -394,14 +413,10 @@ async function cleanStaticOutput(opts: StaticBuildOptions, internals: BuildInter
394
413
}
395
414
}
396
415
397
- async function cleanServerOutput ( opts : StaticBuildOptions ) {
416
+ async function cleanServerOutput ( opts : StaticBuildOptions , ssrOutputChunkNames : string [ ] ) {
398
417
const out = getOutDirWithinCwd ( opts . settings . config . outDir ) ;
399
- // The SSR output is all .mjs files, the client output is not.
400
- const files = await glob ( '**/*.mjs' , {
401
- cwd : fileURLToPath ( out ) ,
402
- // Important! Also cleanup dotfiles like `node_modules/.pnpm/**`
403
- dot : true ,
404
- } ) ;
418
+ // The SSR output chunks for Astro are all .mjs files
419
+ const files = ssrOutputChunkNames . filter ( ( f ) => f . endsWith ( '.mjs' ) ) ;
405
420
if ( files . length ) {
406
421
// Remove all the SSR generated .mjs files
407
422
await Promise . all (
0 commit comments