Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions website/docs/en/plugins/dev/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,53 @@ export const pluginEslint = (options?: Options): RsbuildPlugin => ({
},
});
```

## Extending plugin API

When building custom tools on top of Rsbuild's [JavaScript API](/api/start/), you may want to extend the existing plugin API to provide additional capabilities — for example, exposing utility functions or sharing context objects.

You can achieve this by using the [rsbuild.expose()](/api/javascript-api/instance#rsbuildexpose) method on the Rsbuild instance.

This method works the same way as the plugin's [api.expose()](/plugins/dev/core#apiexpose), allowing you to expose custom methods or objects to Rsbuild plugins.

For example, you can expose `getState` and `setCount` methods:

```ts title="myToolkit.ts"
import { createRsbuild } from '@rsbuild/core';

export const MY_TOOLKIT_ID = 'my-toolkit';

const rsbuild = await createRsbuild({
// ...
});

const state = {
count: 0,
};

rsbuild.expose(MY_TOOLKIT_ID, {
getState() {
return state;
},
setCount(count: number) {
state.count = count;
},
});
```

Plugins can then access these extended APIs using the [api.useExposed()](/plugins/dev/core#apiuseexposed) method:

```ts title="myPlugin.ts"
import { MY_TOOLKIT_ID } from './myToolkit';

const myPlugin = {
name: 'my-plugin',
setup(api) {
const toolkitApi = api.useExposed(MY_TOOLKIT_ID);
if (toolkitApi) {
const { count } = toolkitApi.getState();
toolkitApi.setCount(count + 1);
}
},
};
```
48 changes: 48 additions & 0 deletions website/docs/zh/plugins/dev/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,51 @@ export const pluginEslint = (options?: Options): RsbuildPlugin => ({
},
});
```

## 扩展插件 API

当你基于 Rsbuild 的 [JavaScript API](/api/start/) 来实现自定义的工具时,可能希望在现有插件 API 的基础上,提供更多能力,例如添加工具方法或共享上下文对象。

此时,你可以使用 Rsbuild 实例上的 [rsbuild.expose()](/api/javascript-api/instance#rsbuildexpose) 方法。它的作用与插件的 [api.expose()](/plugins/dev/core#apiexpose) 一致,用于向 Rsbuild 插件暴露自定义的方法或对象。

例如,向插件暴露 `getState` 和 `setCount` 方法:

```ts title="myToolkit.ts"
import { createRsbuild } from '@rsbuild/core';

export const MY_TOOLKIT_ID = 'my-toolkit';

const rsbuild = await createRsbuild({
// ...
});

const state = {
count: 0,
};

rsbuild.expose(MY_TOOLKIT_ID, {
getState() {
return state;
},
setCount(count: number) {
state.count = count;
},
});
```

然后,插件可以通过 [api.useExposed()](/plugins/dev/core#apiuseexposed) 方法访问这些扩展 API:

```ts title="myPlugin.ts"
import { MY_TOOLKIT_ID } from './myToolkit';

const myPlugin = {
name: 'my-plugin',
setup(api) {
const toolkitApi = api.useExposed(MY_TOOLKIT_ID);
if (toolkitApi) {
const { count } = toolkitApi.getState();
toolkitApi.setCount(count + 1);
}
},
};
```
Loading