11import { manifest } from "solid-start:server-manifest"
22import { join } from "pathe" ;
33
4+ // Only reads from client manifest atm, might need server support for islands
45export function getSsrProdManifest ( ) {
56 const viteManifest = manifest . clientViteManifest ;
67 return {
78 import ( id ) {
89 return import ( /* @vite -ignore */ id ) ;
910 } ,
1011 async getAssets ( id ) {
11- return [ ]
12+ return createHtmlTagsForAssets (
13+ findAssetsInViteManifest ( manifest . clientViteManifest , id ) ,
14+ ) ;
1215 } ,
1316 async json ( ) {
1417 const json : Record < string , any > = { } ;
@@ -19,11 +22,66 @@ export function getSsrProdManifest() {
1922
2023 for ( const entryKey of entryKeys ) {
2124 json [ entryKey ] = {
22- output : join ( viteManifest [ entryKey ] ! . file )
23- }
25+ output : join ( "/" , CLIENT_BASE_PATH , viteManifest [ entryKey ] ! . file ) ,
26+ assets : await this . getAssets ( entryKey )
27+ } ;
2428 }
2529
2630 return json
2731 }
2832 } satisfies StartManifest & { json ( ) : Promise < Record < string , any > > } ;
2933}
34+
35+ const CLIENT_BASE_PATH = "_build" ;
36+
37+ function createHtmlTagsForAssets ( assets : string [ ] ) {
38+ return assets
39+ . filter (
40+ ( asset ) =>
41+ asset . endsWith ( ".css" ) ||
42+ asset . endsWith ( ".js" ) ||
43+ asset . endsWith ( ".mjs" ) ,
44+ )
45+ . map ( ( asset ) => ( {
46+ tag : "link" ,
47+ attrs : {
48+ href : join ( "/" , CLIENT_BASE_PATH , asset ) ,
49+ key : join ( "/" , CLIENT_BASE_PATH , asset ) ,
50+ ...( asset . endsWith ( ".css" )
51+ ? { rel : "stylesheet" , fetchPriority : "high" }
52+ : { rel : "modulepreload" } ) ,
53+ } ,
54+ } ) ) ;
55+ }
56+
57+ function findAssetsInViteManifest ( manifest : any , id : string , assetMap = new Map ( ) , stack : string [ ] = [ ] ) {
58+ if ( stack . includes ( id ) ) {
59+ return [ ] ;
60+ }
61+
62+ const cached = assetMap . get ( id ) ;
63+ if ( cached ) {
64+ return cached ;
65+ }
66+ const chunk = manifest [ id ] ;
67+ if ( ! chunk ) {
68+ return [ ] ;
69+ }
70+
71+ const assets = [
72+ ...( chunk . assets ?. filter ( Boolean ) || [ ] ) ,
73+ ...( chunk . css ?. filter ( Boolean ) || [ ] )
74+ ] ;
75+ if ( chunk . imports ) {
76+ stack . push ( id ) ;
77+ for ( let i = 0 , l = chunk . imports . length ; i < l ; i ++ ) {
78+ assets . push ( ...findAssetsInViteManifest ( manifest , chunk . imports [ i ] , assetMap , stack ) ) ;
79+ }
80+ stack . pop ( ) ;
81+ }
82+ assets . push ( chunk . file ) ;
83+ const all = Array . from ( new Set ( assets ) ) ;
84+ assetMap . set ( id , all ) ;
85+
86+ return all ;
87+ }
0 commit comments