Skip to content

Commit 997be6c

Browse files
authored
fix(virtual-module): should addDependencies in virtual-module for persistent cache (#2519)
1 parent 51e78ea commit 997be6c

File tree

12 files changed

+45
-34
lines changed

12 files changed

+45
-34
lines changed

packages/core/src/node/runtimeModule/pageData/createPageData.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { type PageData, SEARCH_INDEX_NAME } from '@rspress/shared';
22
import { groupBy } from 'lodash-es';
3-
import { isProduction } from '../../constants';
43
import { extractPageData } from '../../route/extractPageData';
54
import { createHash } from '../../utils';
65
import type { FactoryContext } from '../types';
@@ -19,6 +18,7 @@ function deletePrivateField<T>(obj: T): T {
1918
}
2019

2120
export async function createPageData(context: FactoryContext): Promise<{
21+
filepaths: string[]; // for addDependencies
2222
pageData: PageData;
2323
searchIndex: Record<string, string>;
2424
indexHashByGroup: Record<string, string>;
@@ -93,6 +93,7 @@ export async function createPageData(context: FactoryContext): Promise<{
9393
pages.map(async pageData => pluginDriver.extendPageData(pageData)),
9494
);
9595

96+
const filepaths: string[] = [];
9697
const pageData: PageData = {
9798
pages: pages.map(page => {
9899
// omit some fields for runtime size
@@ -103,13 +104,13 @@ export async function createPageData(context: FactoryContext): Promise<{
103104
_flattenContent,
104105
...rest
105106
} = page;
106-
// FIXME: should not have differences from development
107-
// In production, we cannot expose the complete filepath for security reasons
108-
return isProduction() ? rest : { ...rest, _filepath };
107+
filepaths.push(_filepath);
108+
return rest;
109109
}),
110110
};
111111

112112
return {
113+
filepaths,
113114
pageData,
114115
searchIndex,
115116
indexHashByGroup,

packages/core/src/node/runtimeModule/pageData/rsbuildPlugin.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ export const rsbuildPluginDocVM = async ({
1414
pageData: PageData | null;
1515
searchIndex: Record<string, string> | null;
1616
indexHashByGroup: Record<string, string> | null;
17-
} = { pageData: null, searchIndex: null, indexHashByGroup: null };
17+
filepaths: string[];
18+
} = {
19+
pageData: null,
20+
searchIndex: null,
21+
indexHashByGroup: null,
22+
filepaths: [],
23+
};
1824
const searchIndexRsbuildPlugin: RsbuildPlugin = {
1925
name: 'rsbuild-plugin-searchIndex',
2026
async setup(api) {
2127
api.modifyBundlerChain(async bundlerChain => {
2228
const alias = bundlerChain.resolve.alias.entries();
23-
const { pageData, indexHashByGroup, searchIndex } =
29+
const { pageData, indexHashByGroup, searchIndex, filepaths } =
2430
await createPageData({
2531
config,
2632
alias: alias as Record<string, string>,
@@ -32,6 +38,8 @@ export const rsbuildPluginDocVM = async ({
3238
ref.pageData = pageData;
3339
ref.searchIndex = searchIndex;
3440
ref.indexHashByGroup = indexHashByGroup;
41+
ref.filepaths = filepaths;
42+
3543
api.processAssets(
3644
{ stage: 'report', environments: ['web'] },
3745
({ compilation, compiler }) => {
@@ -54,10 +62,16 @@ export const rsbuildPluginDocVM = async ({
5462
pluginVirtualModule({
5563
tempDir: '.rspress',
5664
virtualModules: {
57-
[RuntimeModuleID.PageData]: () =>
58-
`export default ${JSON.stringify(ref.pageData, null, 2)}`,
59-
[RuntimeModuleID.SearchIndexHash]: () =>
60-
`export default ${JSON.stringify(ref.indexHashByGroup, null, 2)}`,
65+
[RuntimeModuleID.PageData]: async ({ addDependency }) => {
66+
// TODO: support hmr
67+
// This place needs to obtain the specific file that has been modified and update the file information.
68+
for (const file of ref.filepaths) {
69+
addDependency(file);
70+
}
71+
72+
return `export const pageData = ${JSON.stringify(ref.pageData, null, 2)};
73+
export const searchIndexHash = ${JSON.stringify(ref.indexHashByGroup, null, 2)};`;
74+
},
6175
},
6276
}),
6377
];

packages/core/src/node/runtimeModule/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export enum RuntimeModuleID {
2121
Routes = 'virtual-routes',
2222
SiteData = 'virtual-site-data',
2323
PageData = 'virtual-page-data',
24-
SearchIndexHash = 'virtual-search-index-hash',
2524
socialLinks = 'virtual-social-links',
2625
I18nText = 'virtual-i18n-text',
2726
SearchHooks = 'virtual-search-hooks',

packages/core/src/runtime/env.d.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@ declare module 'virtual-site-data' {
1313

1414
declare module 'virtual-page-data' {
1515
import { PageData } from '@rspress/shared';
16-
17-
const data: PageData;
18-
export default data;
19-
}
20-
21-
declare module 'virtual-search-index-hash' {
22-
const hash: string;
23-
export default hash;
16+
const searchIndexHash: Record<string, string>;
17+
const pageData: PageData;
18+
export { pageData, searchIndexHash };
2419
}
2520

2621
declare module 'virtual-global-components' {

packages/core/src/runtime/initPageData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
MDX_OR_MD_REGEXP,
88
type PageDataLegacy,
99
} from '@rspress/shared';
10-
import pageData from 'virtual-page-data';
10+
import { pageData } from 'virtual-page-data';
1111
import siteData from 'virtual-site-data';
1212

1313
type PageMeta = {

packages/runtime/rslib.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default defineConfig({
1616
externals: [
1717
'@theme',
1818
'virtual-routes',
19-
'virtual-search-index-hash',
19+
'virtual-page-data',
2020
'virtual-site-data',
2121
'virtual-global-styles',
2222
'virtual-global-components',

packages/runtime/src/env.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ declare module 'virtual-site-data' {
1111

1212
declare module 'virtual-page-data' {
1313
import { PageData } from '@rspress/shared';
14-
15-
const data: PageData;
16-
export default data;
14+
const searchIndexHash: Record<string, string>;
15+
const pageData: PageData;
16+
export { pageData, searchIndexHash };
1717
}
1818

1919
declare module 'virtual-i18n-text';
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { PageData } from '@rspress/shared';
2-
import pages from 'virtual-page-data';
2+
import { pageData } from 'virtual-page-data';
33

44
export function usePages(): { pages: PageData['pages'] } {
5-
return { pages: pages.pages };
5+
return { pages: pageData.pages };
66
}

packages/theme-default/rslib.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { pluginPublint } from 'rsbuild-plugin-publint';
66

77
const COMMON_EXTERNALS = [
88
'virtual-routes',
9-
'virtual-search-index-hash',
109
'virtual-site-data',
1110
'virtual-global-styles',
1211
'virtual-global-components',

packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Index, {
1313
type EnrichedDocumentSearchResultSetUnit,
1414
type IndexOptionsForDocumentSearch,
1515
} from 'flexsearch';
16-
import searchIndexHash from 'virtual-search-index-hash';
16+
import { searchIndexHash } from 'virtual-page-data';
1717
import { LOCAL_INDEX, type Provider, type SearchQuery } from '../Provider';
1818
import type { SearchOptions } from '../types';
1919
import { normalizeTextCase } from '../util';

0 commit comments

Comments
 (0)