1+ import fs from 'node:fs/promises' ;
12import { dirname , join } from 'node:path' ;
23import { pathToFileURL } from 'node:url' ;
34import type { Route } from '@/node/route/RouteService' ;
@@ -11,7 +12,6 @@ import {
1112 withBase ,
1213} from '@rspress/shared' ;
1314import chalk from '@rspress/shared/chalk' ;
14- import fs from '@rspress/shared/fs-extra' ;
1515import { logger } from '@rspress/shared/logger' ;
1616import type { HelmetData } from 'react-helmet-async' ;
1717import { PluginDriver } from './PluginDriver' ;
@@ -50,13 +50,18 @@ export async function bundle(
5050 pluginDriver ,
5151 enableSSG ,
5252 ) ;
53+
5354 await rsbuild . build ( ) ;
5455 } finally {
5556 await writeSearchIndex ( config ) ;
5657 await checkLanguageParity ( config ) ;
5758 }
5859}
5960
61+ function emptyDir ( path : string ) : Promise < void > {
62+ return fs . rm ( path , { force : true , recursive : true } ) ;
63+ }
64+
6065export interface SSRBundleExports {
6166 render : (
6267 url : string ,
@@ -77,7 +82,6 @@ export async function renderPages(
7782 const ssrBundlePath = join ( outputPath , 'ssr' , 'main.cjs' ) ;
7883
7984 try {
80- const { default : fs } = await import ( '@rspress/shared/fs-extra' ) ;
8185 const { version } = await import ( '../../package.json' ) ;
8286 // There are two cases where we will fallback to CSR:
8387 // 1. ssr bundle load failed
@@ -204,15 +208,17 @@ export async function renderPages(
204208 return `${ path } .html` . replace ( normalizedBase , '' ) ;
205209 } ;
206210 const fileName = normalizeHtmlFilePath ( routePath ) ;
207- await fs . ensureDir ( join ( outputPath , dirname ( fileName ) ) ) ;
211+ await fs . mkdir ( join ( outputPath , dirname ( fileName ) ) , {
212+ recursive : true ,
213+ } ) ;
208214 await fs . writeFile ( join ( outputPath , fileName ) , html ) ;
209215 } ) ,
210216 ) ;
211217 // Remove ssr bundle
212218 if ( ! isDebugMode ( ) ) {
213- await fs . remove ( join ( outputPath , 'ssr' ) ) ;
219+ await emptyDir ( join ( outputPath , 'ssr' ) ) ;
214220 }
215- await fs . remove ( join ( outputPath , 'html' ) ) ;
221+ await emptyDir ( join ( outputPath , 'html' ) ) ;
216222
217223 const totalTime = Date . now ( ) - startTime ;
218224 logger . success ( `Pages rendered in ${ chalk . yellow ( totalTime ) } ms.` ) ;
@@ -227,13 +233,15 @@ export async function build(options: BuildOptions) {
227233 const pluginDriver = new PluginDriver ( config , true ) ;
228234 await pluginDriver . init ( ) ;
229235 const modifiedConfig = await pluginDriver . modifyConfig ( ) ;
236+
230237 await pluginDriver . beforeBuild ( ) ;
231238 const ssgConfig = modifiedConfig . ssg ?? true ;
232239
233240 // empty temp dir before build
234- await fs . emptyDir ( TEMP_DIR ) ;
241+ await emptyDir ( TEMP_DIR ) ;
235242
236243 await bundle ( docDirectory , modifiedConfig , pluginDriver , Boolean ( ssgConfig ) ) ;
244+
237245 await renderPages ( appDirectory , modifiedConfig , pluginDriver , ssgConfig ) ;
238246 await pluginDriver . afterBuild ( ) ;
239247}
0 commit comments