Skip to content

Commit 8e33012

Browse files
authored
docs: rewrite plugins usage guide (#9132)
1 parent efc8cba commit 8e33012

File tree

6 files changed

+189
-16
lines changed

6 files changed

+189
-16
lines changed

website/docs/en/config/plugins.mdx

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
# Plugins
22

3-
The `plugins` option is used to customize the Rspack build process in a variety of ways. Rspack comes with a variety built-in plugins available under `rspack.[plugin-name]`. See [Plugins page](/plugins/index) for a list of plugins and documentation but note that there are a lot more out in the community.
3+
- **Type:**
4+
5+
```ts
6+
type Falsy = false | '' | 0 | null | undefined;
7+
8+
type Plugin =
9+
| RspackPluginInstance
10+
| RspackPluginFunction
11+
| WebpackPluginInstance
12+
| WebpackPluginFunction
13+
| Falsy;
14+
15+
type Plugins = Plugin[];
16+
```
417

5-
- **Type:** `Array<RspackPluginInstance | RspackPluginFunction | RspackBuiltinPlugin>`
618
- **Default:** `[]`
719

8-
An array of webpack plugins. For example, [`DefinePlugin`](/plugins/webpack/define-plugin) allows you to create global constants which can be configured at compile time. This can be useful for allowing different behavior between development builds and release builds.
20+
The `plugins` option is used to register a set of Rspack or webpack plugins to customize the build process.
21+
22+
Please refer to [Plugins page](/guide/features/plugin) for more information on using plugins in Rspack.
23+
24+
## Built-in plugins
25+
26+
Rspack comes with a variety built-in plugins available under `rspack.PluginName`.
27+
28+
For example, [`DefinePlugin`](/plugins/webpack/define-plugin) allows you to replaces variables in your code with other values or expressions at compile time. You can access it via `rspack.DefinePlugin` and create a plugin instance with `new`:
929

1030
```js title=rspack.config.js
31+
const { rspack } = require('@rspack/core');
32+
1133
module.exports = {
1234
//...
1335
plugins: [
1436
new rspack.DefinePlugin({
15-
// Definitions...
37+
// pass plugin options
1638
}),
1739
],
1840
};
1941
```
2042

43+
## webpack plugins
44+
2145
Rspack strives to maintain compatibility with the webpack plugin ecosystem to leverage the excellent features that have been accumulated and validated by the community. Please refer to the [Plugin Compatibility List](/guide/compatibility/plugin) to access a list of webpack plugins that have passed our compatibility tests:
2246

2347
```js title=rspack.config.js
@@ -29,3 +53,18 @@ module.exports = {
2953
plugins: [new HtmlWebpackPlugin()],
3054
};
3155
```
56+
57+
## Disable plugins
58+
59+
Rspack ignores `false``''``0``null``undefined` values in the `plugins` array, which allows you to easily disable a plugin.
60+
61+
For example, enable [HotModuleReplacementPlugin](/plugins/webpack/hot-module-replacement-plugin) only in the development environment:
62+
63+
```js title=rspack.config.js
64+
const { rspack } = require('@rspack/core');
65+
const isDev = process.env.NODE_ENV === 'development';
66+
67+
module.exports = {
68+
plugins: [isDev && new rspack.HotModuleReplacementPlugin()],
69+
};
70+
```

website/docs/en/guide/features/plugin.mdx

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,70 @@
11
# Plugins
22

3-
If [loaders](/guide/features/loader) are the workhorse for file transformations (preprocessing), then plugins are the workhorse for the overall Rspack build process. Most of Rspack's native implementations rely on the Rust side of the plugin system. For Node users, you don't need to worry about compatibility issues with Rust, because Rspack takes care of those details for you automatically. You can just focus on how to use the plugins. Find out [plugins](/plugins/index) you can use with Rspack.
3+
If [loaders](/guide/features/loader) are the workhorse for file transformations (preprocessing), then plugins are the workhorse for the overall Rspack build process. Most of Rspack's native implementations rely on the Rust side of the plugin system.
4+
5+
For Node.js users, you don't need to worry about interoperability issues with Node.js and Rust, because Rspack takes care of those details for you automatically. You can just focus on how to use the plugins.
46

57
## Plugin usage
68

7-
Here's an example of how to use the already compatible [webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) in `rspack.config.js`:
9+
Rspack provides the [plugins](/config/plugins) configuration, which is used to register a set of Rspack or webpack plugins to customize the build process.
10+
11+
Here is an example of using the [webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) in `rspack.config.js`:
812

913
```js title="rspack.config.js"
1014
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
1115

1216
module.exports = {
13-
plugins: [new BundleAnalyzerPlugin()],
17+
plugins: [
18+
new BundleAnalyzerPlugin({
19+
// options
20+
}),
21+
],
1422
};
1523
```
1624

1725
If you're looking for more Rspack plugins, have a look at the great list of [supported plugins](/plugins/index).
1826

1927
You can also refer to [Plugin compat](/guide/compatibility/plugin) for the list of webpack plugins that have passed Rspack compatibility tests.
2028

29+
## Other plugins
30+
31+
### Unplugin
32+
33+
[unplugin](https://github.com/unjs/unplugin) is a unified plugin system for various build tools. You can use plugins implemented based on unplugin in Rspack, typically by importing the `/rspack` subpath of the plugin and registering it through `plugins`.
34+
35+
Here is an example of using [unplugin-vue-components](https://www.npmjs.com/package/unplugin-vue-components):
36+
37+
```js title=rspack.config.js
38+
const Components = require('unplugin-vue-components/rspack');
39+
40+
module.exports = {
41+
plugins: [
42+
Components.default({
43+
// options
44+
}),
45+
],
46+
};
47+
```
48+
49+
### SWC plugins
50+
51+
In the built-in [swc-loader](/guide/features/builtin-swc-loader) of Rspack, you can use SWC's Wasm plugins, see [jsc.experimental.plugins](/guide/features/builtin-swc-loader#jscexperimentalplugins).
52+
53+
### Rsbuild plugins
54+
55+
[Rsbuild](https://rsbuild.dev) is a build tool based on Rspack, and Rsbuild has its own plugin system.
56+
57+
Please note that you cannot use Rsbuild plugins in Rspack, because Rspack is a more low-level tool, but you can use Rspack plugins in Rsbuild.
58+
59+
Here is a comparison table for the plugins that can be used in Rspack and Rsbuild:
60+
61+
| Tool used | Rspack plugins | webpack plugins | Rsbuild plugins | Unplugins | SWC plugins |
62+
| --------- | -------------- | --------------- | --------------- | --------- | ----------- |
63+
| Rspack ||||||
64+
| Rsbuild ||||||
65+
66+
> Please refer to the [Rsbuild plugin documentation](https://rsbuild.dev/plugins/list/index) for more information.
67+
2168
## Write a plugin
2269

2370
### Plugin structure

website/docs/en/plugins/webpack/hot-module-replacement-plugin.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Enabling HMR is straightforward and in most cases no options are necessary.
1313
Note that HMR can not be used in production build. You can use `process.env.NODE_ENV` to determine if it is a development environment.
1414

1515
```js title="rspack.config.js"
16+
const { rspack } = require('@rspack/core');
1617
const isDev = process.env.NODE_ENV === 'development';
1718

1819
module.exports = {

website/docs/zh/config/plugins.mdx

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
11
# Plugins
22

3-
`plugins` 选项用于以各种方式自定义 Rspack 构建过程。Rspack 附带了各种内置插件,可以通过 `rspack.[plugin-name]` 访问这些插件。请查看[插件页面](/plugins/index)获取插件列表和对应文档,但请注意这只是其中一部分,社区中还有许多插件。
3+
- **类型:**
4+
5+
```ts
6+
type Falsy = false | '' | 0 | null | undefined;
7+
8+
type Plugin =
9+
| RspackPluginInstance
10+
| RspackPluginFunction
11+
| WebpackPluginInstance
12+
| WebpackPluginFunction
13+
| Falsy;
14+
15+
type Plugins = Plugin[];
16+
```
417

5-
- **类型:** `Array<RspackPluginInstance | RspackPluginFunction | RspackBuiltinPlugin>`
618
- **默认值:** `[]`
719

8-
一组 Rspack 插件。例如,[`DefinePlugin`](/plugins/webpack/define-plugin) 允许你创建可在编译时配置的全局常量。这对需要再开发环境构建和生产环境构建之间产生不同行为来说非常有用。
20+
`plugins` 选项用于注册一组 Rspack 或 webpack 插件来自定义构建过程。
21+
22+
请查看 [插件页面](/guide/features/plugin) 来了解在 Rspack 中使用插件的更多信息。
23+
24+
## 内置插件
25+
26+
Rspack 附带了许多内置插件,你可以通过 `rspack.PluginName` 访问这些插件。
27+
28+
例如,[`DefinePlugin`](/plugins/webpack/define-plugin) 允许你在编译时将代码中的变量替换为其他值或表达式。你可以通过 `rspack.DefinePlugin` 来访问它,并使用 `new` 来创建插件实例:
929

1030
```js title=rspack.config.js
31+
const { rspack } = require('@rspack/core');
32+
1133
module.exports = {
1234
//...
1335
plugins: [
1436
new rspack.DefinePlugin({
15-
// 定义的全局常量...
37+
// 传入插件选项
1638
}),
1739
],
1840
};
1941
```
2042

21-
Rspack 致力于兼容 webpack 生态下的插件,以使用社区中已经积累和验证的优秀功能。请查看[插件兼容列表](/guide/compatibility/plugin)获取经过我们兼容性测试的 webpack 插件清单:
43+
## webpack 插件
44+
45+
Rspack 致力于兼容 webpack 生态中的插件,让用户能够使用社区中经过验证的优秀功能。你可以查看 [插件兼容列表](/guide/compatibility/plugin) 来获取已通过兼容性测试的 webpack 插件清单:
2246

2347
```js title=rspack.config.js
2448
const { rspack } = require('@rspack/core');
@@ -29,3 +53,18 @@ module.exports = {
2953
plugins: [new HtmlWebpackPlugin()],
3054
};
3155
```
56+
57+
## 禁用插件
58+
59+
Rspack 会忽略 `plugins` 数组中的 `false``''``0``null``undefined` 这些值,这允许你更容易地禁用某个插件。
60+
61+
比如,仅在开发环境中启用 [HotModuleReplacementPlugin](/plugins/webpack/hot-module-replacement-plugin)
62+
63+
```js title=rspack.config.js
64+
const { rspack } = require('@rspack/core');
65+
const isDev = process.env.NODE_ENV === 'development';
66+
67+
module.exports = {
68+
plugins: [isDev && new rspack.HotModuleReplacementPlugin()],
69+
};
70+
```

website/docs/zh/guide/features/plugin.mdx

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,70 @@
11
# 插件
22

3-
如果说 [Loader](/guide/features/loader) 是文件转换(预处理)的核心,那么插件(Plugin)则是 Rspack 整体构建流程的核心组成部分,大部分 Rspack 的原生实现依赖了 Rust 侧的插件系统。
4-
对于 Node 的用户来说,你无需担心和 Rust 的 Interop 问题,因为 Rspack 会自动帮你处理好这些细节,你只需要关注如何使用插件即可。
3+
如果说 [loader](/guide/features/loader) 是文件转换(预处理)的核心,那么插件(plugin)则是 Rspack 整体构建流程的核心组成部分,Rspack 大部分的原生实现都依赖于 Rust 侧的插件系统。
4+
5+
对于 Node.js 的用户来说,你无需担心 Node.js 和 Rust 的互操作问题,因为 Rspack 会自动帮你处理好这些细节,你只需要关注如何使用插件即可。
56

67
## 使用插件
78

8-
下面是一个例子,在 `rspack.config.js` 中使用已经兼容的 [webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer)
9+
Rspack 提供了 [plugins](/config/plugins) 配置项,用于注册一组 Rspack 或 webpack 插件来自定义构建过程。
10+
11+
下面是一个例子,在 `rspack.config.js` 中使用 [webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) 插件:
912

1013
```js title="rspack.config.js"
1114
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
1215

1316
module.exports = {
14-
plugins: [new BundleAnalyzerPlugin()],
17+
plugins: [
18+
new BundleAnalyzerPlugin({
19+
// options
20+
}),
21+
],
1522
};
1623
```
1724

1825
如果你正在寻找更多的 Rspack 插件,请查看 [插件](/plugins/index) 列表。
1926

2027
你也可以参考 [Plugin 兼容](/guide/compatibility/plugin) 列表,查看已通过 Rspack 兼容性测试的 webpack 插件。
2128

29+
## 其他插件
30+
31+
### Unplugin
32+
33+
[unplugin](https://github.com/unjs/unplugin) 是一个适用于不同构建工具的统一插件系统。你可以在 Rspack 中使用基于 unplugin 实现的插件,通常需要引入插件的 `/rspack` 子路径,并通过 `plugins` 注册它。
34+
35+
下面是使用 [unplugin-vue-components](https://www.npmjs.com/package/unplugin-vue-components) 的示例:
36+
37+
```js title=rspack.config.js
38+
const Components = require('unplugin-vue-components/rspack');
39+
40+
module.exports = {
41+
plugins: [
42+
Components.default({
43+
// options
44+
}),
45+
],
46+
};
47+
```
48+
49+
### SWC 插件
50+
51+
在 Rspack 内置的 [swc-loader](/guide/features/builtin-swc-loader) 中,你可以使用 SWC 的 Wasm 插件,详见 [jsc.experimental.plugins](/guide/features/builtin-swc-loader#jscexperimentalplugins)
52+
53+
### Rsbuild 插件
54+
55+
[Rsbuild](https://rsbuild.dev) 是基于 Rspack 的构建工具,并且具备一套独立的插件系统。
56+
57+
请注意,你无法在 Rspack 中使用 Rsbuild 插件,因为 Rspack 是更加底层的工具,但你可以在 Rsbuild 中使用 Rspack 插件。
58+
59+
下面是 Rspack 与 Rsbuild 可用的插件对比:
60+
61+
| 使用的工具 | Rspack 插件 | webpack 插件 | Rsbuild 插件 | Unplugins | SWC 插件 |
62+
| ---------- | ----------- | ------------ | ------------ | --------- | -------- |
63+
| Rspack ||||||
64+
| Rsbuild ||||||
65+
66+
> 请查看 [Rsbuild 插件文档](https://rsbuild.dev/zh/plugins/list/index) 来获取更多信息。
67+
2268
## 编写一个插件
2369

2470
### 插件结构

website/docs/zh/plugins/webpack/hot-module-replacement-plugin.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ new rspack.HotModuleReplacementPlugin();
1313
注意 HMR 不能被用于生产构建,因此你需要通过 `process.env.NODE_ENV` 来判断当前是否是开发环境。
1414

1515
```js title="rspack.config.js"
16+
const { rspack } = require('@rspack/core');
1617
const isDev = process.env.NODE_ENV === 'development';
1718

1819
module.exports = {

0 commit comments

Comments
 (0)