Skip to content

Commit f288fe8

Browse files
authored
fix(plugin-api-docgen): too much apiDocMap clone in pageData cause performance issue in runtime (#2524)
1 parent 4f2383e commit f288fe8

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

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

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/plugin-api-docgen/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
"types": "./dist/index.d.ts",
2020
"files": [
2121
"dist",
22-
"static",
23-
"index.d.ts"
22+
"static"
2423
],
2524
"scripts": {
2625
"build": "rslib build",

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: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
/// <reference path="../../index.d.ts" />
2-
3-
import { useLang, usePageData } from '@rspress/core/runtime';
1+
import { useLang } from '@rspress/core/runtime';
42
// @ts-expect-error @theme is overridden by alias in @rspress/core
53
import { getCustomMDXComponent } from '@theme';
4+
import GithubSlugger from 'github-slugger';
5+
import type { Content, Element, Root } from 'hast';
66
import ReactMarkdown from 'react-markdown';
77
import remarkGfm from 'remark-gfm';
88
import './API.css';
9-
import GithubSlugger from 'github-slugger';
10-
import type { Content, Element, Root } from 'hast';
119
// biome-ignore lint/style/useImportType: <exact>
1210
import React from 'react';
1311
import type { Plugin } from 'unified';
1412
import { visit } from 'unist-util-visit';
1513

14+
declare global {
15+
var RSPRESS_PLUGIN_API_DOCGEN_MAP: Record<string, string>;
16+
}
17+
1618
function headingRank(node: Root | Content): number | null {
1719
const name =
1820
(node && node.type === 'element' && node.tagName.toLowerCase()) || '';
@@ -98,12 +100,11 @@ const collectHeaderText = (node: Element): string => {
98100
return text;
99101
};
100102

101-
export default (props: { moduleName: string }) => {
103+
const API = (props: { moduleName: string }) => {
102104
const lang = useLang();
103-
const { page } = usePageData();
104105
const { moduleName } = props;
105106
// some api doc have two languages.
106-
const apiDocMap = page.apiDocMap;
107+
const apiDocMap = RSPRESS_PLUGIN_API_DOCGEN_MAP;
107108
// avoid error when no page data
108109
const apiDoc =
109110
apiDocMap?.[moduleName] || apiDocMap?.[`${moduleName}-${lang}`] || '';
@@ -122,3 +123,5 @@ export default (props: { moduleName: string }) => {
122123
</div>
123124
);
124125
};
126+
127+
export default API;

0 commit comments

Comments
 (0)