Skip to content

Commit 14667a9

Browse files
authored
docs: add Configure SWC guide (#713)
1 parent 2b4662b commit 14667a9

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

website/docs/en/guide/basic/configure-rstest.mdx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,43 @@ Rstest uses Rspack for building, so you can directly use Rspack's configuration
5858

5959
More details can be referred to [Configure Rspack](https://rsbuild.rs/guide/configuration/rspack).
6060

61+
## Configure SWC
62+
63+
Rstest uses Rspack's [builtin:swc-loader](https://rspack.rs/guide/features/builtin-swc-loader) to transform JavaScript and TypeScript code by default, which is the Rust version of [swc-loader](https://github.com/swc-project/pkgs/tree/main/packages/swc-loader).
64+
65+
Rstest exposes some options to configure `builtin:swc-loader`:
66+
67+
- [tools.swc](/config/build/tools#toolsswc): Used to configure the options of `builtin:swc-loader`.
68+
- [source.include](/config/build/source#sourceinclude): Used to specify the files that need to be compiled by SWC.
69+
- [source.exclude](/config/build/source#sourceexclude): Used to exclude files that do not need to be compiled by SWC.
70+
71+
```ts
72+
import { defineConfig } from '@rsbuild/core';
73+
74+
export default defineConfig({
75+
tools: {
76+
swc: {
77+
jsc: {
78+
transform: {
79+
react: {
80+
runtime: 'automatic',
81+
},
82+
},
83+
experimental: {
84+
plugins: [['@swc/plugin-emotion', {}]],
85+
},
86+
},
87+
},
88+
},
89+
});
90+
```
91+
92+
### SWC plugin version
93+
94+
Please note that SWC's plugins are still an experimental feature. Currently, SWC's Wasm plugins are not backward compatible, and the version of SWC plugins is strongly coupled with the `swc_core` version that Rspack depends on.
95+
96+
This means that you need to choose SWC plugins that match the current `swc_core` version to make them work properly. If the SWC plugin version you use does not match the `swc_core` version that Rspack depends on, Rspack will throw errors during the build. Please refer to [Rspack FAQ - SWC plugin version mismatch](https://rspack.rs/errors/swc-plugin-version) for handling.
97+
6198
## Detect Rstest environment
6299

63100
You can use `process.env.RSTEST` to detect whether it is an Rstest test environment to apply different configurations/codes in your tests.

website/docs/zh/guide/basic/configure-rstest.mdx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,43 @@ Rstest 底层使用 Rspack 进行构建,因此你可以直接使用 Rspack 的
5858

5959
详情可参考 [配置 Rspack](https://rsbuild.rs/zh/guide/configuration/rspack)
6060

61+
## 配置 SWC
62+
63+
Rstest 默认通过 Rspack 的 [builtin:swc-loader](https://rspack.rs/zh/guide/features/builtin-swc-loader) 来转换 JavaScript 和 TypeScript 代码,它是 [swc-loader](https://github.com/swc-project/pkgs/tree/main/packages/swc-loader) 的 Rust 版本。
64+
65+
Rstest 暴露了一些选项来配置 `builtin:swc-loader`
66+
67+
- [tools.swc](/config/build/tools#toolsswc):用于配置 `builtin:swc-loader` 的选项。
68+
- [source.include](/config/build/source#sourceinclude):用于指定需要被 SWC 编译的文件。
69+
- [source.exclude](/config/build/source#sourceexclude):用于排除不需要被 SWC 编译的文件。
70+
71+
```ts
72+
import { defineConfig } from '@rsbuild/core';
73+
74+
export default defineConfig({
75+
tools: {
76+
swc: {
77+
jsc: {
78+
transform: {
79+
react: {
80+
runtime: 'automatic',
81+
},
82+
},
83+
experimental: {
84+
plugins: [['@swc/plugin-emotion', {}]],
85+
},
86+
},
87+
},
88+
},
89+
});
90+
```
91+
92+
### SWC 插件版本
93+
94+
请注意,SWC 的插件仍然是一个实验性功能,目前 SWC 的 Wasm 插件是不向后兼容的,SWC 插件的版本与 Rspack 依赖的 `swc_core` 版本存在强耦合关系。
95+
96+
这意味着,你需要选择和当前 `swc_core` 版本匹配的 SWC 插件,才能使它正常执行。如果你使用的 SWC 插件版本与 Rspack 依赖的 `swc_core` 版本不匹配,Rspack 在执行构建时会抛出错误,请参考 [Rspack 常见问题 - SWC 插件版本不匹配](https://rspack.rs/zh/errors/swc-plugin-version) 进行处理。
97+
6198
## 检测 Rstest 环境
6299

63100
你可以使用 `process.env.RSTEST` 来检测是否是 Rstest 测试环境以应用不同的配置 / 代码在你的测试中。

0 commit comments

Comments
 (0)