@@ -5,10 +5,15 @@ export interface IStats {
5
5
assetsByChunkName : { [ key : string ] : string | string [ ] } | undefined ;
6
6
}
7
7
8
+ export interface IBundle {
9
+ file : string ;
10
+ }
11
+
8
12
export class HtmlBuilder {
9
13
10
14
private stats : IStats | undefined ;
11
15
private chunkPlaceholder = "<//-CHUNKS-//>" ;
16
+ private stylePlaceholder = "<//-STYLES-//>" ;
12
17
private componentPlaceHolder = "<//-ROOT-//>" ;
13
18
private htmlString = "" ;
14
19
@@ -18,11 +23,21 @@ export class HtmlBuilder {
18
23
}
19
24
20
25
public renderToString ( component : string , modules : any ) {
21
- const asyncChunks : string = getBundles ( this . stats , modules )
22
- . map ( ( bundle : any ) => this . buildTag ( bundle . file ) ) . join ( "\n" ) ;
26
+ const bundles : IBundle [ ] = getBundles ( this . stats , modules ) ;
27
+
28
+ const scripts : string = bundles
29
+ . filter ( ( bundle : IBundle ) => bundle . file . endsWith ( ".js" ) )
30
+ . map ( ( bundle ) => this . buildTag ( bundle . file ) )
31
+ . join ( "\n" ) ;
32
+
33
+ const styles : string = bundles
34
+ . filter ( ( bundle : any ) => bundle . file . endsWith ( ".css" ) )
35
+ . map ( ( bundle ) => this . buildStyle ( bundle . file ) )
36
+ . join ( "\n" ) ;
23
37
24
38
return this . htmlString
25
- . replace ( this . chunkPlaceholder , asyncChunks )
39
+ . replace ( this . chunkPlaceholder , scripts )
40
+ . replace ( this . stylePlaceholder , styles )
26
41
. replace ( this . componentPlaceHolder , component ) ;
27
42
}
28
43
@@ -35,6 +50,7 @@ export class HtmlBuilder {
35
50
<title>react-typescript-ssr</title>
36
51
${ process . env . NODE_ENV === "production" && this . getAsset ( "vendors" , ".css" ) || "" }
37
52
${ process . env . NODE_ENV === "production" && this . getAsset ( "main" , ".css" ) || "" }
53
+ ${ this . stylePlaceholder }
38
54
</head>
39
55
<body>
40
56
<div id="root">${ this . componentPlaceHolder } </div>
0 commit comments