@modern-js/main-doc 2.46.1 → 2.47.1
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/apis/app/commands.mdx +9 -9
- package/docs/en/configure/app/auto-load-plugin.mdx +3 -3
- package/docs/en/configure/app/builder-plugins.mdx +13 -13
- package/docs/en/configure/app/output/disable-node-polyfill.mdx +1 -1
- package/docs/en/configure/app/server/ssr.mdx +1 -1
- package/docs/en/configure/app/source/enable-async-entry.mdx +1 -1
- package/docs/en/configure/app/tools/esbuild.mdx +7 -7
- package/docs/en/guides/advanced-features/compatibility.mdx +1 -1
- package/docs/en/guides/advanced-features/optimize-bundle.mdx +7 -7
- package/docs/en/guides/advanced-features/rsbuild-plugin.mdx +51 -0
- package/docs/en/guides/advanced-features/rspack-start.mdx +1 -3
- package/docs/en/guides/advanced-features/using-storybook.mdx +202 -20
- package/docs/en/guides/basic-features/css.mdx +2 -2
- package/docs/en/guides/concept/builder.mdx +23 -25
- package/docs/en/guides/get-started/tech-stack.mdx +1 -1
- package/docs/en/guides/topic-detail/framework-plugin/introduction.mdx +1 -1
- package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +35 -4
- package/docs/zh/apis/app/commands.mdx +9 -9
- package/docs/zh/configure/app/auto-load-plugin.mdx +3 -3
- package/docs/zh/configure/app/builder-plugins.mdx +14 -14
- package/docs/zh/configure/app/output/disable-node-polyfill.mdx +1 -1
- package/docs/zh/configure/app/server/ssr.mdx +1 -1
- package/docs/zh/configure/app/tools/esbuild.mdx +7 -7
- package/docs/zh/guides/advanced-features/compatibility.mdx +1 -1
- package/docs/zh/guides/advanced-features/optimize-bundle.mdx +8 -8
- package/docs/zh/guides/advanced-features/rsbuild-plugin.mdx +51 -0
- package/docs/zh/guides/advanced-features/rspack-start.mdx +1 -1
- package/docs/zh/guides/advanced-features/using-storybook.mdx +204 -21
- package/docs/zh/guides/basic-features/css.mdx +2 -2
- package/docs/zh/guides/concept/builder.mdx +20 -22
- package/docs/zh/guides/get-started/tech-stack.mdx +1 -1
- package/docs/zh/guides/topic-detail/framework-plugin/introduction.mdx +1 -1
- package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +35 -6
- package/package.json +7 -7
- package/docs/en/configure/app/source/compile-js-data-uri.mdx +0 -9
- package/docs/zh/configure/app/source/compile-js-data-uri.mdx +0 -9
@@ -4,44 +4,42 @@ sidebar_position: 2
|
|
4
4
|
|
5
5
|
# Build Engine
|
6
6
|
|
7
|
-
|
7
|
+
Modern.js internally encapsulates its own build tool (Modern.js Builder) based on [Rsbuild](https://rsbuild.dev/), and supports seamless switching between Webpack and Rspack bundlers.
|
8
8
|
|
9
|
-
|
9
|
+
::: tip What is Rsbuild?
|
10
|
+
Rsbuild is a build tool based on Rspack. It is an enhanced Rspack CLI, easy-to-use, and ready-to-use out of the box.
|
11
|
+
:::
|
10
12
|
|
11
13
|
## Build Architecture
|
12
14
|
|
13
|
-
From
|
15
|
+
From the building perspective, Modern.js can be divided into three-layers, from top to bottom:
|
14
16
|
|
15
|
-
- Upper-level
|
16
|
-
-
|
17
|
-
-
|
17
|
+
- Upper-level framework: Modern.js (open source) and EdenX (internal).
|
18
|
+
- Build tool: Rsbuild.
|
19
|
+
- Bundler: Webpack and Rspack.
|
18
20
|
|
19
|
-
<img
|
21
|
+
<img
|
22
|
+
src="https://lf3-static.bytednsdoc.com/obj/eden-cn/zq-uylkvT/ljhwZthlaukjlkulzlp/build-layers-02193.png"
|
23
|
+
style={{ width: '100%', maxWidth: '540px' }}
|
24
|
+
/>
|
20
25
|
|
21
|
-
##
|
26
|
+
## Build Documentation
|
22
27
|
|
23
|
-
|
28
|
+
The documentation address of Rsbuild is: https://rsbuild.dev/
|
24
29
|
|
25
|
-
In this documentation, you can learn about the detailed introduction of
|
30
|
+
In this documentation, you can learn about the detailed introduction of Rsbuild, and you can also find complete usage guides for various building capabilities.
|
26
31
|
|
27
|
-
|
32
|
+
If you want to understand the use of build configurations, it is recommended that you read the Modern.js documentation first, because the build configurations and defaults in Modern.js are not exactly the same as Rsbuild.
|
28
33
|
|
29
|
-
|
34
|
+
## Build Plugins
|
30
35
|
|
31
|
-
|
36
|
+
In Modern.js, you can register Webpack plugins, Rspack plugins, or Rsbuild plugins:
|
32
37
|
|
33
|
-
|
34
|
-
|
35
|
-
html: {
|
36
|
-
title: 'example',
|
37
|
-
},
|
38
|
-
});
|
39
|
-
```
|
38
|
+
- Webpack / Rspack plugins: Configured through [tools.bundlerChain](/configure/app/tools/bundler-chain).
|
39
|
+
- Rsbuild plugins: Configured through [builderPlugins](/configure/app/builder-plugins).
|
40
40
|
|
41
|
-
|
41
|
+
## Build Capabilities
|
42
42
|
|
43
|
-
|
43
|
+
Rsbuild provides rich build capabilities, including JavaScript compilation, CSS compilation, static assets processing, code hot update, code compression, TS type checking, and dozens of other capabilities.
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
We recommend that you read [「Modern.js Builder - All Features」](https://modernjs.dev/builder/en/guide/features.html) to learn about all the features provided by Modern.js Builder.
|
45
|
+
We recommend that you read [「Rsbuild - All Features」](https://rsbuild.dev/guide/start/features) to learn about all the features provided by Rsbuild.
|
@@ -89,7 +89,7 @@ Modern.js supports enabling ["Tailwind CSS"](/en/guides/basic-features/css.html#
|
|
89
89
|
Modern.js supports three CSS preprocessors: [Sass](https://sass-lang.com/), [Less](https://lesscss.org/), and [Stylus](https://stylus-lang.com/):
|
90
90
|
|
91
91
|
- Sass and Less are supported by default and ready to use.
|
92
|
-
- Stylus is optional and can be used by referring to the ["Stylus Plugin"](https://
|
92
|
+
- Stylus is optional and can be used by referring to the ["Stylus Plugin"](https://rsbuild.dev/plugins/list/plugin-stylus).
|
93
93
|
|
94
94
|
---
|
95
95
|
|
@@ -10,7 +10,7 @@ Modern.js offers a comprehensive plugin system with a complete lifecycle. Plugin
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
-
Plugins must be explicitly registered in the configuration file to be effective. When you need to add plugins to Modern.js, you can configure them in the `[
|
13
|
+
Plugins must be explicitly registered in the configuration file to be effective. When you need to add plugins to Modern.js, you can configure them in the `[plugins](/configure/app/plugins.html)` field:
|
14
14
|
|
15
15
|
```ts title="edenx.config.ts"
|
16
16
|
// an example for bff
|
@@ -12,14 +12,15 @@ Through this chapter you can learn:
|
|
12
12
|
|
13
13
|
## Create an app
|
14
14
|
|
15
|
-
|
15
|
+
The routing modes of the current project are divided into the following three types:
|
16
16
|
|
17
|
-
-
|
18
|
-
-
|
17
|
+
- File-based routing (`router: true` and file-based)
|
18
|
+
- Self-Controlled routing (`router: true` and create `BrowserRouter`)
|
19
|
+
- Other (`router: false` and independent installation of `react-router-dom` in the project)
|
19
20
|
|
20
21
|
First, clarify the routing mode of the main application [create a file-based routing main App](#file-based-routing-main-app) or [create a self-controlled main App](#self-controlled-main-app)
|
21
22
|
|
22
|
-
In this
|
23
|
+
In this tutorial we will create two sub-applications Table and Dashboard for the main application (Table is reduced routing, Dashboard is self-controlled routing)
|
23
24
|
|
24
25
|
### File-based Routing Main App
|
25
26
|
|
@@ -139,6 +140,36 @@ import CustomRouterMicroFrontend from '@site-docs-en/components/custom-router-mi
|
|
139
140
|
|
140
141
|
<CustomRouterMicroFrontend />
|
141
142
|
|
143
|
+
### Other Main App
|
144
|
+
|
145
|
+
Install and use `react-router-dom` independently in the project. The only difference from self-controlled routing is that after setting `router: false`, plugin-garfish cannot obtain `useLocation`, `useHref` and other hooks to assist in calculating basename and main and child applications. Route synchronization
|
146
|
+
|
147
|
+
import { Tab, Tabs } from 'rspress/theme';
|
148
|
+
|
149
|
+
<Tabs>
|
150
|
+
<Tab label="react-router-dom@6">
|
151
|
+
```tsx
|
152
|
+
import { useLocation, useHref } from 'react-router-dom';
|
153
|
+
const App = () => {
|
154
|
+
const basename = useHref('/table');
|
155
|
+
const { Table } = useModuleApps();
|
156
|
+
return <Table useLocation={useLocation} basename={basename} />;
|
157
|
+
};
|
158
|
+
```
|
159
|
+
</Tab>
|
160
|
+
<Tab label="react-router-dom@5">
|
161
|
+
```tsx
|
162
|
+
import { useLocation, useHistory } from 'react-router-dom';
|
163
|
+
const App = () => {
|
164
|
+
const history = useHistory();
|
165
|
+
const basename = history.createHref({ pathname: '/table' });
|
166
|
+
const { Table } = useModuleApps();
|
167
|
+
return <Table useLocation={useLocation} basename={basename} />;
|
168
|
+
};
|
169
|
+
```
|
170
|
+
</Tab>
|
171
|
+
</Tabs>
|
172
|
+
|
142
173
|
### File-based sub-app
|
143
174
|
|
144
175
|
Initialize the project with a command line:
|
@@ -195,7 +195,7 @@ Options:
|
|
195
195
|
|
196
196
|
## modern inspect
|
197
197
|
|
198
|
-
`modern inspect` 命令用于查看项目的 [
|
198
|
+
`modern inspect` 命令用于查看项目的 [Rsbuild 配置](https://rsbuild.dev/zh/config/index) 以及 webpack 或 Rspack 配置。
|
199
199
|
|
200
200
|
```bash
|
201
201
|
Usage: modern inspect [options]
|
@@ -210,16 +210,16 @@ Options:
|
|
210
210
|
|
211
211
|
在项目根目录下执行命令 `npx modern inspect` 后,会在项目的 `dist` 目录生成以下文件:
|
212
212
|
|
213
|
-
- `
|
214
|
-
- `webpack.config.web.
|
213
|
+
- `rsbuild.config.mjs`: 表示在构建时使用的 Rsbuild 配置。
|
214
|
+
- `webpack.config.web.mjs`: 表示在构建时使用的 webpack 配置。
|
215
215
|
|
216
216
|
```bash
|
217
217
|
➜ npx modern inspect
|
218
218
|
|
219
219
|
Inspect config succeed, open following files to view the content:
|
220
220
|
|
221
|
-
-
|
222
|
-
- Webpack Config (web): /root/my-project/dist/webpack.config.web.
|
221
|
+
- Rsbuild Config: /root/my-project/dist/rsbuild.config.mjs
|
222
|
+
- Webpack Config (web): /root/my-project/dist/webpack.config.web.mjs
|
223
223
|
```
|
224
224
|
|
225
225
|
### 指定环境
|
@@ -240,16 +240,16 @@ modern inspect --verbose
|
|
240
240
|
|
241
241
|
### SSR 构建配置
|
242
242
|
|
243
|
-
如果项目开启了 SSR 能力,则在 `dist` 目录会另外生成一份 `webpack.config.node.
|
243
|
+
如果项目开启了 SSR 能力,则在 `dist` 目录会另外生成一份 `webpack.config.node.mjs` 文件,对应 SSR 构建时的 webpack 配置。
|
244
244
|
|
245
245
|
```bash
|
246
246
|
➜ npx modern inspect
|
247
247
|
|
248
248
|
Inspect config succeed, open following files to view the content:
|
249
249
|
|
250
|
-
-
|
251
|
-
- Webpack Config (web): /root/my-project/dist/webpack.config.web.
|
252
|
-
- Webpack Config (node): /root/my-project/dist/webpack.config.node.
|
250
|
+
- Rsbuild Config: /root/my-project/dist/rsbuild.config.mjs
|
251
|
+
- Webpack Config (web): /root/my-project/dist/webpack.config.web.mjs
|
252
|
+
- Webpack Config (node): /root/my-project/dist/webpack.config.node.mjs
|
253
253
|
```
|
254
254
|
|
255
255
|
## modern lint
|
@@ -39,14 +39,14 @@ Modern.js 将通过以下几个步骤帮你自动注册插件
|
|
39
39
|
1. Modern.js 在内部维护一份官方插件列表。
|
40
40
|
|
41
41
|
```js
|
42
|
-
const InternalPlugins = ['@modern-js/app-tools', '@modern-js/plugin-
|
42
|
+
const InternalPlugins = ['@modern-js/app-tools', '@modern-js/plugin-tailwindcss', ...];
|
43
43
|
```
|
44
44
|
|
45
45
|
2. Modern.js 将读取你的 `package.json` 文件,收集依赖信息。
|
46
46
|
|
47
47
|
```json title="package.json"
|
48
48
|
"dependencies": {
|
49
|
-
"@modern-js/plugin-
|
49
|
+
"@modern-js/plugin-tailwindcss": "x.x.x"
|
50
50
|
...
|
51
51
|
},
|
52
52
|
"devDependencies": {
|
@@ -55,7 +55,7 @@ const InternalPlugins = ['@modern-js/app-tools', '@modern-js/plugin-i18n', ...];
|
|
55
55
|
}
|
56
56
|
```
|
57
57
|
|
58
|
-
3. Modern.js 观察到你安装了 `@modern-js/plugin-
|
58
|
+
3. Modern.js 观察到你安装了 `@modern-js/plugin-tailwindcss` 和 `@modern-js/app-tools` 等依赖后,将会引入插件自动注册。
|
59
59
|
|
60
60
|
可以注意到这种方式相对黑盒,你甚至对加载插件的过程是无感知的。我们希望更多的细节暴露给开发者,能让开发者去控制这一过程。
|
61
61
|
|
@@ -4,18 +4,18 @@ sidebar_position: 21
|
|
4
4
|
|
5
5
|
# builderPlugins 构建插件
|
6
6
|
|
7
|
-
- **类型:** `
|
7
|
+
- **类型:** `RsbuildPlugin[]`
|
8
8
|
- **默认值:** `[]`
|
9
9
|
|
10
|
-
用于配置
|
10
|
+
用于配置 Rsbuild 构建插件。
|
11
11
|
|
12
|
-
|
12
|
+
Rsbuild 是 Modern.js 底层的构建工具,请阅读 [构建能力](/guides/concept/builder) 了解相关背景。
|
13
13
|
|
14
|
-
如果你想了解
|
14
|
+
如果你想了解 Rsbuild 插件的编写方式,可以参考 [Rsbuild - 插件系统](https://rsbuild.dev/zh/plugins/dev/index)。
|
15
15
|
|
16
16
|
## 注意事项
|
17
17
|
|
18
|
-
该选项**用于配置
|
18
|
+
该选项**用于配置 Rsbuild 构建插件**,如果你需要配置其他类型的插件,请选择对应的配置方式:
|
19
19
|
|
20
20
|
- 配置 Modern.js 框架插件,请使用 [plugins](/configure/app/plugins) 配置项。
|
21
21
|
- 配置 Rspack 或 webpack 插件,请使用 [tools.bundlerChain](/configure/app/tools/bundler-chain) 配置项。
|
@@ -23,23 +23,23 @@ Modern.js Builder 是 Modern.js 底层的构建工具,请阅读 [构建能力]
|
|
23
23
|
|
24
24
|
## 何时使用
|
25
25
|
|
26
|
-
大部分场景下,我们推荐你使用 Modern.js 框架插件,框架插件可以通过 [plugins](/configure/app/plugins) 字段进行注册。因为框架插件提供的 API 更丰富、能力更强,而
|
26
|
+
大部分场景下,我们推荐你使用 Modern.js 框架插件,框架插件可以通过 [plugins](/configure/app/plugins) 字段进行注册。因为框架插件提供的 API 更丰富、能力更强,而 Rsbuild 插件提供的 API 只能用于构建场景。
|
27
27
|
|
28
|
-
当你需要引用一些现有的
|
28
|
+
当你需要引用一些现有的 Rsbuild 插件(并且在 Modern.js 中没有相关能力),或是在不同的框架之间复用 Rsbuild 插件时,你可以使用 `builderPlugins` 字段进行注册。
|
29
29
|
|
30
30
|
## 示例
|
31
31
|
|
32
|
-
下面是
|
32
|
+
下面是 Rsbuild 插件的使用示例。
|
33
33
|
|
34
34
|
### 使用 npm 上的插件
|
35
35
|
|
36
36
|
使用 npm 上的插件,需要通过包管理器安装插件,并通过 import 引入。
|
37
37
|
|
38
38
|
```ts title="modern.config.ts"
|
39
|
-
import {
|
39
|
+
import { myRsbuildPlugin } from 'my-rsbuild-plugin';
|
40
40
|
|
41
41
|
export default defineConfig({
|
42
|
-
builderPlugins: [
|
42
|
+
builderPlugins: [myRsbuildPlugin()],
|
43
43
|
});
|
44
44
|
```
|
45
45
|
|
@@ -48,10 +48,10 @@ export default defineConfig({
|
|
48
48
|
使用本地代码仓库中的插件,直接通过相对路径 import 引入即可。
|
49
49
|
|
50
50
|
```ts title="modern.config.ts"
|
51
|
-
import {
|
51
|
+
import { myRsbuildPlugin } from './plugin/myRsbuildPlugin';
|
52
52
|
|
53
53
|
export default defineConfig({
|
54
|
-
builderPlugins: [
|
54
|
+
builderPlugins: [myRsbuildPlugin()],
|
55
55
|
});
|
56
56
|
```
|
57
57
|
|
@@ -60,11 +60,11 @@ export default defineConfig({
|
|
60
60
|
如果插件提供了一些自定义的配置项,可以通过插件函数的参数传入配置。
|
61
61
|
|
62
62
|
```ts title="modern.config.ts"
|
63
|
-
import {
|
63
|
+
import { myRsbuildPlugin } from 'my-rsbuild-plugin';
|
64
64
|
|
65
65
|
export default defineConfig({
|
66
66
|
builderPlugins: [
|
67
|
-
|
67
|
+
myRsbuildPlugin({
|
68
68
|
foo: 1,
|
69
69
|
bar: 2,
|
70
70
|
}),
|
@@ -19,4 +19,4 @@ export default defineConfig({
|
|
19
19
|
});
|
20
20
|
```
|
21
21
|
|
22
|
-
该配置项基于
|
22
|
+
该配置项基于 Rsbuild 的 Node Polyfill 插件实现,你可以阅读 [Rsbuild - Node Polyfill 插件](https://rsbuild.dev/zh/plugins/list/plugin-node-polyfill) 文档来了解详细信息。
|
@@ -27,7 +27,7 @@ export default defineConfig({
|
|
27
27
|
|
28
28
|
- `mode`:`string = 'string'`,默认为使用 `renderToString` 渲染。配置为 `stream` 开启流式渲染。
|
29
29
|
- `forceCSR`:`boolean = false`,默认关闭强制 CSR 渲染。配置为 `true` 后,在页面访问时添加 `?csr=true` 或添加请求头 `x-modern-ssr-fallback` 即可强制 CSR。
|
30
|
-
- `inlineScript`:`boolean = true`,默认情况下,SSR 的数据会以内联脚本的方式注入到 HTML 中,并且直接赋值给全局变量。配置为 `false` 后,会下发 JSON
|
30
|
+
- `inlineScript`:`boolean = true`,默认情况下,SSR 的数据会以内联脚本的方式注入到 HTML 中,并且直接赋值给全局变量。配置为 `false` 后,会下发 JSON,而不是赋值给全局变量,Streaming SSR 下,该配置不会生效。
|
31
31
|
- `disablePrerender`: `boolean = fasle`, 为了兼容旧数据请求方式 - `useLoader`, 默认情况下 Modern.js 会对组件进行一次预渲染即有两次渲染。
|
32
32
|
开发者在保证项目中没有使用 useLoader Api 情况下, 可通过配置 `disablePrerender=true`来减少一次渲染。
|
33
33
|
- `unsafeHeaders`: `string[] = []`, 为了安全考虑,Modern.js 不会往 SSR_DATA 添加过多的内容。开发者可以通过该配置,对需要注入的 headers 进行配置。
|
@@ -7,18 +7,18 @@ sidebar_label: esbuild
|
|
7
7
|
- **类型:** `Object`
|
8
8
|
- **默认值:** `undefined`
|
9
9
|
|
10
|
-
|
10
|
+
:::warning
|
11
|
+
**当前文档中的 esbuild 功能已停止迭代**,我们更推荐使用 Rspack + SWC 的方案,因为 Rspack + SWC 具备更好的构建性能、功能丰富度和产物兼容性。
|
11
12
|
|
12
|
-
|
13
|
+
请参考[「使用 Rspack」](/guides/advanced-features/rspack-start.html)了解更多。
|
13
14
|
|
14
|
-
|
15
|
+
:::
|
15
16
|
|
16
|
-
|
17
|
-
相较于 esbuild,我们更推荐使用 SWC 来编译和压缩代码,因为 SWC 支持更多的语法特性、拥有更好的代码压缩率,并且产物具备更好的兼容性。
|
17
|
+
## 介绍
|
18
18
|
|
19
|
-
|
19
|
+
import Esbuild from '@modern-js/builder-doc/docs/zh/shared/esbuild.md';
|
20
20
|
|
21
|
-
|
21
|
+
<Esbuild />
|
22
22
|
|
23
23
|
## 配置项
|
24
24
|
|
@@ -44,9 +44,9 @@ BUNDLE_ANALYZE=true pnpm build
|
|
44
44
|
|
45
45
|
## 提升 Browserslist 范围
|
46
46
|
|
47
|
-
|
47
|
+
Modern.js 会根据项目的 Browserslist 配置范围进行代码编译,并注入相应的 Polyfill。如果项目不需要兼容旧版浏览器,可以根据实际情况来提升 Browserslist 范围,从而减少在语法和 Polyfill 上的编译开销。
|
48
48
|
|
49
|
-
|
49
|
+
Modern.js 默认的 Browserslist 配置为:
|
50
50
|
|
51
51
|
```js
|
52
52
|
['> 0.01%', 'not dead', 'not op_mini all'];
|
@@ -59,14 +59,14 @@ Builder 默认的 Browserslist 配置为:
|
|
59
59
|
```
|
60
60
|
|
61
61
|
:::tip
|
62
|
-
请阅读 [设置浏览器范围](https://
|
62
|
+
请阅读 [设置浏览器范围](https://rsbuild.dev/zh/guide/advanced/browserslist) 章节来了解更多关于 Browserslist 的用法。
|
63
63
|
:::
|
64
64
|
|
65
65
|
## 按需引入 polyfill
|
66
66
|
|
67
67
|
在明确第三方依赖不需要额外 polyfill 的情况下,你可以将 [output.polyfill](/configure/app/output/polyfill.html) 设置为 `usage`。
|
68
68
|
|
69
|
-
在 `usage` 模式下,
|
69
|
+
在 `usage` 模式下,Modern.js 会分析源代码中使用的语法,按需注入所需的 polyfill 代码,从而减少 polyfill 的代码量。
|
70
70
|
|
71
71
|
```js
|
72
72
|
export default {
|
@@ -85,20 +85,20 @@ export default {
|
|
85
85
|
在一般的前端项目中,图片资源的体积往往是项目产物体积的大头,因此如果能尽可能精简图片的体积,那么将会对项目的打包产物体积起到明显的优化效果。你可以在 Modern.js 中注册插件来启用图片压缩功能:
|
86
86
|
|
87
87
|
```js title="modern.config.ts"
|
88
|
-
import {
|
88
|
+
import { pluginImageCompress } from '@rsbuild/plugin-image-compress';
|
89
89
|
|
90
90
|
export default {
|
91
|
-
builderPlugins: [
|
91
|
+
builderPlugins: [pluginImageCompress()],
|
92
92
|
};
|
93
93
|
```
|
94
94
|
|
95
|
-
详见 [Image Compress 插件](https://
|
95
|
+
详见 [Image Compress 插件](https://rsbuild.dev/zh/plugins/list/plugin-image-compress)。
|
96
96
|
|
97
97
|
## 代码拆包
|
98
98
|
|
99
99
|
良好的拆包策略对于提升应用的加载性能是十分重要的,可以充分利用浏览器的缓存机制,减少请求数量,加快页面加载速度。
|
100
100
|
|
101
|
-
在 Modern.js 中内置了[多种拆包策略](https://
|
101
|
+
在 Modern.js 中内置了[多种拆包策略](https://rsbuild.dev/zh/guide/optimization/split-chunk),可以满足大部分应用的需求,你也可以根据自己的业务场景,自定义拆包配置。
|
102
102
|
|
103
103
|
比如将 node_modules 下的 `axios` 库拆分到 `axios.js` 中:
|
104
104
|
|
@@ -0,0 +1,51 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 21
|
3
|
+
title: 使用 Rsbuild 插件
|
4
|
+
---
|
5
|
+
|
6
|
+
# 使用 Rsbuild 插件
|
7
|
+
|
8
|
+
## 插件介绍
|
9
|
+
|
10
|
+
Rsbuild 是 Modern.js 底层的构建工具,可通过添加 Rsbuild 插件修改默认的构建行为并添加各类额外功能,包括但不限于:
|
11
|
+
|
12
|
+
- 修改 Rsbuild 配置
|
13
|
+
- 处理新的文件类型
|
14
|
+
- 修改或编译文件
|
15
|
+
- 部署产物
|
16
|
+
|
17
|
+
你可以在 `modern.config.ts` 中通过 `builderPlugins` 选项来注册 Rsbuild 插件,详见 [builderPlugins 构建插件](/configure/app/builder-plugins.html)。
|
18
|
+
|
19
|
+
## 官方插件
|
20
|
+
|
21
|
+
:::tip
|
22
|
+
Rsbuild 中大部分插件已内置在 Modern.js 中,如:需要注入 Node 模块的 Polyfill 时,可直接使用 [output.disableNodePolyfill](/configure/app/output/disable-node-polyfill.html) 配置项。
|
23
|
+
:::
|
24
|
+
|
25
|
+
### 内置插件
|
26
|
+
|
27
|
+
以下是已在 Modern.js 中内置的 Rsbuild 官方插件:
|
28
|
+
|
29
|
+
| 插件 | 介绍 | Modern.js 链接 |
|
30
|
+
| -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
31
|
+
| [React 插件](https://rsbuild.dev/zh/plugins/list/plugin-react) | 提供对 React 的支持 | - |
|
32
|
+
| [SVGR 插件](https://rsbuild.dev/zh/plugins/list/plugin-svgr) | 支持将 SVG 图片转换为一个 React 组件使用 | [output.disableSvgr](/configure/app/output/disable-svgr)<br />[output.svgDefaultExport](/configure/app/output/svg-default-export) |
|
33
|
+
| [Styled Components 插件](https://rsbuild.dev/zh/plugins/list/plugin-styled-components) | 提供对 styled-components 的编译时支持 | [tools.styledComponents](/configure/app/tools/styled-components.html) |
|
34
|
+
| [Assets Retry 插件](https://rsbuild.dev/zh/plugins/list/plugin-assets-retry) | 用于在静态资源加载失败时自动发起重试请求 | [output.assetsRetry](/configure/app/output/assets-retry.html) |
|
35
|
+
| [Type Check 插件](https://rsbuild.dev/zh/plugins/list/plugin-type-check) | 用于在单独的进程中运行 TypeScript 类型检查 | [output.disableTsChecker](/configure/app/output/disable-ts-checker.html)<br />[tools.tsChecker](/configure/app/tools/ts-checker.html) |
|
36
|
+
| [Node Polyfill 插件](https://rsbuild.dev/zh/plugins/list/plugin-node-polyfill) | 用于注入 Node 核心模块在浏览器端的 polyfills | [output.disableNodePolyfill](/configure/app/output/disable-node-polyfill.html) |
|
37
|
+
| [Source Build 插件](https://rsbuild.dev/zh/plugins/list/plugin-source-build) | 用于 monorepo 场景,支持引用其他子目录的源代码,并完成构建和热更新 | [experiments.sourceBuild](/configure/app/experiments/source-build.html) |
|
38
|
+
| [Check Syntax 插件](https://rsbuild.dev/zh/plugins/list/plugin-check-syntax) | 用于分析产物的语法兼容性,判断是否存在导致兼容性问题的高级语法 | [security.checkSyntax](/configure/app/security/check-syntax.html) |
|
39
|
+
| [CSS Minimizer 插件](https://rsbuild.dev/zh/plugins/list/plugin-css-minimizer) | 用于自定义 CSS 压缩工具,切换到 [cssnano](https://cssnano.co/) 或其他工具进行 CSS 压缩。 | [tools.minifyCss](/configure/app/tools/minify-css.html) |
|
40
|
+
| [Pug 插件](https://rsbuild.dev/zh/plugins/list/plugin-pug) | 提供对 Pug 模板引擎的支持 | [tools.pug](/configure/app/tools/pug.html) |
|
41
|
+
| [Rem 插件](https://rsbuild.dev/zh/plugins/list/plugin-rem) | 用于实现移动端页面的 rem 自适应布局 | [output.convertToRem](/configure/app/output/convert-to-rem.html) |
|
42
|
+
| [YAML 插件](https://rsbuild.dev/zh/plugins/list/plugin-yaml) | 用于引用 YAML 文件,并将其转换为 JavaScript 对象 | [TOML 文件](/guides/basic-features/json-files.html#toml-文件) |
|
43
|
+
| [TOML 插件](https://rsbuild.dev/zh/plugins/list/plugin-toml) | 用于引用 TOML 文件,并将其转换为 JavaScript 对象 | [YAML 文件](/guides/basic-features/json-files.html#yaml-文件) |
|
44
|
+
|
45
|
+
### 未内置插件
|
46
|
+
|
47
|
+
以下是未在 Modern.js 中内置的 Rsbuild 官方插件:
|
48
|
+
|
49
|
+
- [Image Compress 插件](https://rsbuild.dev/zh/plugins/list/plugin-image-compress):将项目中用到的图片资源进行压缩处理。
|
50
|
+
- [Stylus 插件](https://rsbuild.dev/zh/plugins/list/plugin-stylus):使用 Stylus 作为 CSS 预处理器。
|
51
|
+
- [UMD 插件](https://rsbuild.dev/zh/plugins/list/plugin-umd):用于构建 UMD 格式的产物。
|
@@ -60,7 +60,7 @@ Modern.js 中 [tools.webpack](/configure/app/tools/webpack) 和 [tools.webpackCh
|
|
60
60
|
```diff
|
61
61
|
export default {
|
62
62
|
tools: {
|
63
|
-
-
|
63
|
+
- webpack: (config, { env }) => {
|
64
64
|
+ rspack: (config, { env }) => {
|
65
65
|
if (env === 'development') {
|
66
66
|
config.devtool = 'cheap-module-eval-source-map';
|