Skip to content

Commit fc125cb

Browse files
authored
refactor!: remove builderPlugins and migrate it to builderConfig.plugins (#2371)
1 parent d54e0f0 commit fc125cb

File tree

10 files changed

+119
-95
lines changed

10 files changed

+119
-95
lines changed

packages/core/src/node/initRsbuild.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import {
3030
inlineThemeScript,
3131
isProduction,
3232
} from './constants';
33-
import { hintThemeBreakingChange } from './logger/hint';
33+
import {
34+
hintBuilderPluginsBreakingChange,
35+
hintThemeBreakingChange,
36+
} from './logger/hint';
3437
import { RouteService } from './route/RouteService';
3538
import {
3639
getVirtualModulesFromPlugins,
@@ -55,10 +58,9 @@ import { getSocialIcons } from './utils/getSocialIcons';
5558

5659
function isPluginIncluded(config: UserConfig, pluginName: string): boolean {
5760
return Boolean(
58-
config.builderPlugins?.some(plugin => plugin.name === pluginName) ||
59-
config.builderConfig?.plugins?.some(
60-
plugin => plugin && (plugin as RsbuildPlugin).name === pluginName,
61-
),
61+
config.builderConfig?.plugins?.some(
62+
plugin => plugin && (plugin as RsbuildPlugin).name === pluginName,
63+
),
6264
);
6365
}
6466

@@ -380,14 +382,13 @@ async function createInternalBuildConfig(
380382

381383
export async function initRsbuild(
382384
rootDir: string,
383-
_config: UserConfig,
385+
config: UserConfig,
384386
pluginDriver: PluginDriver,
385387
enableSSG: boolean,
386388
extraRsbuildConfig?: RsbuildConfig,
387389
): Promise<RsbuildInstance> {
388390
const cwd = process.cwd();
389-
const userDocRoot = path.resolve(rootDir || _config?.root || cwd);
390-
const builderPlugins = _config?.builderPlugins ?? [];
391+
const userDocRoot = path.resolve(rootDir || config?.root || cwd);
391392
// We use a temp dir to store runtime files, so we can separate client and server build
392393
// and we should empty temp dir before build
393394
// TODO: remove all the temp dir
@@ -396,19 +397,20 @@ export async function initRsbuild(
396397
await fs.mkdir(runtimeAbsTempDir, { recursive: true });
397398

398399
const routeService = await RouteService.create({
399-
config: _config,
400+
config: config,
400401
runtimeTempDir: runtimeAbsTempDir,
401402
scanDir: userDocRoot,
402403
pluginDriver,
403404
});
404405

405-
const config = await modifyConfigWithAutoNavSide(_config);
406+
const mergedConfig = await modifyConfigWithAutoNavSide(config);
407+
hintBuilderPluginsBreakingChange(mergedConfig);
406408

407409
const { createRsbuild, mergeRsbuildConfig } = await import('@rsbuild/core');
408410

409411
const internalRsbuildConfig = await createInternalBuildConfig(
410412
userDocRoot,
411-
config,
413+
mergedConfig,
412414
enableSSG,
413415
routeService,
414416
pluginDriver,
@@ -421,12 +423,10 @@ export async function initRsbuild(
421423
...(pluginDriver
422424
.getPlugins()
423425
?.map(plugin => plugin.builderConfig ?? {}) || []),
424-
config?.builderConfig || {},
426+
mergedConfig.builderConfig || {},
425427
extraRsbuildConfig || {},
426428
),
427429
});
428430

429-
rsbuild.addPlugins(builderPlugins);
430-
431431
return rsbuild;
432432
}

packages/core/src/node/logger/hint.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readFile } from 'node:fs/promises';
22
import { join, relative } from 'node:path';
3+
import type { UserConfig } from '@rspress/shared';
34
import { logger } from '@rspress/shared/logger';
45
import picocolors from 'picocolors';
56
import type { NavJson } from '../auto-nav-sidebar/type';
@@ -44,6 +45,33 @@ export async function hintThemeBreakingChange(customThemeDir: string) {
4445
}
4546
}
4647

48+
export function hintBuilderPluginsBreakingChange(config: UserConfig) {
49+
if (
50+
// config.builderPlugins is removed in V2
51+
'builderPlugins' in config &&
52+
Array.isArray(config.builderPlugins) &&
53+
config.builderPlugins.length > 0
54+
) {
55+
logger.error(
56+
`[Rspress v2] The "builderPlugins" option has been renamed to "builderConfig.plugins", please update your config accordingly (https://rspress.rs/api/config/config-build#builderconfigplugins).\n`,
57+
`
58+
export default defineConfig({
59+
${picocolors.redBright(
60+
` builderPlugins: [
61+
pluginFoo()
62+
],`,
63+
)}
64+
${picocolors.greenBright(` builderConfig: {
65+
plugins: [
66+
pluginFoo()
67+
],
68+
},`)}
69+
});`,
70+
);
71+
process.exit(1);
72+
}
73+
}
74+
4775
/**
4876
* Possible reasons for printing "ssg: false" and some troubleshooting guidelines for users.
4977
*/

packages/core/src/node/route/RouteService.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ async function initRouteService(
1818
});
1919

2020
const { routeData } = routeService;
21-
const routeMeta = routeService.getRoutes();
22-
const routeCode = routeService.generateRoutesCodeByRouteMeta(routeMeta);
21+
const routeCode = routeService.generateRoutesCode();
2322

2423
return {
2524
routeData,

packages/core/src/node/route/RouteService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class RouteService {
192192
return this.generateRoutesCodeByRouteMeta(this.getRoutes());
193193
}
194194

195-
generateRoutesCodeByRouteMeta(routeMeta: RouteMeta[]) {
195+
private generateRoutesCodeByRouteMeta(routeMeta: RouteMeta[]) {
196196
return `
197197
import React from 'react';
198198
import { lazyWithPreload } from "react-lazy-with-preload";

packages/document/docs/en/api/config/config-build.mdx

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ If you want to modify the output directory, please use [outDir](/api/config/conf
4242

4343
:::
4444

45-
## builderPlugins
45+
## builderConfig.plugins
4646

4747
- Type: `RsbuildPlugin[]`
4848

@@ -57,7 +57,9 @@ import { defineConfig } from 'rspress/config';
5757
import { pluginVue } from '@rsbuild/plugin-vue';
5858

5959
export default defineConfig({
60-
builderPlugins: [pluginVue()],
60+
builderConfig: {
61+
plugins: [pluginVue()],
62+
},
6163
});
6264
```
6365

@@ -68,12 +70,14 @@ import { defineConfig } from 'rspress/config';
6870
import { pluginGoogleAnalytics } from 'rsbuild-plugin-google-analytics';
6971

7072
export default defineConfig({
71-
builderPlugins: [
72-
pluginGoogleAnalytics({
73-
// replace this with your Google tag ID
74-
id: 'G-xxxxxxxxxx',
75-
}),
76-
],
73+
builderConfig: {
74+
plugins: [
75+
pluginGoogleAnalytics({
76+
// replace this with your Google tag ID
77+
id: 'G-xxxxxxxxxx',
78+
}),
79+
],
80+
},
7781
});
7882
```
7983

@@ -84,13 +88,15 @@ import { defineConfig } from 'rspress/config';
8488
import { pluginOpenGraph } from 'rsbuild-plugin-open-graph';
8589

8690
export default defineConfig({
87-
builderPlugins: [
88-
pluginOpenGraph({
89-
title: 'My Website',
90-
type: 'website',
91-
// ...options
92-
}),
93-
],
91+
builderConfig: {
92+
plugins: [
93+
pluginOpenGraph({
94+
title: 'My Website',
95+
type: 'website',
96+
// ...options
97+
}),
98+
],
99+
},
94100
});
95101
```
96102

@@ -103,11 +109,13 @@ import { defineConfig } from 'rspress/config';
103109
import { pluginReact } from '@rsbuild/plugin-react';
104110

105111
export default defineConfig({
106-
builderPlugins: [
107-
pluginReact({
108-
// ...options
109-
}),
110-
],
112+
builderConfig: {
113+
plugins: [
114+
pluginReact({
115+
// ...options
116+
}),
117+
],
118+
},
111119
});
112120
```
113121

@@ -195,15 +203,6 @@ export default defineConfig({
195203

196204
After enabling this config, Rspress will check the links in the document based on the conventional routing table. If there is an unreachable link, the build will throw an error and exit.
197205

198-
### markdown.mdxRs
199-
200-
- Type: `boolean | { include: (filepath: string) => boolean }`
201-
- Default: `true`
202-
203-
import MdxRs from '../../fragments/mdx-rs';
204-
205-
<MdxRs />
206-
207206
### markdown.showLineNumbers
208207

209208
- Type: `boolean`

packages/document/docs/en/guide/advanced/extend-build.mdx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default defineConfig({
2222
});
2323
```
2424

25-
Rspress also provides the [builderPlugins](/api/config/config-build#builderplugins) config to register Rsbuild plugins. You can leverage Rsbuild's extensive plugin ecosystem to enhance and extend your build capabilities.
25+
Rspress also provides the [builderConfig.plugins](/api/config/config-build#builderconfigplugins) config to register Rsbuild plugins. You can leverage Rsbuild's extensive plugin ecosystem to enhance and extend your build capabilities.
2626

2727
For example, add Google analytics through [rsbuild-plugin-google-analytics](https://github.com/rspack-contrib/rsbuild-plugin-google-analytics):
2828

@@ -31,12 +31,14 @@ import { defineConfig } from 'rspress/config';
3131
import { pluginGoogleAnalytics } from 'rsbuild-plugin-google-analytics';
3232

3333
export default defineConfig({
34-
builderPlugins: [
35-
pluginGoogleAnalytics({
36-
// replace this with your Google tag ID
37-
id: 'G-xxxxxxxxxx',
38-
}),
39-
],
34+
builderConfig: {
35+
plugins: [
36+
pluginGoogleAnalytics({
37+
// replace this with your Google tag ID
38+
id: 'G-xxxxxxxxxx',
39+
}),
40+
],
41+
},
4042
});
4143
```
4244

packages/document/docs/zh/api/config/config-build.mdx

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default defineConfig({
4242

4343
:::
4444

45-
## builderPlugins
45+
## builderConfig.plugins
4646

4747
- Type: `RsbuildPlugin[]`
4848

@@ -57,7 +57,9 @@ import { defineConfig } from 'rspress/config';
5757
import { pluginVue } from '@rsbuild/plugin-vue';
5858

5959
export default defineConfig({
60-
builderPlugins: [pluginVue()],
60+
builderConfig: {
61+
plugins: [pluginVue()],
62+
},
6163
});
6264
```
6365

@@ -68,12 +70,14 @@ import { defineConfig } from 'rspress/config';
6870
import { pluginGoogleAnalytics } from 'rsbuild-plugin-google-analytics';
6971

7072
export default defineConfig({
71-
builderPlugins: [
72-
pluginGoogleAnalytics({
73-
// replace this with your Google tag ID
74-
id: 'G-xxxxxxxxxx',
75-
}),
76-
],
73+
builderConfig: {
74+
plugins: [
75+
pluginGoogleAnalytics({
76+
// replace this with your Google tag ID
77+
id: 'G-xxxxxxxxxx',
78+
}),
79+
],
80+
},
7781
});
7882
```
7983

@@ -84,13 +88,15 @@ import { defineConfig } from 'rspress/config';
8488
import { pluginOpenGraph } from 'rsbuild-plugin-open-graph';
8589

8690
export default defineConfig({
87-
builderPlugins: [
88-
pluginOpenGraph({
89-
title: 'My Website',
90-
type: 'website',
91-
// ...options
92-
}),
93-
],
91+
builderConfig: {
92+
plugins: [
93+
pluginOpenGraph({
94+
title: 'My Website',
95+
type: 'website',
96+
// ...options
97+
}),
98+
],
99+
},
94100
});
95101
```
96102

@@ -103,11 +109,13 @@ import { defineConfig } from 'rspress/config';
103109
import { pluginReact } from '@rsbuild/plugin-react';
104110

105111
export default defineConfig({
106-
builderPlugins: [
107-
pluginReact({
108-
// ...options
109-
}),
110-
],
112+
builderConfig: {
113+
plugins: [
114+
pluginReact({
115+
// ...options
116+
}),
117+
],
118+
},
111119
});
112120
```
113121

@@ -195,15 +203,6 @@ export default defineConfig({
195203

196204
开启这个配置后,Rspress 会基于约定式路由表对文档中的链接进行检查,若出现无法访问的链接,构建会抛出错误并退出。
197205

198-
### markdown.mdxRs
199-
200-
- Type: `boolean | { include: (filepath: string) => boolean }`
201-
- Default: `true`
202-
203-
import MdxRs from '../../fragments/mdx-rs';
204-
205-
<MdxRs />
206-
207206
### markdown.showLineNumbers
208207

209208
- Type: `boolean`

packages/document/docs/zh/guide/advanced/extend-build.mdx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default defineConfig({
2424
});
2525
```
2626

27-
Rspress 还提供了 [builderPlugins](/api/config/config-build#builderplugins) 配置项来注册 Rsbuild 插件。你可以利用 Rsbuild 丰富的插件生态来增强和扩展构建能力。
27+
Rspress 还提供了 [builderConfig.plugins](/api/config/config-build#builderconfigplugins) 配置项来注册 Rsbuild 插件。你可以利用 Rsbuild 丰富的插件生态来增强和扩展构建能力。
2828

2929
比如通过 [rsbuild-plugin-google-analytics](https://github.com/rspack-contrib/rsbuild-plugin-google-analytics) 添加 Google analytics:
3030

@@ -33,12 +33,14 @@ import { defineConfig } from 'rspress/config';
3333
import { pluginGoogleAnalytics } from 'rsbuild-plugin-google-analytics';
3434

3535
export default defineConfig({
36-
builderPlugins: [
37-
pluginGoogleAnalytics({
38-
// replace this with your Google tag ID
39-
id: 'G-xxxxxxxxxx',
40-
}),
41-
],
36+
builderConfig: {
37+
plugins: [
38+
pluginGoogleAnalytics({
39+
// replace this with your Google tag ID
40+
id: 'G-xxxxxxxxxx',
41+
}),
42+
],
43+
},
4244
});
4345
```
4446

0 commit comments

Comments
 (0)