@@ -16,7 +16,9 @@ import { getCritters } from './critial'
1616import { render } from './server'
1717import { SCRIPT_COMMENT_PLACEHOLDER , detectEntry , renderHTML } from './html'
1818import { renderPreloadLinks } from './preload-links'
19+ import { collectAssets } from './assets'
1920
21+ const dotVitedir = Number . parseInt ( viteVersion ) >= 5 ? [ '.vite' ] : [ ]
2022export type SSRManifest = Record < string , string [ ] >
2123export interface ManifestItem {
2224 css ?: string [ ]
@@ -111,6 +113,7 @@ export async function build(ssgOptions: Partial<ViteReactSSGOptions> = {}, viteC
111113 await viteBuild ( mergeConfig ( viteConfig , {
112114 build : {
113115 ssr : ssrEntry ,
116+ manifest : true ,
114117 outDir : ssgOut ,
115118 minify : false ,
116119 cssCodeSplit : false ,
@@ -133,6 +136,7 @@ export async function build(ssgOptions: Partial<ViteReactSSGOptions> = {}, viteC
133136 const prefix = ( format === 'esm' && process . platform === 'win32' ) ? 'file://' : ''
134137 const ext = format === 'esm' ? '.mjs' : '.cjs'
135138 const serverEntry = join ( prefix , ssgOut , parse ( ssrEntry ) . name + ext )
139+ const serverManifest : Manifest = JSON . parse ( await fs . readFile ( join ( ssgOut , ...dotVitedir , 'manifest.json' ) , 'utf-8' ) )
136140
137141 const _require = createRequire ( import . meta. url )
138142
@@ -142,7 +146,7 @@ export async function build(ssgOptions: Partial<ViteReactSSGOptions> = {}, viteC
142146 const includedRoutes = serverEntryIncludedRoutes || configIncludedRoutes
143147 const { routes } = await createRoot ( false )
144148
145- const { paths, pathToEntry } = await routesToPaths ( routes )
149+ const { paths } = await routesToPaths ( routes )
146150
147151 let routesPaths = includeAllRoutes
148152 ? paths
@@ -158,7 +162,6 @@ export async function build(ssgOptions: Partial<ViteReactSSGOptions> = {}, viteC
158162 if ( critters )
159163 console . log ( `${ gray ( '[vite-react-ssg]' ) } ${ blue ( 'Critical CSS generation enabled via `critters`' ) } ` )
160164
161- const dotVitedir = Number . parseInt ( viteVersion ) >= 5 ? [ '.vite' ] : [ ]
162165 const ssrManifest : SSRManifest = JSON . parse ( await fs . readFile ( join ( out , ...dotVitedir , 'ssr-manifest.json' ) , 'utf-8' ) )
163166 const manifest : Manifest = JSON . parse ( await fs . readFile ( join ( out , ...dotVitedir , 'manifest.json' ) , 'utf-8' ) )
164167 let indexHTML = await fs . readFile ( join ( out , 'index.html' ) , 'utf-8' )
@@ -182,6 +185,8 @@ export async function build(ssgOptions: Partial<ViteReactSSGOptions> = {}, viteC
182185 const fetchUrl = `${ withTrailingSlash ( base ) } ${ removeLeadingSlash ( path ) } `
183186 const request = createRequest ( fetchUrl )
184187
188+ const assets = ! app ? collectAssets ( { routes : [ ...routes ] , locationArg : fetchUrl , base, serverManifest, manifest, ssrManifest } ) : new Set < string > ( )
189+
185190 const { appHTML, bodyAttributes, htmlAttributes, metaAttributes, styleTag, routerContext } = await render ( app ?? [ ...routes ] , request , styleCollector , base )
186191 staticLoaderDataManifest [ path ] = routerContext ?. loaderData
187192
@@ -199,9 +204,7 @@ export async function build(ssgOptions: Partial<ViteReactSSGOptions> = {}, viteC
199204
200205 const jsdom = new JSDOM ( renderedHTML )
201206
202- const modules = collectModulesForEntrys ( manifest , pathToEntry ?. [ path ] )
203-
204- renderPreloadLinks ( jsdom . window . document , modules , ssrManifest )
207+ renderPreloadLinks ( jsdom . window . document , assets )
205208
206209 const html = jsdom . serialize ( )
207210 let transformed = ( await onPageRendered ?.( path , html , appCtx ) ) || html
@@ -298,26 +301,3 @@ async function formatHtml(html: string, formatting: ViteReactSSGOptions['formatt
298301 }
299302 return html
300303}
301-
302- function collectModulesForEntrys ( manifest : Manifest , entrys : Set < string > | undefined ) {
303- const mods = new Set < string > ( )
304- if ( ! entrys )
305- return mods
306-
307- for ( const entry of entrys )
308- collectModules ( manifest , entry , mods )
309-
310- return mods
311- }
312-
313- function collectModules ( manifest : Manifest , entry : string | undefined , mods = new Set < string > ( ) ) {
314- if ( ! entry )
315- return mods
316-
317- mods . add ( entry )
318- manifest [ entry ] ?. dynamicImports ?. forEach ( item => {
319- collectModules ( manifest , item , mods )
320- } )
321-
322- return mods
323- }
0 commit comments