1
- import express from "express" ;
2
1
import fs from 'node:fs' ;
3
- import { createRsbuild , loadConfig , logger } from "@rsbuild/core" ;
2
+ import { createRsbuild , loadConfig , logger } from '@rsbuild/core' ;
3
+ import express from 'express' ;
4
4
5
5
const templateHtml = fs . readFileSync ( './template.html' , 'utf-8' ) ;
6
6
7
7
let manifest ;
8
8
9
9
const serverRender = ( serverAPI ) => async ( _req , res ) => {
10
- const indexModule = await serverAPI . environments . ssr . loadBundle ( " index" ) ;
10
+ const indexModule = await serverAPI . environments . node . loadBundle ( ' index' ) ;
11
11
12
12
const markup = indexModule . render ( ) ;
13
13
14
14
const { entries } = JSON . parse ( manifest ) ;
15
15
16
- const { js = [ ] , css = [ ] } = entries [ 'index' ] . initial ;
16
+ const { js = [ ] , css = [ ] } = entries [ 'index' ] . initial ;
17
17
18
- const scriptTags = js
19
- . map ( ( url ) => `<script src="${ url } " defer></script>` )
20
- . join ( '\n' ) ;
21
- const styleTags = css
22
- . map ( ( file ) => `<link rel="stylesheet" href="${ file } ">` )
23
- . join ( '\n' ) ;
18
+ const scriptTags = js . map ( ( url ) => `<script src="${ url } " defer></script>` ) . join ( '\n' ) ;
19
+ const styleTags = css . map ( ( file ) => `<link rel="stylesheet" href="${ file } ">` ) . join ( '\n' ) ;
24
20
25
- const html = templateHtml . replace ( "<!--app-content-->" , markup ) . replace ( '<!--app-head-->' , `${ scriptTags } \n${ styleTags } ` ) ;
21
+ const html = templateHtml
22
+ . replace ( '<!--app-content-->' , markup )
23
+ . replace ( '<!--app-head-->' , `${ scriptTags } \n${ styleTags } ` ) ;
26
24
27
25
res . writeHead ( 200 , {
28
- " Content-Type" : " text/html" ,
26
+ ' Content-Type' : ' text/html' ,
29
27
} ) ;
30
28
res . end ( html ) ;
31
29
} ;
@@ -41,7 +39,7 @@ export async function startDevServer() {
41
39
rsbuild . onDevCompileDone ( async ( ) => {
42
40
// update manifest info when rebuild
43
41
manifest = await fs . promises . readFile ( './dist/manifest.json' , 'utf-8' ) ;
44
- } )
42
+ } ) ;
45
43
46
44
const app = express ( ) ;
47
45
@@ -50,11 +48,12 @@ export async function startDevServer() {
50
48
51
49
const serverRenderMiddleware = serverRender ( rsbuildServer ) ;
52
50
53
- app . get ( "/" , async ( req , res , next ) => {
51
+ app . get ( '/' , async ( req , res , next ) => {
54
52
try {
55
53
await serverRenderMiddleware ( req , res , next ) ;
56
54
} catch ( err ) {
57
- logger . error ( "SSR render error, downgrade to CSR...\n" , err ) ;
55
+ logger . error ( 'SSR render error, downgrade to CSR...' ) ;
56
+ logger . error ( err ) ;
58
57
next ( ) ;
59
58
}
60
59
} ) ;
0 commit comments