@@ -25,45 +25,52 @@ export function build_server_nodes(out, kit, manifest_data, server_manifest, cli
25
25
/** @type {Set<string> } */
26
26
const client_stylesheets = new Set ( ) ;
27
27
for ( const key in client_manifest ) {
28
- const file = client_manifest [ key ] ;
29
- if ( file . css ?. [ 0 ] ) {
30
- client_stylesheets . add ( file . css [ 0 ] ) ;
31
- }
28
+ client_manifest [ key ] . css ?. forEach ( ( filename ) => {
29
+ client_stylesheets . add ( filename ) ;
30
+ } ) ;
32
31
}
33
32
34
- /** @type {Map<number, string> } */
33
+ /** @type {Map<number, string[] > } */
35
34
const server_stylesheets = new Map ( ) ;
35
+ manifest_data . nodes . forEach ( ( node , i ) => {
36
+ if ( ! node . component || ! server_manifest [ node . component ] ) return ;
36
37
37
- const component_stylesheet_map = new Map ( Object . values ( server_manifest ) . map ( ( file ) => [ file . src , file . css ?. [ 0 ] ] ) ) ;
38
+ const { stylesheets } = find_deps ( server_manifest , node . component , false ) ;
38
39
39
- manifest_data . nodes . forEach ( ( node , i ) => {
40
- const server_stylesheet = component_stylesheet_map . get ( node . component ) ;
41
- if ( node . component && server_stylesheet ) {
42
- server_stylesheets . set ( i , server_stylesheet ) ;
40
+ if ( stylesheets . length ) {
41
+ server_stylesheets . set ( i , stylesheets ) ;
43
42
}
44
43
} ) ;
45
44
46
- // ignore dynamically imported stylesheets since we can't inline those
47
- css . filter ( asset => client_stylesheets . has ( asset . fileName ) )
48
- . forEach ( ( asset ) => {
49
- if ( asset . source . length < kit . inlineStyleThreshold ) {
50
- // We know that the names for entry points are numbers.
51
- const [ index ] = basename ( asset . fileName ) . split ( '.' ) ;
52
- // There can also be other CSS files from shared components
53
- // for example, which we need to ignore here.
54
- if ( isNaN ( + index ) ) return ;
55
-
56
- const server_stylesheet = server_stylesheets . get ( + index ) ;
57
- const file = `${ out } /server/stylesheets/${ index } .js` ;
58
-
59
- // we need to inline the server stylesheet instead of the client one
60
- // so that asset paths are correct on document load
61
- const source = fs . readFileSync ( `${ out } /server/${ server_stylesheet } ` , 'utf-8' ) ;
62
-
63
- fs . writeFileSync ( file , `// ${ server_stylesheet } \nexport default ${ s ( source ) } ;` ) ;
64
- stylesheet_lookup . set ( asset . fileName , index ) ;
65
- }
45
+ for ( const asset of css ) {
46
+ // ignore dynamically imported stylesheets since we don't need to inline those
47
+ if ( ! client_stylesheets . has ( asset . fileName ) || asset . source . length >= kit . inlineStyleThreshold ) {
48
+ continue ;
49
+ }
50
+
51
+ // We know that the names for entry points are numbers.
52
+ const [ index ] = basename ( asset . fileName ) . split ( '.' ) ;
53
+ // There can also be other CSS files from shared components
54
+ // for example, which we need to ignore here.
55
+ if ( isNaN ( + index ) ) continue ;
56
+
57
+ const file = `${ out } /server/stylesheets/${ index } .js` ;
58
+
59
+ // we need to inline the server stylesheet instead of the client one
60
+ // so that asset paths are correct on document load
61
+ const filenames = server_stylesheets . get ( + index ) ;
62
+
63
+ if ( ! filenames ) {
64
+ throw new Error ( 'This should never happen, but if it does, it means we failed to find the server stylesheet for a node.' ) ;
65
+ }
66
+
67
+ const sources = filenames . map ( ( filename ) => {
68
+ return fs . readFileSync ( `${ out } /server/${ filename } ` , 'utf-8' ) ;
66
69
} ) ;
70
+ fs . writeFileSync ( file , `// ${ filenames . join ( ', ' ) } \nexport default ${ s ( sources . join ( '\n' ) ) } ;` ) ;
71
+
72
+ stylesheet_lookup . set ( asset . fileName , index ) ;
73
+ }
67
74
}
68
75
69
76
manifest_data . nodes . forEach ( ( node , i ) => {
0 commit comments