Skip to content

Commit 0a8f5e9

Browse files
committed
split css from scripts bundle
1 parent 6b9542f commit 0a8f5e9

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

server/htmlBuilder.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ export interface IStats {
55
assetsByChunkName: { [key: string]: string | string[] } | undefined;
66
}
77

8+
export interface IBundle {
9+
file: string;
10+
}
11+
812
export class HtmlBuilder {
913

1014
private stats: IStats | undefined;
1115
private chunkPlaceholder = "<//-CHUNKS-//>";
16+
private stylePlaceholder = "<//-STYLES-//>";
1217
private componentPlaceHolder = "<//-ROOT-//>";
1318
private htmlString = "";
1419

@@ -18,11 +23,21 @@ export class HtmlBuilder {
1823
}
1924

2025
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");
2337

2438
return this.htmlString
25-
.replace(this.chunkPlaceholder, asyncChunks)
39+
.replace(this.chunkPlaceholder, scripts)
40+
.replace(this.stylePlaceholder, styles)
2641
.replace(this.componentPlaceHolder, component);
2742
}
2843

@@ -35,6 +50,7 @@ export class HtmlBuilder {
3550
<title>react-typescript-ssr</title>
3651
${process.env.NODE_ENV === "production" && this.getAsset("vendors", ".css") || ""}
3752
${process.env.NODE_ENV === "production" && this.getAsset("main", ".css") || ""}
53+
${this.stylePlaceholder}
3854
</head>
3955
<body>
4056
<div id="root">${this.componentPlaceHolder}</div>

src/components/pages/About.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background-color: red;
3+
}

src/components/pages/AboutPage.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
22
import { hot } from "react-hot-loader";
3+
import "./About.css";
34

45
export class AboutPage extends React.Component<any, any> {
56
public render() {

0 commit comments

Comments
 (0)