@modern-js/main-doc 2.65.4 → 2.66.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/en/apis/app/hooks/config/_meta.json +1 -0
- package/docs/en/apis/app/hooks/config/favicon.mdx +29 -0
- package/docs/en/apis/app/hooks/config/icon.mdx +3 -30
- package/docs/en/community/blog/v2-release-note.mdx +1 -1
- package/docs/en/configure/app/plugins.mdx +2 -2
- package/docs/en/configure/app/tools/esbuild.mdx +1 -1
- package/docs/en/configure/app/tools/swc.mdx +1 -1
- package/docs/en/plugin/_meta.json +8 -7
- package/docs/en/plugin/cli-plugins/_meta.json +1 -1
- package/docs/en/plugin/cli-plugins/api.mdx +617 -0
- package/docs/en/plugin/cli-plugins/life-cycle.mdx +139 -0
- package/docs/en/plugin/cli-plugins/migration.mdx +98 -0
- package/docs/en/plugin/introduction.mdx +119 -47
- package/docs/en/plugin/official/_meta.json +12 -0
- package/docs/en/plugin/official/cli-plugins/_meta.json +1 -0
- package/docs/en/plugin/official/cli-plugins.mdx +6 -0
- package/docs/en/plugin/official/rsbuild-plugins.mdx +3 -0
- package/docs/en/plugin/plugin-system.mdx +237 -0
- package/docs/en/plugin/runtime-plugins/_meta.json +1 -0
- package/docs/en/plugin/runtime-plugins/api.mdx +165 -0
- package/docs/en/plugin/runtime-plugins/life-cycle.mdx +29 -0
- package/docs/en/plugin/runtime-plugins/migration.mdx +101 -0
- package/docs/en/plugin/server-plugins/api.mdx +3 -0
- package/docs/en/plugin/server-plugins/life-cycle.mdx +3 -0
- package/docs/zh/apis/app/hooks/config/_meta.json +1 -0
- package/docs/zh/apis/app/hooks/config/favicon.mdx +29 -0
- package/docs/zh/apis/app/hooks/config/icon.mdx +3 -30
- package/docs/zh/community/blog/v2-release-note.mdx +1 -1
- package/docs/zh/configure/app/plugins.mdx +2 -2
- package/docs/zh/configure/app/tools/esbuild.mdx +1 -1
- package/docs/zh/configure/app/tools/swc.mdx +1 -1
- package/docs/zh/plugin/_meta.json +8 -7
- package/docs/zh/plugin/cli-plugins/_meta.json +1 -1
- package/docs/zh/plugin/cli-plugins/api.mdx +617 -0
- package/docs/zh/plugin/cli-plugins/life-cycle.mdx +139 -0
- package/docs/zh/plugin/cli-plugins/migration.mdx +98 -0
- package/docs/zh/plugin/introduction.mdx +92 -20
- package/docs/zh/plugin/official/_meta.json +12 -0
- package/docs/zh/plugin/official/cli-plugins/_meta.json +1 -0
- package/docs/zh/plugin/official/cli-plugins.mdx +6 -0
- package/docs/zh/plugin/official/rsbuild-plugins.mdx +3 -0
- package/docs/zh/plugin/plugin-system.mdx +239 -0
- package/docs/zh/plugin/runtime-plugins/_meta.json +1 -0
- package/docs/zh/plugin/runtime-plugins/api.mdx +166 -0
- package/docs/zh/plugin/runtime-plugins/life-cycle.mdx +29 -0
- package/docs/zh/plugin/runtime-plugins/migration.mdx +101 -0
- package/docs/zh/plugin/server-plugins/api.mdx +3 -0
- package/docs/zh/plugin/server-plugins/life-cycle.mdx +3 -0
- package/i18n.json +4 -0
- package/package.json +4 -4
- package/src/components/Footer/index.tsx +1 -1
- package/src/components/Mermaid/index.tsx +60 -0
- package/src/components/Mermaid/style.scss +221 -0
- package/docs/en/plugin/cli-plugins.mdx +0 -6
- package/docs/en/plugin/plugin-system/_meta.json +0 -10
- package/docs/en/plugin/plugin-system/extend.mdx +0 -163
- package/docs/en/plugin/plugin-system/hook-list.mdx +0 -711
- package/docs/en/plugin/plugin-system/hook.mdx +0 -188
- package/docs/en/plugin/plugin-system/implement.mdx +0 -243
- package/docs/en/plugin/plugin-system/introduction.mdx +0 -95
- package/docs/en/plugin/plugin-system/lifecycle.mdx +0 -16
- package/docs/en/plugin/plugin-system/plugin-api.mdx +0 -138
- package/docs/en/plugin/plugin-system/relationship.mdx +0 -119
- package/docs/en/plugin/rsbuild-plugins.mdx +0 -3
- package/docs/zh/plugin/cli-plugins.mdx +0 -6
- package/docs/zh/plugin/plugin-system/_meta.json +0 -10
- package/docs/zh/plugin/plugin-system/extend.mdx +0 -163
- package/docs/zh/plugin/plugin-system/hook-list.mdx +0 -715
- package/docs/zh/plugin/plugin-system/hook.mdx +0 -173
- package/docs/zh/plugin/plugin-system/implement.mdx +0 -250
- package/docs/zh/plugin/plugin-system/introduction.mdx +0 -94
- package/docs/zh/plugin/plugin-system/lifecycle.mdx +0 -16
- package/docs/zh/plugin/plugin-system/plugin-api.mdx +0 -138
- package/docs/zh/plugin/plugin-system/relationship.mdx +0 -119
- package/docs/zh/plugin/rsbuild-plugins.mdx +0 -4
- /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-bff.mdx +0 -0
- /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-ssg.mdx +0 -0
- /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-swc.mdx +0 -0
- /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-tailwind.mdx +0 -0
- /package/docs/en/plugin/{rsbuild-plugins → official/rsbuild-plugins}/_meta.json +0 -0
- /package/docs/en/plugin/{rsbuild-plugins → official/rsbuild-plugins}/plugin-esbuild.mdx +0 -0
- /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-bff.mdx +0 -0
- /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-ssg.mdx +0 -0
- /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-swc.mdx +0 -0
- /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-tailwind.mdx +0 -0
- /package/docs/zh/plugin/{rsbuild-plugins → official/rsbuild-plugins}/_meta.json +0 -0
- /package/docs/zh/plugin/{rsbuild-plugins → official/rsbuild-plugins}/plugin-esbuild.mdx +0 -0
@@ -0,0 +1,139 @@
|
|
1
|
+
# 生命周期
|
2
|
+
|
3
|
+
import Mermaid from '@site/src/components/Mermaid';
|
4
|
+
|
5
|
+
<Mermaid>
|
6
|
+
{`
|
7
|
+
flowchart TD
|
8
|
+
init{{"CLI 初始化"}}
|
9
|
+
env(初始化环境变量)
|
10
|
+
load_config(加载用户配置文件)
|
11
|
+
cli_plugin(加载 CLI 插件)
|
12
|
+
registry_hooks(注册 Hooks 函数<br><sub>执行插件 setup 函数,注册插件中定义的 Hook, 插件 setup 中逻辑也会在这里执行</sub>)
|
13
|
+
|
14
|
+
app_tools{{"app-tools"}}
|
15
|
+
|
16
|
+
config(config)
|
17
|
+
onPrepare(onPrepare)
|
18
|
+
addCommand(addCommand)
|
19
|
+
addWatchFiles(addWatchFiles)
|
20
|
+
onFileChanged(onFileChanged)
|
21
|
+
onBeforeExit(onBeforeExit<br><sub>SIGINT, SIGTERM, unhandledRejection, uncaughtException 事件触发</sub>)
|
22
|
+
|
23
|
+
onBeforeDev(onBeforeDev)
|
24
|
+
onBeforeRestart(onBeforeRestart<br><sub>修改的文件需要重启服务时触发</sub>)
|
25
|
+
onBeforeBuild(onBeforeBuild<br><sub>在独立 BFF 项目时触发</sub>)
|
26
|
+
onAfterBuild(onAfterBuild<br><sub>在独立 BFF 项目时触发</sub>)
|
27
|
+
modifyServerRoutes(modifyServerRoutes)
|
28
|
+
modifyHtmlPartials(modifyHtmlPartials)
|
29
|
+
|
30
|
+
onPrepare_fork(onPrepare)
|
31
|
+
addCommand_fork(addCommand)
|
32
|
+
onFileChanged_fork(onFileChanged)
|
33
|
+
|
34
|
+
registry_rsbuild_hooks(注册 rsbuild Hooks<br><sub>这里只是注册 rsbuild 的 Hook,具体的执行时机是由 rsbuild 的插件机制决定的</sub>)
|
35
|
+
|
36
|
+
modifyBundlerChain(modifyBundlerChain)
|
37
|
+
modifyRsbuildConfig(modifyRsbuildConfig)
|
38
|
+
modifyRspackConfig(modifyRspackConfig)
|
39
|
+
modifyWebpackChain(modifyWebpackChain)
|
40
|
+
modifyWebpackConfig(modifyWebpackConfig)
|
41
|
+
|
42
|
+
onBeforeBuild_rsbuild(onBeforeBuild)
|
43
|
+
onAfterBuild_rsbuild(onAfterBuild)
|
44
|
+
onDevCompileDone(onDevCompileDone)
|
45
|
+
onBeforeCreateCompiler(onBeforeCreateCompiler)
|
46
|
+
onAfterCreateCompiler(onAfterCreateCompiler)
|
47
|
+
onAfterDev(onAfterDev<br><sub>对应 rsbuild 的 onAfterStartDevServer Hook</sub>)
|
48
|
+
|
49
|
+
dev[/dev/]
|
50
|
+
build[/build/]
|
51
|
+
extra_command[/.../]
|
52
|
+
|
53
|
+
dev_fork[/dev/]
|
54
|
+
build_fork[/build/]
|
55
|
+
serve[/serve/]
|
56
|
+
deploy[/deploy/]
|
57
|
+
inspect[/inspect/]
|
58
|
+
upgrade[/upgrade/]
|
59
|
+
|
60
|
+
onPrepare_fork --> modifyServerRoutes
|
61
|
+
modifyServerRoutes --> modifyHtmlPartials
|
62
|
+
modifyHtmlPartials --> registry_rsbuild_hooks
|
63
|
+
registry_rsbuild_hooks --> modifyBundlerChain
|
64
|
+
modifyBundlerChain --> modifyRsbuildConfig
|
65
|
+
modifyRsbuildConfig --> modifyRspackConfig
|
66
|
+
modifyRspackConfig --> modifyWebpackChain
|
67
|
+
modifyWebpackChain --> modifyWebpackConfig
|
68
|
+
registry_rsbuild_hooks --> onBeforeBuild_rsbuild
|
69
|
+
onBeforeBuild_rsbuild --> onAfterBuild_rsbuild
|
70
|
+
onAfterBuild_rsbuild --> onDevCompileDone
|
71
|
+
onDevCompileDone --> onBeforeCreateCompiler
|
72
|
+
onBeforeCreateCompiler --> onAfterCreateCompiler
|
73
|
+
onAfterCreateCompiler --> onAfterDev
|
74
|
+
|
75
|
+
dev_fork --> onBeforeDev
|
76
|
+
onBeforeDev --> onFileChanged_fork
|
77
|
+
onFileChanged_fork --> onBeforeRestart
|
78
|
+
|
79
|
+
build_fork --> onBeforeBuild
|
80
|
+
onBeforeBuild --> onAfterBuild
|
81
|
+
|
82
|
+
app_tools --- spacer5[ ] --- spacer6[ ] --- spacer7[ ] --- spacer8[ ] --- spacer9[ ] --> onPrepare_fork
|
83
|
+
onPrepare_fork --> addCommand_fork
|
84
|
+
addCommand_fork --> dev_fork
|
85
|
+
addCommand_fork --> build_fork
|
86
|
+
addCommand_fork --> serve
|
87
|
+
addCommand_fork --> deploy
|
88
|
+
addCommand_fork --> inspect
|
89
|
+
addCommand_fork --> upgrade
|
90
|
+
|
91
|
+
init --> env
|
92
|
+
env --> load_config
|
93
|
+
load_config --> cli_plugin
|
94
|
+
cli_plugin --> registry_hooks
|
95
|
+
|
96
|
+
registry_hooks --> config
|
97
|
+
config --> onPrepare
|
98
|
+
onPrepare --> addCommand
|
99
|
+
|
100
|
+
addCommand --> dev
|
101
|
+
addCommand --> build
|
102
|
+
addCommand --> extra_command
|
103
|
+
|
104
|
+
dev --> addWatchFiles
|
105
|
+
addWatchFiles --> onFileChanged
|
106
|
+
|
107
|
+
onFileChanged --> onBeforeExit
|
108
|
+
build --- spacer[ ] --- spacer2[ ] --> onBeforeExit
|
109
|
+
extra_command --- spacer3[ ] --- spacer4[ ] --> onBeforeExit
|
110
|
+
|
111
|
+
style init fill:#FDE68A;
|
112
|
+
style app_tools fill:#FDE68A;
|
113
|
+
style env stroke-dasharray:5 5,fill:#86EFAC;
|
114
|
+
style registry_rsbuild_hooks stroke-dasharray:5 5,fill:#9CA3AF;
|
115
|
+
style load_config stroke-dasharray:5 5,fill:#86EFAC;
|
116
|
+
style cli_plugin stroke-dasharray:5 5,fill:#86EFAC;
|
117
|
+
style registry_hooks stroke-dasharray:5 5,fill:#9CA3AF;
|
118
|
+
|
119
|
+
style dev fill:#7DD3FC;
|
120
|
+
style build fill:#7DD3FC;
|
121
|
+
style extra_command fill:#7DD3FC;
|
122
|
+
style dev_fork fill:#7DD3FC;
|
123
|
+
style build_fork fill:#7DD3FC;
|
124
|
+
style serve fill:#7DD3FC;
|
125
|
+
style upgrade fill:#7DD3FC;
|
126
|
+
style deploy fill:#7DD3FC;
|
127
|
+
style inspect fill:#7DD3FC;
|
128
|
+
|
129
|
+
style spacer fill:none,stroke:none,height:0px;
|
130
|
+
style spacer2 fill:none,stroke:none,height:0px;
|
131
|
+
style spacer3 fill:none,stroke:none,height:0px;
|
132
|
+
style spacer4 fill:none,stroke:none,height:0px;
|
133
|
+
style spacer5 fill:none,stroke:none,height:0px;
|
134
|
+
style spacer6 fill:none,stroke:none,height:0px;
|
135
|
+
style spacer7 fill:none,stroke:none,height:0px;
|
136
|
+
style spacer8 fill:none,stroke:none,height:0px;
|
137
|
+
style spacer9 fill:none,stroke:none,height:0px;
|
138
|
+
`}
|
139
|
+
</Mermaid>
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# 插件迁移
|
2
|
+
|
3
|
+
### 迁移背景
|
4
|
+
|
5
|
+
Modern.js 插件系统在不断演进,为了提供更清晰的 API 和更强大的功能,我们对 CLI 插件的定义和使用方式进行了优化。虽然旧版插件写法仍被兼容,但我们强烈建议您按照本指南进行迁移,以充分利用新版插件系统的优势。
|
6
|
+
|
7
|
+
### 迁移步骤总览
|
8
|
+
|
9
|
+
1. **更新插件类型定义**:将 `CliPlugin` 类型替换为 `CliPluginFuture`。
|
10
|
+
2. **调整 Hooks 调用方式**:从 `return hooks` 模式迁移到 `api.xxx` 直接调用。
|
11
|
+
3. **替换已变更的 API**:参照详细的 API 映射表,更新您的代码。
|
12
|
+
|
13
|
+
### 详细迁移步骤
|
14
|
+
|
15
|
+
#### 更新插件类型定义
|
16
|
+
|
17
|
+
这是迁移的第一步,也是最关键的一步。它确保您的插件能够与新版插件系统正确交互。
|
18
|
+
|
19
|
+
```typescript
|
20
|
+
// 旧版写法
|
21
|
+
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
22
|
+
|
23
|
+
const plugin: CliPlugin<AppTools<'shared'>> = { ... };
|
24
|
+
|
25
|
+
// 新版写法
|
26
|
+
import type { CliPluginFuture, AppTools } from '@modern-js/app-tools';
|
27
|
+
|
28
|
+
const plugin: CliPluginFuture<AppTools<'shared'>> = { ... };
|
29
|
+
```
|
30
|
+
|
31
|
+
**说明:** `CliPluginFuture` 类型是新版插件的标准定义,它带来了更好的类型推断和更清晰的 API 结构。
|
32
|
+
|
33
|
+
#### 调整 Hooks 调用方式
|
34
|
+
|
35
|
+
新版插件系统推荐使用 `api` 对象直接调用 Hooks,这种方式更直观、更易于维护。
|
36
|
+
|
37
|
+
```typescript
|
38
|
+
// 旧版写法 (return hooks)
|
39
|
+
{
|
40
|
+
setup: () => ({
|
41
|
+
commands({ program }) { /*...*/ }
|
42
|
+
})
|
43
|
+
}
|
44
|
+
|
45
|
+
// 新版写法 (api.xxx)
|
46
|
+
{
|
47
|
+
setup: api => {
|
48
|
+
api.addCommand(({ program }) => { /*...*/ })
|
49
|
+
}
|
50
|
+
}
|
51
|
+
```
|
52
|
+
|
53
|
+
**说明:** `api` 对象提供了所有可用的 Hooks 和工具方法。
|
54
|
+
|
55
|
+
#### 替换已变更的 API
|
56
|
+
|
57
|
+
为了保持 API 的一致性和清晰性,我们对部分 API 的名称进行了调整。下表列出了所有变更的 API 及其新旧对应关系:
|
58
|
+
|
59
|
+
| 旧版 API | 新版 API | 说明 |
|
60
|
+
| :----------------------- | :---------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
61
|
+
| `useAppContext` | `getAppContext` | 获取应用上下文信息。 |
|
62
|
+
| `useConfigContext` | `getConfig` | 获取用户配置文件中的配置。 |
|
63
|
+
| `useResolvedConfigContext` | `getNormalizedConfig` | 获取经过插件处理后的最终配置。 |
|
64
|
+
| `beforeConfig` | (直接在 `setup` 中定义) | 不再需要单独的 `beforeConfig` Hook,直接在 `setup` 函数中编写相关逻辑即可。 |
|
65
|
+
| `prepare` | `onPrepare` | 运行主流程的前置准备阶段。 |
|
66
|
+
| `afterPrepare` | `onAfterPrepare` (不推荐) | 在 `onPrepare` 之后执行,但可能在未来版本中废弃。建议将逻辑合并到 `onPrepare` 中。 |
|
67
|
+
| `beforePrintInstructions`| `onBeforePrintInstructions` | 在打印日志信息前执行。 |
|
68
|
+
| `commands` | `addCommand` | 添加新的 CLI 命令。 |
|
69
|
+
| `watchFiles` | `addWatchFiles` | 添加需要监听的文件。 |
|
70
|
+
| `fileChange` | `onFileChanged` | 监听文件变化事件。 |
|
71
|
+
| `beforeCreateCompiler` | `onBeforeCreateCompiler` | 创建编译器前执行。 |
|
72
|
+
| `afterCreateCompiler` | `onAfterCreateCompiler` | 创建编译器后执行。 |
|
73
|
+
| `beforeBuild` | `onBeforeBuild` | 构建前执行。 |
|
74
|
+
| `afterBuild` | `onAfterBuild` | 构建后执行。 |
|
75
|
+
| `beforeDev` | `onBeforeDev` | 运行 `dev` 命令前执行。 |
|
76
|
+
| `afterDev` | `onDevCompileDone` | `dev` 命令编译完成后执行。 |
|
77
|
+
| `beforeExit` | `onBeforeExit` | 进程退出前执行。 |
|
78
|
+
| `htmlPartials` | `modifyHtmlPartials` | 修改 HTML 模板片段。 |
|
79
|
+
|
80
|
+
**说明:** 请务必仔细检查您的代码,确保所有旧版 API 都已替换为新版 API。
|
81
|
+
|
82
|
+
### 常见问题解答
|
83
|
+
|
84
|
+
**Q: 迁移后,我的插件还能正常工作吗?**
|
85
|
+
|
86
|
+
A: 只要您按照本指南正确完成了所有步骤,您的插件应该能够正常工作。如果您遇到任何问题,请查阅 Modern.js 的官方文档或寻求社区支持。
|
87
|
+
|
88
|
+
**Q: 我必须立即迁移我的插件吗?**
|
89
|
+
|
90
|
+
A: 虽然旧版插件写法仍然兼容,但我们强烈建议您尽快迁移。新版插件系统提供了更好的性能和更丰富的功能,长期来看,迁移是值得的。
|
91
|
+
|
92
|
+
**Q: 我可以在哪里找到更多关于新版插件系统的信息?**
|
93
|
+
|
94
|
+
A: 请查阅 Modern.js 的官方文档,特别是关于插件系统的部分。您也可以参考其他已迁移的插件示例,了解最佳实践。
|
95
|
+
|
96
|
+
### 总结
|
97
|
+
|
98
|
+
通过这份详细的迁移指南,我们希望能够帮助您顺利地将您的 CLI 插件迁移到 Modern.js 的新版插件系统。如果您在迁移过程中遇到任何问题,请随时向我们寻求帮助。
|
@@ -1,65 +1,137 @@
|
|
1
|
-
#
|
1
|
+
# 插件介绍
|
2
2
|
|
3
|
-
|
3
|
+
Modern.js 提供了一套强大的插件体系,允许开发者通过编写插件来扩展框架功能、定制构建流程,以及满足各种个性化的开发需求。无论你是想添加一个自定义命令、优化构建产物,还是实现一套独特的部署方案,Modern.js 的插件系统都能为你提供强大的支持。
|
4
4
|
|
5
|
-
##
|
5
|
+
## 为什么需要插件?
|
6
|
+
|
7
|
+
在 Web 应用开发中,我们经常会遇到一些框架本身无法直接满足的需求,例如:
|
8
|
+
|
9
|
+
- **我想添加一个自定义的命令行工具,来帮助我自动化一些任务。**
|
10
|
+
- **我希望能够处理一种新的文件格式,比如 `.xyz`。**
|
11
|
+
- **我需要在应用启动前执行一些初始化的操作。**
|
12
|
+
- **我想对构建生成的 CSS 文件进行特殊的处理。**
|
13
|
+
- **我需要定制应用的路由逻辑,或者添加一些服务端中间件。**
|
14
|
+
|
15
|
+
在没有插件系统的情况下,这些需求可能需要修改框架源码,或者采用一些繁琐的 hack 手段。而 Modern.js 的插件系统提供了一种优雅、灵活且可维护的解决方案。
|
16
|
+
|
17
|
+
## 何时使用哪种插件?
|
18
|
+
|
19
|
+
Modern.js 提供了两种主要的插件类型:Modern.js 框架插件和 Rsbuild 构建插件。选择哪种插件取决于你的具体需求:
|
20
|
+
|
21
|
+
- **Rsbuild 构建插件:** 如果你的需求与构建过程密切相关,特别是涉及到 Webpack 或 Rspack 配置的修改,那么你应该选择 Rsbuild 插件。例如:
|
22
|
+
- 修改 Webpack/Rspack 的 `loader` 或 `plugin` 配置。
|
23
|
+
- 处理新的文件类型。
|
24
|
+
- 修改或编译文件内容。
|
25
|
+
- 对构建产物进行优化或处理。
|
6
26
|
|
7
|
-
Modern.js
|
27
|
+
- **Modern.js 框架插件:** 如果你的需求与 Modern.js 框架本身的功能扩展、运行时行为或服务端逻辑相关,那么你应该选择 Modern.js 插件。例如:
|
28
|
+
- 添加自定义的命令行命令。
|
29
|
+
- 修改应用的路由配置。
|
30
|
+
- 定制化应用的渲染过程(如 SSR)。
|
31
|
+
- 添加服务端中间件或处理函数。
|
32
|
+
|
33
|
+
简单来说,需要修改 Webpack/Rspack 配置时,使用 Rsbuild 插件;其他与框架相关的功能扩展,使用 Modern.js 插件。
|
34
|
+
|
35
|
+
## Modern.js 框架插件
|
8
36
|
|
9
37
|
### 插件类型
|
10
38
|
|
11
|
-
|
39
|
+
Modern.js 框架插件可以细分为三类,分别是:
|
40
|
+
|
41
|
+
#### CLI 插件
|
42
|
+
|
43
|
+
CLI 插件用于在应用执行 `modern` 命令时提供额外功能,例如添加命令、修改配置、监听文件等。大部分构建相关能力均可通过 CLI 插件实现。
|
44
|
+
|
45
|
+
CLI 插件可通过 `modern.config.ts` 中的 `plugins` 字段进行配置。
|
12
46
|
|
13
|
-
|
14
|
-
|
15
|
-
|
47
|
+
```ts title="modern.config.ts"
|
48
|
+
// an example for bff
|
49
|
+
import { appTools, defineConfig } from '@modern-js/app-tools';
|
50
|
+
import { bffPlugin } from '@modern-js/plugin-bff';
|
51
|
+
|
52
|
+
export default defineConfig({
|
53
|
+
plugins: [appTools(), bffPlugin()],
|
54
|
+
});
|
55
|
+
```
|
56
|
+
|
57
|
+
#### Runtime 插件
|
58
|
+
|
59
|
+
Runtime 插件用于在应用运行 React 代码时提供额外功能,例如执行初始化行为、实现 React 高阶组件(HOC)封装。
|
60
|
+
|
61
|
+
Runtime 插件通过 `src/modern.runtime.ts` 中的 `plugins` 字段配置。
|
62
|
+
|
63
|
+
```ts title="src/modern.runtime.ts"
|
64
|
+
import { defineRuntimeConfig } from '@modern-js/runtime';
|
65
|
+
import { routerPlugin } from '@modern-js/runtime/router';
|
66
|
+
|
67
|
+
export default defineRuntimeConfig({
|
68
|
+
plugins: [routerPlugin()],
|
69
|
+
});
|
70
|
+
```
|
71
|
+
|
72
|
+
{/* #### Server 插件
|
73
|
+
|
74
|
+
Server 插件用于在应用接受请求时提供额外功能,例如添加中间件、修改请求响应等。
|
75
|
+
|
76
|
+
Server 插件通过 `server/modern.server.ts` 中的 `plugins` 字段进行配置。
|
77
|
+
|
78
|
+
```ts title="src/modern.server.ts"
|
79
|
+
import { defineConfig } from '@modern-js/app-tools/server';
|
80
|
+
import bffPlugin from '@modern-js/plugin-bff/server';
|
81
|
+
|
82
|
+
export default defineConfig({
|
83
|
+
plugins: [bffPlugin()],
|
84
|
+
});
|
85
|
+
``` */}
|
86
|
+
|
87
|
+
:::tip
|
88
|
+
Modern.js 从 `MAJOR_VERSION.66.0` 起正式对外使用新的插件机制。
|
16
89
|
|
17
|
-
|
18
|
-
服务端插件和运行时插件即将正式对外开放。
|
90
|
+
如果你的当前版本低于 MAJOR_VERSION.66.0,可通过执行 `npx modern upgrade` 进行升级。
|
19
91
|
:::
|
20
92
|
|
21
93
|
### 开发插件
|
22
94
|
|
23
|
-
如果你需要开发 Modern.js
|
95
|
+
如果你需要开发 Modern.js 框架插件,请阅读 [Modern.js 插件系统](/plugin/plugin-system) 获取更多信息。
|
24
96
|
|
25
97
|
## Rsbuild 构建插件
|
26
98
|
|
27
|
-
Rsbuild 是 Modern.js
|
99
|
+
Rsbuild 是 Modern.js 底层的构建工具,通过添加 Rsbuild 插件可修改默认的构建行为,添加各类额外功能,包括但不限于:
|
28
100
|
|
29
101
|
- 修改 Rsbuild 配置
|
30
102
|
- 处理新的文件类型
|
31
103
|
- 修改或编译文件
|
32
104
|
- 部署产物
|
33
105
|
|
34
|
-
你可以在 `modern.config.ts` 中通过 `builderPlugins`
|
106
|
+
你可以在 `modern.config.ts` 中通过 `builderPlugins` 选项注册 Rsbuild 插件,详见 [builderPlugins 构建插件](/configure/app/builder-plugins.html)。
|
35
107
|
|
36
108
|
:::tip
|
37
|
-
Modern.js 从 `MAJOR_VERSION.46.0`
|
109
|
+
Modern.js 从 `MAJOR_VERSION.46.0` 起,底层构建工具升级为 [Rsbuild](https://rsbuild.dev/)。
|
38
110
|
|
39
|
-
|
111
|
+
如果你的当前版本低于 MAJOR_VERSION.46.0,可通过执行 `npx modern upgrade` 进行升级。
|
40
112
|
:::
|
41
113
|
|
42
114
|
:::info
|
43
|
-
可以阅读 [Rsbuild 官网 - 插件](https://rsbuild.dev/plugins/list/index)
|
115
|
+
可以阅读 [Rsbuild 官网 - 插件](https://rsbuild.dev/plugins/list/index) 了解更多 Rsbuild 插件体系内容。
|
44
116
|
:::
|
45
117
|
|
46
118
|
### 官方插件
|
47
119
|
|
48
120
|
#### 内置插件
|
49
121
|
|
50
|
-
|
122
|
+
以下为已在 Modern.js 中内置的 Rsbuild 官方插件,无需安装,即可启用:
|
51
123
|
|
52
124
|
| 插件 | 介绍 | Modern.js 链接 |
|
53
125
|
| -------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
54
126
|
| [React 插件](https://rsbuild.dev/zh/plugins/list/plugin-react) | 提供对 React 的支持 | - |
|
55
|
-
| [SVGR 插件](https://rsbuild.dev/zh/plugins/list/plugin-svgr) | 支持将 SVG 图片转换为一个 React
|
127
|
+
| [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) |
|
56
128
|
| [Styled Components 插件](https://github.com/rspack-contrib/rsbuild-plugin-styled-components) | 提供对 styled-components 的编译时支持 | [tools.styledComponents](/configure/app/tools/styled-components.html) |
|
57
129
|
| [Assets Retry 插件](https://rsbuild.dev/zh/plugins/list/plugin-assets-retry) | 用于在静态资源加载失败时自动发起重试请求 | [output.assetsRetry](/configure/app/output/assets-retry.html) |
|
58
|
-
| [Type Check 插件](https://github.com/rspack-contrib/rsbuild-plugin-type-check)
|
130
|
+
| [Type Check 插件](https://github.com/rspack-contrib/rsbuild-plugin-type-check) | 用于在单独的进程中运行 TypeScript 类型检查 | [output.disableTsChecker](/configure/app/output/disable-ts-checker.html)<br />[tools.tsChecker](/configure/app/tools/ts-checker.html) |
|
59
131
|
| [Node Polyfill 插件](https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill) | 用于注入 Node 核心模块在浏览器端的 polyfills | [output.disableNodePolyfill](/configure/app/output/disable-node-polyfill.html) |
|
60
132
|
| [Source Build 插件](https://github.com/rspack-contrib/rsbuild-plugin-source-build) | 用于 monorepo 场景,支持引用其他子目录的源代码,并完成构建和热更新 | [experiments.sourceBuild](/configure/app/experiments/source-build.html) |
|
61
133
|
| [Check Syntax 插件](https://github.com/rspack-contrib/rsbuild-plugin-check-syntax) | 用于分析产物的语法兼容性,判断是否存在导致兼容性问题的高级语法 | [security.checkSyntax](/configure/app/security/check-syntax.html) |
|
62
|
-
| [CSS Minimizer 插件](https://github.com/rspack-contrib/rsbuild-plugin-css-minimizer) | 用于自定义 CSS 压缩工具,切换到 [cssnano](https://cssnano.co/) 或其他工具进行 CSS
|
134
|
+
| [CSS Minimizer 插件](https://github.com/rspack-contrib/rsbuild-plugin-css-minimizer) | 用于自定义 CSS 压缩工具,切换到 [cssnano](https://cssnano.co/) 或其他工具进行 CSS 压缩 | [tools.minifyCss](/configure/app/tools/minify-css.html) |
|
63
135
|
| [Pug 插件](https://github.com/rspack-contrib/rsbuild-plugin-pug) | 提供对 Pug 模板引擎的支持 | [tools.pug](/configure/app/tools/pug.html) |
|
64
136
|
| [Rem 插件](https://github.com/rspack-contrib/rsbuild-plugin-rem) | 用于实现移动端页面的 rem 自适应布局 | [output.convertToRem](/configure/app/output/convert-to-rem.html) |
|
65
137
|
| [YAML 插件](https://github.com/rspack-contrib/rsbuild-plugin-yaml) | 用于引用 YAML 文件,并将其转换为 JavaScript 对象 | [YAML 文件](/guides/basic-features/static-assets/json-files.html#yaml-文件) |
|
@@ -0,0 +1 @@
|
|
1
|
+
["plugin-tailwind", "plugin-bff", "plugin-ssg", "plugin-swc"]
|
@@ -0,0 +1,6 @@
|
|
1
|
+
# 概览
|
2
|
+
|
3
|
+
- [@modern-js/plugin-tailwindcss](/plugin/official/cli-plugins/plugin-tailwind):提供使用 Tailwind CSS 样式的能力。
|
4
|
+
- [@modern-js/plugin-bff](/plugin/official/cli-plugins/plugin-bff):提供 BFF 服务,一体化调用的能力。
|
5
|
+
- [@modern-js/plugin-ssg](/plugin/official/cli-plugins/plugin-bff):提供静态站点生成的能力。
|
6
|
+
- [@modern-js/plugin-swc](/plugin/official/cli-plugins/plugin-swc):提供 SWC 编译支持。
|