-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathPluginDriver.ts
267 lines (235 loc) · 6.85 KB
/
PluginDriver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { pluginContainerSyntax } from '@rspress/plugin-container-syntax';
import type {
PageIndexInfo,
RouteMeta,
RspressPlugin,
UserConfig,
} from '@rspress/shared';
import { isDevDebugMode } from '@rspress/shared';
import type { RouteService } from './route/RouteService';
type RspressPluginHookKeys =
| 'beforeBuild'
| 'afterBuild'
| 'addPages'
| 'addRuntimeModules'
| 'routeGenerated'
| 'routeServiceGenerated'
| 'addSSGRoutes'
| 'extendPageData'
| 'modifySearchIndexData';
export class PluginDriver {
#config: UserConfig;
#plugins: RspressPlugin[];
#isProd: boolean;
constructor(config: UserConfig, isProd: boolean) {
this.#config = config;
this.#isProd = isProd;
this.#plugins = [];
}
// The init function is used to initialize the doc plugins and will execute before the build process.
async init() {
// Clear RspressPlugins first, for the watch mode
this.clearPlugins();
const config = this.#config;
const themeConfig = config?.themeConfig || {};
const enableLastUpdated =
themeConfig?.lastUpdated ||
themeConfig?.locales?.some(locale => locale.lastUpdated);
const mediumZoomConfig = config?.mediumZoom ?? true;
const haveNavSidebarConfig =
themeConfig.nav ||
themeConfig.sidebar ||
themeConfig.locales?.[0]?.nav ||
themeConfig.locales?.[0]?.sidebar;
if (enableLastUpdated) {
const { pluginLastUpdated } = await import(
'@rspress/plugin-last-updated'
);
this.addPlugin(pluginLastUpdated());
}
if (mediumZoomConfig) {
const { pluginMediumZoom } = await import('@rspress/plugin-medium-zoom');
this.addPlugin(
pluginMediumZoom(
typeof mediumZoomConfig === 'object' ? mediumZoomConfig : undefined,
),
);
}
// Support the container syntax in markdown/mdx, such as :::tip
this.addPlugin(pluginContainerSyntax());
if (isDevDebugMode()) {
const SourceBuildPlugin = await import(
// @ts-ignore just for local dev, so we do not need type
'@rspress/theme-default/node/source-build-plugin.js'
).then(
r => r.SourceBuildPlugin,
() => null as never,
);
if (SourceBuildPlugin) {
this.addPlugin(SourceBuildPlugin());
}
}
(config.plugins || []).forEach(plugin => {
this.addPlugin(plugin);
});
// read _meta.json in the final, allow user's plugin to modify _meta.json
if (!haveNavSidebarConfig) {
const { pluginAutoNavSidebar } = await import(
'@rspress/plugin-auto-nav-sidebar'
);
this.addPlugin(pluginAutoNavSidebar());
}
}
addPlugin(plugin: RspressPlugin) {
const existedIndex = this.#plugins.findIndex(
item => item.name === plugin.name,
);
// Avoid the duplicated plugin
if (existedIndex !== -1) {
throw new Error(`The plugin "${plugin.name}" has been registered`);
}
this.#plugins.push(plugin);
}
getPlugins() {
return this.#plugins;
}
clearPlugins() {
this.#plugins = [];
}
removePlugin(pluginName: string) {
const index = this.#plugins.findIndex(item => item.name === pluginName);
if (index !== -1) {
this.#plugins.splice(index, 1);
}
}
async modifyConfig() {
let config = this.#config;
for (let i = 0; i < this.#plugins.length; i++) {
const plugin = this.#plugins[i];
if (typeof plugin.config === 'function') {
config = await plugin.config(
config || {},
{
addPlugin: this.addPlugin.bind(this),
removePlugin: (pluginName: string) => {
const index = this.#plugins.findIndex(
item => item.name === pluginName,
);
this.removePlugin(pluginName);
if (index <= i && index > 0) {
i--;
}
},
},
this.#isProd,
);
}
}
this.#config = config;
return this.#config;
}
async beforeBuild() {
return this._runParallelAsyncHook(
'beforeBuild',
this.#config || {},
this.#isProd,
);
}
async afterBuild() {
return this._runParallelAsyncHook(
'afterBuild',
this.#config || {},
this.#isProd,
);
}
async modifySearchIndexData(pages: PageIndexInfo[]) {
return this._runParallelAsyncHook(
'modifySearchIndexData',
pages,
this.#isProd,
);
}
async extendPageData(pageData: PageIndexInfo) {
return this._runParallelAsyncHook('extendPageData', pageData, this.#isProd);
}
async addPages() {
const result = await this._runParallelAsyncHook(
'addPages',
this.#config || {},
this.#isProd,
);
return result.flat();
}
async routeGenerated(routes: RouteMeta[]) {
return this._runParallelAsyncHook('routeGenerated', routes, this.#isProd);
}
async routeServiceGenerated(routeService: RouteService) {
return this._runParallelAsyncHook(
'routeServiceGenerated',
routeService,
this.#isProd,
);
}
async addRuntimeModules() {
const result = await this._runParallelAsyncHook(
'addRuntimeModules',
this.#config || {},
this.#isProd,
);
return result.reduce((prev, current) => {
return {
...prev,
...current,
};
}, {});
}
async addSSGRoutes() {
const result = await this._runParallelAsyncHook<'addSSGRoutes'>(
'addSSGRoutes',
this.#config || {},
this.#isProd,
);
return result.flat();
}
globalUIComponents() {
const result = this.#plugins.map(plugin => plugin.globalUIComponents || []);
return result.flat();
}
globalStyles(): string[] {
return this.#plugins
.filter(plugin => typeof plugin.globalStyles === 'string')
.map(plugin => plugin.globalStyles) as string[];
}
_runParallelAsyncHook<H extends RspressPluginHookKeys>(
hookName: H,
...args: Parameters<Required<RspressPlugin>[H]>
): Promise<Awaited<ReturnType<Required<RspressPlugin>[H]>>[]> {
// @ts-expect-error - FIXME: TS is not able to infer the correct type
return Promise.all(
this.#plugins
.filter(plugin => typeof plugin[hookName] === 'function')
.map(plugin =>
plugin[hookName]!(
// @ts-expect-error - FIXME: TS is not able to infer the correct type
...args,
),
),
);
}
_runSerialAsyncHook<H extends RspressPluginHookKeys>(
hookName: H,
...args: Parameters<Required<RspressPlugin>[H]>
) {
// @ts-expect-error - FIXME: TS is not able to infer the correct type
return this.#plugins.reduce(async (prev, plugin) => {
if (typeof plugin[hookName] === 'function') {
await prev;
return plugin[hookName](
// @ts-expect-error - FIXME: TS is not able to infer the correct type
...args,
);
}
return prev;
}, Promise.resolve());
}
}