Skip to content

Commit a7a12f1

Browse files
committed
fix(plugin-api-docgen): too much apiDocMap clone in pageData cause performance issue in runtime
1 parent 997be6c commit a7a12f1

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

packages/plugin-api-docgen/src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/// <reference path="../index.d.ts" />
2-
31
import fs from 'node:fs';
42
import path from 'node:path';
53
import type { RspressPlugin } from '@rspress/core';
@@ -41,6 +39,12 @@ export function pluginApiDocgen(options?: PluginOptions): RspressPlugin {
4139
parseToolOptions,
4240
isProd,
4341
});
42+
config.builderConfig = config.builderConfig || {};
43+
config.builderConfig.source = config.builderConfig.source || {};
44+
config.builderConfig.source.define = {
45+
...config.builderConfig.source.define,
46+
RSPRESS_PLUGIN_API_DOCGEN_MAP: JSON.stringify(apiDocMap),
47+
};
4448
},
4549
async modifySearchIndexData(pages) {
4650
// Update the search index of module doc which includes `<API moduleName="foo" />` and `<API moduleName="foo" ></API>
@@ -73,9 +77,6 @@ export function pluginApiDocgen(options?: PluginOptions): RspressPlugin {
7377
}),
7478
);
7579
},
76-
extendPageData(pageData) {
77-
pageData.apiDocMap = { ...apiDocMap };
78-
},
7980
markdown: {
8081
globalComponents: [
8182
path.join(__dirname, '..', 'static', 'global-components', 'API.tsx'),

packages/plugin-api-docgen/static/global-components/API.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
/// <reference path="../../index.d.ts" />
2-
3-
import { useLang, usePageData } from '@rspress/core/runtime';
4-
// @ts-expect-error @theme is overridden by alias in @rspress/core
5-
import { getCustomMDXComponent } from '@theme';
1+
import { useLang } from '@rspress/core/runtime';
2+
import { getCustomMDXComponent } from '@rspress/core/theme';
3+
import GithubSlugger from 'github-slugger';
4+
import type { Content, Element, Root } from 'hast';
65
import ReactMarkdown from 'react-markdown';
76
import remarkGfm from 'remark-gfm';
87
import './API.css';
9-
import GithubSlugger from 'github-slugger';
10-
import type { Content, Element, Root } from 'hast';
118
// biome-ignore lint/style/useImportType: <exact>
129
import React from 'react';
1310
import type { Plugin } from 'unified';
1411
import { visit } from 'unist-util-visit';
1512

13+
declare global {
14+
var RSPRESS_PLUGIN_API_DOCGEN_MAP: Record<string, string>;
15+
}
16+
1617
function headingRank(node: Root | Content): number | null {
1718
const name =
1819
(node && node.type === 'element' && node.tagName.toLowerCase()) || '';
@@ -98,12 +99,11 @@ const collectHeaderText = (node: Element): string => {
9899
return text;
99100
};
100101

101-
export default (props: { moduleName: string }) => {
102+
const API = (props: { moduleName: string }) => {
102103
const lang = useLang();
103-
const { page } = usePageData();
104104
const { moduleName } = props;
105105
// some api doc have two languages.
106-
const apiDocMap = page.apiDocMap;
106+
const apiDocMap = RSPRESS_PLUGIN_API_DOCGEN_MAP;
107107
// avoid error when no page data
108108
const apiDoc =
109109
apiDocMap?.[moduleName] || apiDocMap?.[`${moduleName}-${lang}`] || '';
@@ -122,3 +122,5 @@ export default (props: { moduleName: string }) => {
122122
</div>
123123
);
124124
};
125+
126+
export default API;

0 commit comments

Comments
 (0)