Skip to content

Commit 06d7dfb

Browse files
authored
docs(plugin-swc): add guide of usage scenarios (#3675)
1 parent a273ac3 commit 06d7dfb

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

packages/document/builder-doc/docs/en/plugins/plugin-swc.mdx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,37 @@ import SWC from '@en/shared/swc.md';
44

55
<SWC />
66

7-
## Quick Start
7+
## Usage Scenarios
88

9-
:::tip
10-
When using Rspack as the bundler, SWC will be used for transpiling and compression by default, so you don't need to install or configure the SWC plugin.
11-
:::
9+
Before using the SWC plugin, please understand the scenarios and limitations of the SWC plugin to determine whether your project is suitable for using it.
10+
11+
### Rspack Scenario
12+
13+
If you are already using Rspack as the bundler in your project, then you do not need to use the SWC plugin, because Rspack uses SWC for transpiler and minifier by default, and the SWC compilation capabilities are available out of the box.
14+
15+
### Babel Plugins
16+
17+
If your project requires the registration of some custom Babel plugins, you will not be able to register and use Babel plugins after using SWC, since it replaces Babel as the transpiler.
18+
19+
For most common Babel plugins, you can find corresponding replacements in SWC, such as:
20+
21+
- `@babel/preset-env`: use [presetEnv](#presetenv) instead.
22+
- `@babel/preset-react`: use [presetReact](#presetreact) instead.
23+
- `babel-plugin-import`: use [source.transformImport](/api/config-source.html#sourcetransformimport) instead.
24+
- `babel-plugin-lodash`: use [extensions.lodash](#extensionslodash) instead.
25+
- `@emotion/babel-plugin`: use [extensions.emotion](#extensionsemotion) instead.
26+
- `babel-plugin-styled-components`: use [extensions.styledComponent](#extensionsstyledcomponent) instead.
27+
- `@babel/plugin-react-transform-remove-prop-types`: use [reactUtils.removePropTypes](#extensionsreactutils) instead.
28+
29+
If you use Babel plugin capabilities that are not yet supported by SWC, you will no longer be able to use them after switching to SWC compilation. You can give feedback via issues under the [swc-plugins](https://github.com/web-infra-dev/swc-plugins) repository and we will evaluate if built-in support is needed.
30+
31+
### Bundle Size
32+
33+
When using SWC for code minification instead of [terser](https://github.com/terser/terser) and [cssnano](https://github.com/cssnano/cssnano), there may be a small change in the bundle size. SWC outperforms terser for JavaScript code compression and slightly underperforms cssnano for CSS code compression.
34+
35+
For a detailed comparison between minifiers, see [minification-benchmarks](https://github.com/privatenumber/minification-benchmarks).
36+
37+
## Quick Start
1238

1339
### Install
1440

@@ -161,7 +187,7 @@ Remove `React` runtime type checking. This is ported from [@babel/plugin-react-t
161187
- **Type:** `{ cwd?: string; ids?: string;}`
162188
- **Default:** `{ cwd: process.cwd(), ids: [] }`
163189

164-
Ported from [@babel/plugin-lodash](https://github.com/lodash/babel-plugin-lodash).
190+
Ported from [babel-plugin-lodash](https://github.com/lodash/babel-plugin-lodash).
165191

166192
#### extensions.styledComponent
167193

@@ -187,7 +213,7 @@ boolean | {
187213
};
188214
```
189215

190-
This is ported by Next.js team from [styled-component](https://github.com/styled-components/babel-plugin-styled-components).
216+
This is ported by Next.js team from [babel-plugin-styled-components](https://github.com/styled-components/babel-plugin-styled-components).
191217

192218
#### extensions.emotion
193219

packages/document/builder-doc/docs/zh/plugins/plugin-swc.mdx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,37 @@ import SWC from '@zh/shared/swc.md';
44

55
<SWC />
66

7-
## 快速开始
7+
## 适用场景
88

9-
:::tip
10-
在使用 Rspack 作为打包工具时,默认会使用 SWC 进行转译和压缩,因此你不需要再安装和配置 SWC 插件。
11-
:::
9+
在使用 SWC 插件之前,请先了解一下 SWC 插件的适用场景和局限性,以明确你的项目是否需要使用 SWC 插件。
10+
11+
### Rspack 场景
12+
13+
如果你的项目中已经使用了 Rspack 作为打包工具,那么你不需要接入 SWC 插件,因为 Rspack 默认会使用 SWC 进行转译和压缩,各个 SWC 编译能力可以开箱即用。
14+
15+
### Babel 插件
16+
17+
如果你的项目需要注册一些自定义的 Babel 插件,由于 SWC 替代了 Babel 作为转译工具,因此使用 SWC 后,你将无法注册和使用 Babel 插件。
18+
19+
对于大部分常见的 Babel 插件,你可以在 SWC 中找到对应的替代品,比如:
20+
21+
- `@babel/preset-env`: 使用 [presetEnv](#presetenv) 代替。
22+
- `@babel/preset-react`: 使用 [presetReact](#presetreact) 代替。
23+
- `babel-plugin-import`:使用 [source.transformImport](/api/config-source.html#sourcetransformimport) 代替。
24+
- `babel-plugin-lodash`:使用 [extensions.lodash](#extensionslodash) 代替。
25+
- `@emotion/babel-plugin`:使用 [extensions.emotion](#extensionsemotion) 代替。
26+
- `babel-plugin-styled-components`:使用 [extensions.styledComponent](#extensionsstyledcomponent) 代替。
27+
- `@babel/plugin-react-transform-remove-prop-types`: 使用 [reactUtils.removePropTypes](#extensionsreactutils) 代替。
28+
29+
如果你使用了 SWC 尚未支持的 Babel 插件能力,在切换到 SWC 编译后,将无法再使用它们。你可以到 [swc-plugins](https://github.com/web-infra-dev/swc-plugins) 仓库下通过 issues 进行反馈,我们会评估是否需要内置支持。
30+
31+
### 产物体积
32+
33+
在使用 SWC 来代替 [terser](https://github.com/terser/terser)[cssnano](https://github.com/cssnano/cssnano) 进行代码压缩时,构建产物的体积可能会出现少量变化。在 JavaScript 代码压缩方面,SWC 的压缩率是优于 terser 的;在 CSS 代码压缩方面,SWC 的压缩率稍逊于 cssnano。
34+
35+
对于压缩工具之间的详细对比,可以参考 [minification-benchmarks](https://github.com/privatenumber/minification-benchmarks)
36+
37+
## 快速开始
1238

1339
### 安装
1440

@@ -161,7 +187,7 @@ type RemovePropTypesOptions = {
161187
- **类型:** `{ cwd?: string; ids?: string;}`
162188
- **默认值:** `{ cwd: process.cwd(), ids: [] }`
163189

164-
移植自 [@babel/plugin-lodash](https://github.com/lodash/babel-plugin-lodash)
190+
移植自 [babel-plugin-lodash](https://github.com/lodash/babel-plugin-lodash)
165191

166192
#### extensions.styledComponent
167193

@@ -179,7 +205,7 @@ boolean | {
179205
};
180206
```
181207

182-
由 Next.js 团队移植自 [styled-component](https://github.com/styled-components/babel-plugin-styled-components)
208+
由 Next.js 团队移植自 [babel-plugin-styled-components](https://github.com/styled-components/babel-plugin-styled-components)
183209

184210
#### extensions.emotion
185211

0 commit comments

Comments
 (0)