11import type { RsbuildPlugin } from '@rsbuild/core' ;
2- import type { PageData } from '@rspress/shared' ;
32import { logger } from '@rspress/shared/logger' ;
43import { pluginVirtualModule } from 'rsbuild-plugin-virtual-module' ;
54import { type FactoryContext , RuntimeModuleID } from '../types' ;
@@ -11,17 +10,7 @@ export const rsbuildPluginDocVM = async ({
1110 routeService,
1211 pluginDriver,
1312} : Omit < FactoryContext , 'alias' > ) : Promise < RsbuildPlugin [ ] > => {
14- const ref : {
15- pageData : PageData | null ;
16- searchIndex : Record < string , string > | null ;
17- indexHashByGroup : Record < string , string > | null ;
18- filepaths : string [ ] ;
19- } = {
20- pageData : null ,
21- searchIndex : null ,
22- indexHashByGroup : null ,
23- filepaths : [ ] ,
24- } ;
13+ let pageDataResult : Awaited < ReturnType < typeof createPageData > > | undefined ;
2514 let webAlias : Record < string , string > | undefined ;
2615 let refreshPromise : Promise < void > | undefined ;
2716
@@ -31,20 +20,14 @@ export const rsbuildPluginDocVM = async ({
3120 }
3221 refreshPromise ??= ( async ( ) => {
3322 const now = performance . now ( ) ;
34- const { pageData, indexHashByGroup, searchIndex, filepaths } =
35- await createPageData ( {
36- config,
37- alias : webAlias ,
38- userDocRoot,
39- routeService,
40- pluginDriver,
41- } ) ;
23+ pageDataResult = await createPageData ( {
24+ config,
25+ alias : webAlias ,
26+ userDocRoot,
27+ routeService,
28+ pluginDriver,
29+ } ) ;
4230 logger . debug ( `createPageData cost: ${ performance . now ( ) - now } ms` ) ;
43-
44- ref . pageData = pageData ;
45- ref . searchIndex = searchIndex ;
46- ref . indexHashByGroup = indexHashByGroup ;
47- ref . filepaths = filepaths ;
4831 } ) ( ) ;
4932 try {
5033 await refreshPromise ;
@@ -65,11 +48,11 @@ export const rsbuildPluginDocVM = async ({
6548 api . processAssets (
6649 { stage : 'report' , environments : [ 'web' ] } ,
6750 ( { compilation, compiler } ) => {
68- if ( ! ref . searchIndex ) {
51+ if ( ! pageDataResult ) {
6952 return ;
7053 }
7154 for ( const [ filename , stringifiedIndex ] of Object . entries (
72- ref . searchIndex ,
55+ pageDataResult . searchIndex ,
7356 ) ) {
7457 compilation . emitAsset (
7558 `static/${ filename } ` ,
@@ -89,12 +72,12 @@ export const rsbuildPluginDocVM = async ({
8972 virtualModules : {
9073 [ RuntimeModuleID . PageData ] : async ( { addDependency } ) => {
9174 await refreshPageData ( ) ;
92- for ( const file of ref . filepaths ) {
75+ for ( const file of pageDataResult ? .filepaths ?? [ ] ) {
9376 addDependency ( file ) ;
9477 }
9578
96- return `export const pageData = ${ JSON . stringify ( ref . pageData , null , 2 ) } ;
97- export const searchIndexHash = ${ JSON . stringify ( ref . indexHashByGroup , null , 2 ) } ;` ;
79+ return `export const pageData = ${ JSON . stringify ( pageDataResult ? .pageData ?? null , null , 2 ) } ;
80+ export const searchIndexHash = ${ JSON . stringify ( pageDataResult ? .indexHashByGroup ?? null , null , 2 ) } ;` ;
9881 } ,
9982 } ,
10083 } ) ,
0 commit comments