@modern-js/main-doc 3.0.0-alpha.0 → 3.0.0-alpha.1
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/commands.mdx +6 -30
- package/docs/en/components/bff-upload.mdx +3 -5
- package/docs/en/components/bundler.mdx +1 -1
- package/docs/en/components/enable-bff.mdx +6 -2
- package/docs/en/components/enable-ssg.mdx +1 -0
- package/docs/en/components/esbuild.mdx +2 -2
- package/docs/en/components/extend-bff-function.mdx +2 -4
- package/docs/en/components/hono.mdx +119 -0
- package/docs/en/components/international/custom-instance-code.mdx +16 -0
- package/docs/en/components/international/init-options-desc.mdx +1 -0
- package/docs/en/components/international/install-command.mdx +15 -1
- package/docs/en/components/international/instance-code.mdx +26 -0
- package/docs/en/configure/app/builder-plugins.mdx +1 -2
- package/docs/en/configure/app/dev/server.mdx +108 -0
- package/docs/en/configure/app/experiments/source-build.mdx +0 -1
- package/docs/en/configure/app/output/assets-retry.mdx +1 -1
- package/docs/en/configure/app/output/disable-inline-runtime-chunk.mdx +2 -2
- package/docs/en/configure/app/output/filename.mdx +2 -4
- package/docs/en/configure/app/output/temp-dir.mdx +3 -3
- package/docs/en/configure/app/performance/build-cache.mdx +1 -1
- package/docs/en/configure/app/performance/profile.mdx +1 -1
- package/docs/en/configure/app/plugins.mdx +1 -3
- package/docs/en/configure/app/runtime/router.mdx +0 -4
- package/docs/en/configure/app/security/sri.mdx +0 -1
- package/docs/en/configure/app/source/alias.mdx +1 -1
- package/docs/en/configure/app/source/enable-async-entry.mdx +1 -1
- package/docs/en/configure/app/source/include.mdx +2 -14
- package/docs/en/configure/app/tools/dev-server.mdx +8 -8
- package/docs/en/configure/app/usage.mdx +0 -12
- package/docs/en/guides/_meta.json +5 -0
- package/docs/en/guides/advanced-features/bff/_meta.json +9 -1
- package/docs/en/guides/advanced-features/bff/cross-project.mdx +1 -1
- package/docs/en/guides/advanced-features/bff/frameworks.mdx +2 -15
- package/docs/en/guides/advanced-features/bff/function.mdx +4 -4
- package/docs/en/guides/advanced-features/bff/operators.mdx +628 -0
- package/docs/en/guides/advanced-features/bff/sdk.mdx +17 -9
- package/docs/en/guides/advanced-features/bff/upload.mdx +3 -1
- package/docs/en/guides/advanced-features/international/configuration.mdx +7 -16
- package/docs/en/guides/advanced-features/international/quick-start.mdx +4 -32
- package/docs/en/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
- package/docs/en/guides/advanced-features/page-performance/react-compiler.mdx +18 -4
- package/docs/en/guides/advanced-features/rspack-start.mdx +1 -1
- package/docs/en/guides/advanced-features/server-monitor/monitors.mdx +62 -5
- package/docs/en/guides/basic-features/data/data-cache.mdx +60 -76
- package/docs/en/guides/basic-features/data/data-fetch.mdx +15 -14
- package/docs/en/guides/basic-features/debug/proxy.mdx +6 -9
- package/docs/en/guides/basic-features/render/rsc.mdx +24 -19
- package/docs/en/guides/basic-features/render/ssg.mdx +4 -9
- package/docs/en/guides/basic-features/render/ssr-cache.mdx +0 -4
- package/docs/en/guides/basic-features/static-assets/svg-assets.mdx +0 -4
- package/docs/en/guides/get-started/tech-stack.mdx +1 -1
- package/docs/en/guides/upgrade/_meta.json +1 -0
- package/docs/en/guides/upgrade/config.mdx +936 -0
- package/docs/en/guides/upgrade/entry.mdx +463 -0
- package/docs/en/guides/upgrade/other.mdx +83 -0
- package/docs/en/guides/upgrade/overview.mdx +33 -0
- package/docs/en/guides/upgrade/tailwindcss.mdx +130 -0
- package/docs/en/guides/upgrade/web-server.mdx +91 -0
- package/docs/en/plugin/_meta.json +5 -0
- package/docs/en/plugin/cli-plugins/_meta.json +1 -1
- package/docs/en/plugin/cli-plugins/api.mdx +13 -63
- package/docs/en/plugin/cli-plugins/life-cycle.mdx +0 -4
- package/docs/en/plugin/introduction.mdx +8 -20
- package/docs/en/plugin/plugin-system.mdx +3 -3
- package/docs/en/plugin/runtime-plugins/_meta.json +1 -1
- package/docs/en/plugin/runtime-plugins/api.mdx +1 -1
- package/docs/en/plugin/server-plugins/_meta.json +1 -0
- package/docs/en/plugin/server-plugins/api.mdx +210 -1
- package/docs/en/plugin/server-plugins/life-cycle.mdx +41 -1
- package/docs/zh/apis/app/commands.mdx +6 -30
- package/docs/zh/components/bff-operator-code.mdx +5 -0
- package/docs/zh/components/bff-upload.mdx +0 -2
- package/docs/zh/components/bundler.mdx +1 -1
- package/docs/zh/components/enable-bff.mdx +6 -2
- package/docs/zh/components/enable-ssg.mdx +3 -1
- package/docs/zh/components/esbuild.mdx +2 -2
- package/docs/zh/components/extend-bff-function.mdx +2 -4
- package/docs/zh/components/hono.mdx +119 -0
- package/docs/zh/components/international/custom-instance-code.mdx +16 -0
- package/docs/zh/components/international/init-options-desc.mdx +1 -0
- package/docs/zh/components/international/install-command.mdx +15 -0
- package/docs/zh/components/international/instance-code.mdx +26 -0
- package/docs/zh/configure/app/builder-plugins.mdx +1 -2
- package/docs/zh/configure/app/dev/server.mdx +109 -2
- package/docs/zh/configure/app/experiments/source-build.mdx +0 -1
- package/docs/zh/configure/app/output/assets-retry.mdx +1 -1
- package/docs/zh/configure/app/output/disable-inline-runtime-chunk.mdx +2 -2
- package/docs/zh/configure/app/output/filename.mdx +2 -4
- package/docs/zh/configure/app/output/temp-dir.mdx +3 -3
- package/docs/zh/configure/app/performance/build-cache.mdx +1 -1
- package/docs/zh/configure/app/performance/profile.mdx +1 -1
- package/docs/zh/configure/app/plugins.mdx +1 -2
- package/docs/zh/configure/app/runtime/router.mdx +0 -4
- package/docs/zh/configure/app/security/sri.mdx +0 -1
- package/docs/zh/configure/app/source/alias.mdx +1 -1
- package/docs/zh/configure/app/source/enable-async-entry.mdx +1 -1
- package/docs/zh/configure/app/source/include.mdx +2 -16
- package/docs/zh/configure/app/tools/dev-server.mdx +5 -5
- package/docs/zh/configure/app/usage.mdx +0 -12
- package/docs/zh/guides/advanced-features/bff/_meta.json +9 -1
- package/docs/zh/guides/advanced-features/bff/frameworks.mdx +2 -16
- package/docs/zh/guides/advanced-features/bff/operators.mdx +628 -0
- package/docs/zh/guides/advanced-features/bff/sdk.mdx +19 -12
- package/docs/zh/guides/advanced-features/bff/upload.mdx +3 -1
- package/docs/zh/guides/advanced-features/international/configuration.mdx +7 -16
- package/docs/zh/guides/advanced-features/international/quick-start.mdx +2 -25
- package/docs/zh/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
- package/docs/zh/guides/advanced-features/page-performance/react-compiler.mdx +18 -4
- package/docs/zh/guides/advanced-features/server-monitor/monitors.mdx +60 -5
- package/docs/zh/guides/basic-features/data/data-cache.mdx +47 -54
- package/docs/zh/guides/basic-features/data/data-fetch.mdx +9 -12
- package/docs/zh/guides/basic-features/debug/proxy.mdx +4 -7
- package/docs/zh/guides/basic-features/render/rsc.mdx +23 -37
- package/docs/zh/guides/basic-features/render/ssr-cache.mdx +0 -4
- package/docs/zh/guides/basic-features/static-assets/svg-assets.mdx +0 -4
- package/docs/zh/guides/get-started/tech-stack.mdx +1 -1
- package/docs/zh/guides/troubleshooting/builder.mdx +1 -1
- package/docs/zh/guides/upgrade/config.mdx +132 -1
- package/docs/zh/plugin/_meta.json +5 -0
- package/docs/zh/plugin/cli-plugins/_meta.json +1 -1
- package/docs/zh/plugin/cli-plugins/api.mdx +15 -65
- package/docs/zh/plugin/cli-plugins/life-cycle.mdx +0 -4
- package/docs/zh/plugin/introduction.mdx +4 -16
- package/docs/zh/plugin/plugin-system.mdx +3 -14
- package/docs/zh/plugin/runtime-plugins/_meta.json +1 -1
- package/docs/zh/plugin/runtime-plugins/api.mdx +1 -1
- package/docs/zh/plugin/server-plugins/_meta.json +1 -0
- package/docs/zh/plugin/server-plugins/api.mdx +210 -1
- package/docs/zh/plugin/server-plugins/life-cycle.mdx +41 -1
- package/package.json +2 -2
- package/src/components/FrameworkCode/index.tsx +605 -0
- package/docs/en/configure/app/performance/bundle-analyze.mdx +0 -24
- package/docs/en/configure/app/tools/babel.mdx +0 -225
- package/docs/en/plugin/cli-plugins/migration.mdx +0 -83
- package/docs/en/plugin/runtime-plugins/migration.mdx +0 -110
- package/docs/zh/components/default-mwa-generate.mdx +0 -4
- package/docs/zh/configure/app/performance/bundle-analyze.mdx +0 -24
- package/docs/zh/configure/app/tools/babel.mdx +0 -224
- package/docs/zh/plugin/cli-plugins/migration.mdx +0 -83
- package/docs/zh/plugin/runtime-plugins/migration.mdx +0 -110
- /package/docs/en/components/{router-legacy-tip.mdx → upgrade-config-deploy.mdx} +0 -0
- /package/docs/zh/components/{router-legacy-tip.mdx → upgrade-config-deploy.mdx} +0 -0
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: babel
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
# tools.babel
|
|
6
|
-
|
|
7
|
-
- **Type:** `Object | Function`
|
|
8
|
-
- **Default:** `undefined`
|
|
9
|
-
|
|
10
|
-
With `tools.babel` you can modify the options of [babel-loader](https://github.com/babel/babel-loader).
|
|
11
|
-
|
|
12
|
-
### Usage Scenarios
|
|
13
|
-
|
|
14
|
-
Please note the limitations of `tools.babel` in the following usage scenarios:
|
|
15
|
-
|
|
16
|
-
- Rspack scenario: When using Rspack as the bundler, using the `tools.babel` option will significantly slow down the Rspack's build speed. This is because Rspack defaults to using SWC for compilation, and configuring Babel will cause the code to be compiled twice, resulting in additional compilation overhead.
|
|
17
|
-
- webpack + SWC scenario: When using webpack as the bundler, if you use SWC plugin for code compilation, the `tools.babel` option will not take effect.
|
|
18
|
-
|
|
19
|
-
### Function Type
|
|
20
|
-
|
|
21
|
-
When `tools.babel` is of type `Function`, the default Babel configuration will be passed as the first parameter. You can directly modify the configuration object or return an object as the final `babel-loader` configuration.
|
|
22
|
-
|
|
23
|
-
```js
|
|
24
|
-
export default {
|
|
25
|
-
tools: {
|
|
26
|
-
babel(config) {
|
|
27
|
-
// Add a Babel plugin
|
|
28
|
-
// note: the plugin have been added to the default config to support antd load on demand
|
|
29
|
-
config.plugins.push([
|
|
30
|
-
'babel-plugin-import',
|
|
31
|
-
{
|
|
32
|
-
libraryName: 'xxx-components',
|
|
33
|
-
libraryDirectory: 'es',
|
|
34
|
-
style: true,
|
|
35
|
-
},
|
|
36
|
-
]);
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
The second parameter of the `tools.babel` function provides some more convenient utility functions. Please continue reading the documentation below.
|
|
43
|
-
|
|
44
|
-
:::tip
|
|
45
|
-
The above example is just for reference, usually you don't need to manually configure `babel-plugin-import`, because Modern.js already provides a more general `source.transformImport` configuration.
|
|
46
|
-
:::
|
|
47
|
-
|
|
48
|
-
### Object Type
|
|
49
|
-
|
|
50
|
-
When `tools.babel`'s type is `Object`, the config will be shallow merged with default config by `Object.assign`.
|
|
51
|
-
|
|
52
|
-
:::caution
|
|
53
|
-
Note that `Object.assign` is a shallow copy and will completely overwrite the built-in `presets` or `plugins` array, please use it with caution.
|
|
54
|
-
:::
|
|
55
|
-
|
|
56
|
-
```js
|
|
57
|
-
export default {
|
|
58
|
-
tools: {
|
|
59
|
-
babel: {
|
|
60
|
-
plugins: [
|
|
61
|
-
[
|
|
62
|
-
'babel-plugin-import',
|
|
63
|
-
{
|
|
64
|
-
libraryName: 'xxx-components',
|
|
65
|
-
libraryDirectory: 'es',
|
|
66
|
-
style: true,
|
|
67
|
-
},
|
|
68
|
-
],
|
|
69
|
-
],
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Util Functions
|
|
76
|
-
|
|
77
|
-
When `tools.babel` is a Function, the tool functions available for the second parameter are as follows:
|
|
78
|
-
|
|
79
|
-
#### addPlugins
|
|
80
|
-
|
|
81
|
-
- **Type:** `(plugins: BabelPlugin[]) => void`
|
|
82
|
-
|
|
83
|
-
Add some Babel plugins. For example:
|
|
84
|
-
|
|
85
|
-
```js
|
|
86
|
-
export default {
|
|
87
|
-
tools: {
|
|
88
|
-
babel(config, { addPlugins }) {
|
|
89
|
-
addPlugins([
|
|
90
|
-
[
|
|
91
|
-
'babel-plugin-import',
|
|
92
|
-
{
|
|
93
|
-
libraryName: 'xxx-components',
|
|
94
|
-
libraryDirectory: 'es',
|
|
95
|
-
style: true,
|
|
96
|
-
},
|
|
97
|
-
],
|
|
98
|
-
]);
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
};
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
#### addPresets
|
|
105
|
-
|
|
106
|
-
- **Type:** `(presets: BabelPlugin[]) => void`
|
|
107
|
-
|
|
108
|
-
Add Babel preset configuration. (No need to add presets in most cases)
|
|
109
|
-
|
|
110
|
-
```js
|
|
111
|
-
export default {
|
|
112
|
-
tools: {
|
|
113
|
-
babel(config, { addPresets }) {
|
|
114
|
-
addPresets(['@babel/preset-env']);
|
|
115
|
-
},
|
|
116
|
-
},
|
|
117
|
-
};
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
#### removePlugins
|
|
121
|
-
|
|
122
|
-
- **Type:** `(plugins: string | string[]) => void`
|
|
123
|
-
|
|
124
|
-
To remove the Babel plugin, just pass in the name of the plugin to be removed, you can pass in a single string or an array of strings.
|
|
125
|
-
|
|
126
|
-
```js
|
|
127
|
-
export default {
|
|
128
|
-
tools: {
|
|
129
|
-
babel(config, { removePlugins }) {
|
|
130
|
-
removePlugins('babel-plugin-import');
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
#### removePresets
|
|
137
|
-
|
|
138
|
-
- **Type:** `(presets: string | string[]) => void`
|
|
139
|
-
|
|
140
|
-
To remove the Babel preset configuration, pass in the name of the preset to be removed, you can pass in a single string or an array of strings.
|
|
141
|
-
|
|
142
|
-
```js
|
|
143
|
-
export default {
|
|
144
|
-
tools: {
|
|
145
|
-
babel(config, { removePresets }) {
|
|
146
|
-
removePresets('@babel/preset-env');
|
|
147
|
-
},
|
|
148
|
-
},
|
|
149
|
-
};
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
#### modifyPresetEnvOptions
|
|
153
|
-
|
|
154
|
-
- **Type:** `(options: PresetEnvOptions) => void`
|
|
155
|
-
|
|
156
|
-
Modify the configuration of [@babel/preset-env](https://babeljs.io/docs/en/babel-preset-env), the configuration you pass in will be shallowly merged with default config. For example:
|
|
157
|
-
|
|
158
|
-
```js
|
|
159
|
-
export default {
|
|
160
|
-
tools: {
|
|
161
|
-
babel(config, { modifyPresetEnvOptions }) {
|
|
162
|
-
modifyPresetEnvOptions({
|
|
163
|
-
targets: 'last 2 versions',
|
|
164
|
-
});
|
|
165
|
-
},
|
|
166
|
-
},
|
|
167
|
-
};
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
```js
|
|
171
|
-
export default {
|
|
172
|
-
tools: {
|
|
173
|
-
babel(config, { modifyPresetEnvOptions }) {
|
|
174
|
-
modifyPresetEnvOptions({
|
|
175
|
-
targets: {
|
|
176
|
-
chrome: '58',
|
|
177
|
-
ie: '11',
|
|
178
|
-
},
|
|
179
|
-
});
|
|
180
|
-
},
|
|
181
|
-
},
|
|
182
|
-
};
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
#### modifyPresetReactOptions
|
|
186
|
-
|
|
187
|
-
- **Type:** `(options: PresetReactOptions) => void`
|
|
188
|
-
|
|
189
|
-
Modify the configuration of [@babel/preset-react](https://babeljs.io/docs/en/babel-preset-react), the configuration you pass in will be shallowly merged with default config. For example:
|
|
190
|
-
|
|
191
|
-
```js
|
|
192
|
-
export default {
|
|
193
|
-
tools: {
|
|
194
|
-
babel(config, { modifyPresetReactOptions }) {
|
|
195
|
-
modifyPresetReactOptions({
|
|
196
|
-
pragma: 'React.createElement',
|
|
197
|
-
});
|
|
198
|
-
},
|
|
199
|
-
},
|
|
200
|
-
};
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
#### addIncludes
|
|
204
|
-
|
|
205
|
-
Deprecated, please use [source.include](https://modernjs.dev/en/configure/app/source/include.html) instead, both have the same functionality.
|
|
206
|
-
|
|
207
|
-
#### addExcludes
|
|
208
|
-
|
|
209
|
-
Deprecated, please use [source.exclude](https://modernjs.dev/en/configure/app/source/exclude.html) instead, both have the same functionality.
|
|
210
|
-
|
|
211
|
-
### Debugging Babel Configuration
|
|
212
|
-
|
|
213
|
-
After modifying the `babel-loader` configuration through `tools.babel`, you can view the final generated configuration in [Rsbuild debug mode](https://rsbuild.rs/guide/debug/debug-mode).
|
|
214
|
-
|
|
215
|
-
First, enable debug mode by using the `DEBUG=builder` parameter:
|
|
216
|
-
|
|
217
|
-
```bash
|
|
218
|
-
# Debug development mode
|
|
219
|
-
DEBUG=builder pnpm dev
|
|
220
|
-
|
|
221
|
-
# Debug production mode
|
|
222
|
-
DEBUG=builder pnpm build
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
Then open the generated `(webpack|rspack).config.web.js` file and search for the `babel-loader` keyword to see the complete `babel-loader` configuration.
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
# Plugin Migration
|
|
2
|
-
|
|
3
|
-
### Migration Background
|
|
4
|
-
|
|
5
|
-
The Modern.js plugin system is constantly evolving. To provide a clearer API and more powerful features, we've optimized the definition and usage of CLI plugins. While the old plugin syntax is still supported for compatibility, we strongly recommend migrating according to this guide to take full advantage of the new plugin system.
|
|
6
|
-
|
|
7
|
-
### Migration Steps Overview
|
|
8
|
-
|
|
9
|
-
1. **Adjust Hook Invocation:** Migrate from the `return hooks` pattern to direct `api.xxx` calls.
|
|
10
|
-
2. **Replace Changed APIs:** Refer to the detailed API mapping table and update your code.
|
|
11
|
-
|
|
12
|
-
### Detailed Migration Steps
|
|
13
|
-
|
|
14
|
-
#### Adjust Hook Invocation
|
|
15
|
-
|
|
16
|
-
The new plugin system recommends using the `api` object to directly call Hooks. This approach is more intuitive and easier to maintain.
|
|
17
|
-
|
|
18
|
-
```typescript
|
|
19
|
-
// Old Syntax (return hooks)
|
|
20
|
-
{
|
|
21
|
-
setup: () => ({
|
|
22
|
-
commands({ program }) {
|
|
23
|
-
/*...*/
|
|
24
|
-
},
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// New Syntax (api.xxx)
|
|
29
|
-
{
|
|
30
|
-
setup: api => {
|
|
31
|
-
api.addCommand(({ program }) => {
|
|
32
|
-
/*...*/
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
**Explanation:** The `api` object provides all available Hooks and utility methods.
|
|
39
|
-
|
|
40
|
-
#### Replace Changed APIs
|
|
41
|
-
|
|
42
|
-
To maintain API consistency and clarity, we've adjusted the names of some APIs. The following table lists all changed APIs and their old and new counterparts:
|
|
43
|
-
|
|
44
|
-
| Old API | New API | Description |
|
|
45
|
-
| :------------------------- | :---------------------------- | :-------------------------------------------------------------------------------------------------------------------------- |
|
|
46
|
-
| `useAppContext` | `getAppContext` | Gets the application context information. |
|
|
47
|
-
| `useConfigContext` | `getConfig` | Gets the configuration defined in the user's configuration file. |
|
|
48
|
-
| `useResolvedConfigContext` | `getNormalizedConfig` | Gets the final configuration after processing by plugins. |
|
|
49
|
-
| `beforeConfig` | (Defined directly in `setup`) | The `beforeConfig` Hook is no longer needed. Write related logic directly in the `setup` function. |
|
|
50
|
-
| `prepare` | `onPrepare` | The preparation stage before running the main process. |
|
|
51
|
-
| `afterPrepare` | `onAfterPrepare` (Deprecated) | Executes after `onPrepare`, but may be deprecated in future versions. It's recommended to merge the logic into `onPrepare`. |
|
|
52
|
-
| `beforePrintInstructions` | `onBeforePrintInstructions` | Executes before printing log messages. |
|
|
53
|
-
| `commands` | `addCommand` | Adds a new CLI command. |
|
|
54
|
-
| `watchFiles` | `addWatchFiles` | Adds files to be watched. |
|
|
55
|
-
| `fileChange` | `onFileChanged` | Listens for file change events. |
|
|
56
|
-
| `beforeCreateCompiler` | `onBeforeCreateCompiler` | Executes before creating the compiler. |
|
|
57
|
-
| `afterCreateCompiler` | `onAfterCreateCompiler` | Executes after creating the compiler. |
|
|
58
|
-
| `beforeBuild` | `onBeforeBuild` | Executes before building. |
|
|
59
|
-
| `afterBuild` | `onAfterBuild` | Executes after building. |
|
|
60
|
-
| `beforeDev` | `onBeforeDev` | Executes before running the `dev` command. |
|
|
61
|
-
| `afterDev` | `onDevCompileDone` | Executes after the `dev` command compilation is complete. |
|
|
62
|
-
| `beforeExit` | `onBeforeExit` | Executes before the process exits. |
|
|
63
|
-
| `htmlPartials` | `modifyHtmlPartials` | Modifies HTML template partials. |
|
|
64
|
-
|
|
65
|
-
**Explanation:** Please carefully review your code and ensure that all old APIs have been replaced with the new APIs.
|
|
66
|
-
|
|
67
|
-
### Frequently Asked Questions
|
|
68
|
-
|
|
69
|
-
**Q: Will my plugin still work after the migration?**
|
|
70
|
-
|
|
71
|
-
A: As long as you have correctly completed all the steps in this guide, your plugin should work normally. If you encounter any problems, please refer to the official Modern.js documentation or seek community support.
|
|
72
|
-
|
|
73
|
-
**Q: Do I have to migrate my plugin immediately?**
|
|
74
|
-
|
|
75
|
-
A: While the old plugin syntax is still supported, we strongly recommend migrating as soon as possible. The new plugin system offers better performance and richer features, and migration is worthwhile in the long run.
|
|
76
|
-
|
|
77
|
-
**Q: Where can I find more information about the new plugin system?**
|
|
78
|
-
|
|
79
|
-
A: Please refer to the official Modern.js documentation, especially the section on the plugin system. You can also refer to examples of other migrated plugins to understand best practices.
|
|
80
|
-
|
|
81
|
-
### Summary
|
|
82
|
-
|
|
83
|
-
With this detailed migration guide, we hope to help you smoothly migrate your CLI plugins to the new Modern.js plugin system. If you encounter any problems during the migration, please feel free to ask us for help.
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
# Plugin Migration
|
|
2
|
-
|
|
3
|
-
### Migration Background
|
|
4
|
-
|
|
5
|
-
The Modern.js plugin system is constantly evolving. To provide a clearer API and more powerful features, we've optimized the definition and usage of Runtime plugins. While the old plugin syntax is still partially compatible, we strongly recommend migrating according to this guide to take full advantage of the new plugin system.
|
|
6
|
-
|
|
7
|
-
### Migration Steps Overview
|
|
8
|
-
|
|
9
|
-
1. **Update Plugin Type Definition:** Replace the `Plugin` type with `RuntimePlugin`.
|
|
10
|
-
2. **Adjust Hook Invocation:** Migrate from the `return hooks` pattern to direct `api.xxx` calls.
|
|
11
|
-
3. **Replace Changed APIs:** Refer to the detailed API mapping table and update your code.
|
|
12
|
-
|
|
13
|
-
### Detailed Migration Steps
|
|
14
|
-
|
|
15
|
-
#### Update Plugin Type Definition
|
|
16
|
-
|
|
17
|
-
This is the first and most crucial step. It ensures that your plugin interacts correctly with the new plugin system.
|
|
18
|
-
|
|
19
|
-
```typescript
|
|
20
|
-
// Old Syntax
|
|
21
|
-
import type { Plugin } from '@modern-js/runtime';
|
|
22
|
-
|
|
23
|
-
const plugin: Plugin = { ... };
|
|
24
|
-
|
|
25
|
-
// New Syntax
|
|
26
|
-
import type { RuntimePlugin } from '@modern-js/runtime';
|
|
27
|
-
|
|
28
|
-
const plugin: RuntimePlugin = { ... };
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
**Explanation:** The `RuntimePlugin` type is the standard definition for new plugins. It provides better type inference and a clearer API structure.
|
|
32
|
-
|
|
33
|
-
#### Adjust Hook Invocation
|
|
34
|
-
|
|
35
|
-
The new plugin system recommends using the `api` object to directly call Hooks. This approach is more intuitive and easier to maintain.
|
|
36
|
-
|
|
37
|
-
```typescript
|
|
38
|
-
// Old Syntax (return hooks)
|
|
39
|
-
{
|
|
40
|
-
setup: () => ({
|
|
41
|
-
beforeRender({ req, res }) {
|
|
42
|
-
/*...*/
|
|
43
|
-
},
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// New Syntax (api.xxx)
|
|
48
|
-
{
|
|
49
|
-
setup: api => {
|
|
50
|
-
api.onBeforeRender(({ req, res }) => {
|
|
51
|
-
/*...*/
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
**Explanation:** The `api` object provides all available Hooks and utility methods.
|
|
58
|
-
|
|
59
|
-
#### Replace Changed APIs
|
|
60
|
-
|
|
61
|
-
To maintain API consistency and clarity, we've adjusted the names of some APIs. Additionally, the `hoc` and `init` Hooks have been removed. Please use the new Hooks as replacements. The table below lists all changed APIs and their old and new counterparts:
|
|
62
|
-
|
|
63
|
-
| Old API | New API | Description |
|
|
64
|
-
| :------------- | :--------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
65
|
-
| `beforeRender` | `onBeforeRender` | Executed before application rendering. |
|
|
66
|
-
| `hoc` | `wrapRoot` | **Important Change:** Used to wrap the root component, achieving the functionality of a Higher-Order Component (HOC). Make sure to pass props to the original component. |
|
|
67
|
-
| `init` | `onBeforeRender` | **Important Change:** Execute initialization logic before application rendering. This replaces the previous `init` hook. Use `onBeforeRender` to perform any setup or initialization tasks that were previously done in `init`. This provides a single, consistent point for pre-rendering logic. |
|
|
68
|
-
|
|
69
|
-
**Explanation:**
|
|
70
|
-
|
|
71
|
-
- `onBeforeRender` replaces both the original `beforeRender` and `init`, used for pre-rendering logic and initialization, respectively.
|
|
72
|
-
- `wrapRoot` replaces `hoc` and is used to implement higher-order component functionality. It's crucial to pass props correctly when using `wrapRoot`.
|
|
73
|
-
|
|
74
|
-
**`wrapRoot` Usage Example:**
|
|
75
|
-
|
|
76
|
-
```typescript
|
|
77
|
-
{
|
|
78
|
-
setup: api => {
|
|
79
|
-
api.wrapRoot(App => {
|
|
80
|
-
const AppWrapper = props => {
|
|
81
|
-
// Ensure props are passed to the original component
|
|
82
|
-
return (
|
|
83
|
-
<Provider value={xx}>
|
|
84
|
-
<App {...props} />
|
|
85
|
-
</Provider>
|
|
86
|
-
);
|
|
87
|
-
};
|
|
88
|
-
return AppWrapper;
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Frequently Asked Questions
|
|
95
|
-
|
|
96
|
-
**Q: Will my plugin still work correctly after migration?**
|
|
97
|
-
|
|
98
|
-
A: As long as you've correctly completed all the steps in this guide, your plugin should function properly. If you encounter any issues, please refer to the official Modern.js documentation or seek community support.
|
|
99
|
-
|
|
100
|
-
**Q: Do I have to migrate my plugin immediately?**
|
|
101
|
-
|
|
102
|
-
A: While the old plugin syntax remains partially compatible, we strongly recommend migrating as soon as possible. The new plugin system offers improved performance and richer functionality, making migration worthwhile in the long run.
|
|
103
|
-
|
|
104
|
-
**Q: Where can I find more information about the new plugin system?**
|
|
105
|
-
|
|
106
|
-
A: Please consult the official Modern.js documentation, especially the section on the plugin system. You can also refer to examples of other migrated plugins to understand best practices.
|
|
107
|
-
|
|
108
|
-
### Summary
|
|
109
|
-
|
|
110
|
-
With this detailed migration guide, we hope to help you smoothly transition your Runtime plugins to the new Modern.js plugin system. If you encounter any problems during the migration process, please don't hesitate to ask for assistance.
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: bundleAnalyze
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
# performance.bundleAnalyze
|
|
6
|
-
|
|
7
|
-
- **类型:** `Object | undefined`
|
|
8
|
-
|
|
9
|
-
用于开启 [webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) 插件来分析产物体积。
|
|
10
|
-
|
|
11
|
-
默认情况下,Modern.js 不会开启 `webpack-bundle-analyzer`。当开启该功能后,内部的默认配置如下:
|
|
12
|
-
|
|
13
|
-
```js
|
|
14
|
-
const defaultConfig = {
|
|
15
|
-
analyzerMode: 'static',
|
|
16
|
-
openAnalyzer: false,
|
|
17
|
-
// target 为编译目标,如 `web`、`node` 等
|
|
18
|
-
reportFilename: `report-${target}.html`,
|
|
19
|
-
};
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip';
|
|
23
|
-
|
|
24
|
-
<RsbuildConfig configName="performance.bundleAnalyze" />
|
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: babel
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
# tools.babel
|
|
6
|
-
|
|
7
|
-
- **类型:** `Object | Function`
|
|
8
|
-
- **默认值:** `undefined`
|
|
9
|
-
|
|
10
|
-
通过 `tools.babel` 可以修改 [babel-loader](https://github.com/babel/babel-loader) 的配置项。
|
|
11
|
-
|
|
12
|
-
### 使用场景
|
|
13
|
-
|
|
14
|
-
请留意 `tools.babel` 在以下使用场景中的局限性:
|
|
15
|
-
|
|
16
|
-
- Rspack 场景:在使用 Rspack 作为打包工具时,使用 `tools.babel` 配置项将会明显拖慢 Rspack 构建速度。因为 Rspack 默认使用的是 SWC 编译,配置 Babel 会导致代码需要被编译两次,产生了额外的编译开销。
|
|
17
|
-
- webpack + SWC 场景:在使用 webpack 作为打包工具时,如果你使用了 SWC 插件进行代码编译,那么 `tools.babel` 选项将不会生效。
|
|
18
|
-
|
|
19
|
-
### Function 类型
|
|
20
|
-
|
|
21
|
-
当 `tools.babel` 为 Function 类型时,默认 Babel 配置会作为第一个参数传入,你可以直接修改配置对象,也可以返回一个对象作为最终的 `babel-loader` 配置。
|
|
22
|
-
|
|
23
|
-
```js
|
|
24
|
-
export default {
|
|
25
|
-
tools: {
|
|
26
|
-
babel(config) {
|
|
27
|
-
// 添加一个插件,比如配置某个组件库的按需引入
|
|
28
|
-
config.plugins.push([
|
|
29
|
-
'babel-plugin-import',
|
|
30
|
-
{
|
|
31
|
-
libraryName: 'xxx-components',
|
|
32
|
-
libraryDirectory: 'es',
|
|
33
|
-
style: true,
|
|
34
|
-
},
|
|
35
|
-
]);
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
`tools.babel` 函数的第二个参数提供了一些方便的工具函数,请继续阅读下方文档。
|
|
42
|
-
|
|
43
|
-
:::tip
|
|
44
|
-
以上示例仅作为参考,通常来说,你不需要手动配置 `babel-plugin-import`,因为 Modern.js 已经提供了更通用的 `source.transformImport` 配置。
|
|
45
|
-
:::
|
|
46
|
-
|
|
47
|
-
### Object 类型
|
|
48
|
-
|
|
49
|
-
当 `tools.babel` 的值为 `Object` 类型时,会与默认配置通过 `Object.assign` 浅合并。
|
|
50
|
-
|
|
51
|
-
:::caution
|
|
52
|
-
`Object.assign` 是浅拷贝,会完全覆盖内置的 `presets` 或 `plugins` 数组,导致内置的 presets 或 plugins 失效,请在明确影响面的情况下再使用这种方式。
|
|
53
|
-
:::
|
|
54
|
-
|
|
55
|
-
```js
|
|
56
|
-
export default {
|
|
57
|
-
tools: {
|
|
58
|
-
babel: {
|
|
59
|
-
plugins: [
|
|
60
|
-
[
|
|
61
|
-
'babel-plugin-import',
|
|
62
|
-
{
|
|
63
|
-
libraryName: 'xxx-components',
|
|
64
|
-
libraryDirectory: 'es',
|
|
65
|
-
style: true,
|
|
66
|
-
},
|
|
67
|
-
],
|
|
68
|
-
],
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### 工具函数
|
|
75
|
-
|
|
76
|
-
`tools.babel` 为 Function 类型时,第二个参数可用的工具函数如下:
|
|
77
|
-
|
|
78
|
-
#### addPlugins
|
|
79
|
-
|
|
80
|
-
- **类型:** `(plugins: BabelPlugin[]) => void`
|
|
81
|
-
|
|
82
|
-
添加若干个 Babel 插件。
|
|
83
|
-
|
|
84
|
-
```js
|
|
85
|
-
export default {
|
|
86
|
-
tools: {
|
|
87
|
-
babel(config, { addPlugins }) {
|
|
88
|
-
addPlugins([
|
|
89
|
-
[
|
|
90
|
-
'babel-plugin-import',
|
|
91
|
-
{
|
|
92
|
-
libraryName: 'xxx-components',
|
|
93
|
-
libraryDirectory: 'es',
|
|
94
|
-
style: true,
|
|
95
|
-
},
|
|
96
|
-
],
|
|
97
|
-
]);
|
|
98
|
-
},
|
|
99
|
-
},
|
|
100
|
-
};
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
#### addPresets
|
|
104
|
-
|
|
105
|
-
- **类型:** `(presets: BabelPlugin[]) => void`
|
|
106
|
-
|
|
107
|
-
添加若干个 Babel 预设配置 (大多数情况下不需要增加预设)。
|
|
108
|
-
|
|
109
|
-
```js
|
|
110
|
-
export default {
|
|
111
|
-
tools: {
|
|
112
|
-
babel(config, { addPresets }) {
|
|
113
|
-
addPresets(['@babel/preset-env']);
|
|
114
|
-
},
|
|
115
|
-
},
|
|
116
|
-
};
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
#### removePlugins
|
|
120
|
-
|
|
121
|
-
- **类型:** `(plugins: string | string[]) => void`
|
|
122
|
-
|
|
123
|
-
移除 Babel 插件,传入需要移除的插件名称即可,你可以传入单个字符串,也可以传入一个字符串数组。
|
|
124
|
-
|
|
125
|
-
```js
|
|
126
|
-
export default {
|
|
127
|
-
tools: {
|
|
128
|
-
babel(config, { removePlugins }) {
|
|
129
|
-
removePlugins('babel-plugin-import');
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
};
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
#### removePresets
|
|
136
|
-
|
|
137
|
-
- **类型:** `(presets: string | string[]) => void`
|
|
138
|
-
|
|
139
|
-
移除 Babel 预设配置,传入需要移除的预设名称即可,你可以传入单个字符串,也可以传入一个字符串数组。
|
|
140
|
-
|
|
141
|
-
```js
|
|
142
|
-
export default {
|
|
143
|
-
tools: {
|
|
144
|
-
babel(config, { removePresets }) {
|
|
145
|
-
removePresets('@babel/preset-env');
|
|
146
|
-
},
|
|
147
|
-
},
|
|
148
|
-
};
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
#### modifyPresetEnvOptions
|
|
152
|
-
|
|
153
|
-
- **类型:** `(options: PresetEnvOptions) => void`
|
|
154
|
-
|
|
155
|
-
修改 [@babel/preset-env](https://babeljs.io/docs/en/babel-preset-env) 的配置项,传入的配置会与默认配置进行浅层合并,比如:
|
|
156
|
-
|
|
157
|
-
```js
|
|
158
|
-
export default {
|
|
159
|
-
tools: {
|
|
160
|
-
babel(config, { modifyPresetEnvOptions }) {
|
|
161
|
-
modifyPresetEnvOptions({
|
|
162
|
-
targets: 'last 2 versions',
|
|
163
|
-
});
|
|
164
|
-
},
|
|
165
|
-
},
|
|
166
|
-
};
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
```js
|
|
170
|
-
export default {
|
|
171
|
-
tools: {
|
|
172
|
-
babel(config, { modifyPresetEnvOptions }) {
|
|
173
|
-
modifyPresetEnvOptions({
|
|
174
|
-
targets: {
|
|
175
|
-
chrome: '58',
|
|
176
|
-
ie: '11',
|
|
177
|
-
},
|
|
178
|
-
});
|
|
179
|
-
},
|
|
180
|
-
},
|
|
181
|
-
};
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
#### modifyPresetReactOptions
|
|
185
|
-
|
|
186
|
-
- **类型:** `(options: PresetReactOptions) => void`
|
|
187
|
-
|
|
188
|
-
修改 [@babel/preset-react](https://babeljs.io/docs/en/babel-preset-react) 的配置项,传入的配置会与默认配置进行浅层合并,比如:
|
|
189
|
-
|
|
190
|
-
```js
|
|
191
|
-
export default {
|
|
192
|
-
tools: {
|
|
193
|
-
babel(config, { modifyPresetReactOptions }) {
|
|
194
|
-
modifyPresetReactOptions({
|
|
195
|
-
pragma: 'React.createElement',
|
|
196
|
-
});
|
|
197
|
-
},
|
|
198
|
-
},
|
|
199
|
-
};
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
#### addIncludes
|
|
203
|
-
|
|
204
|
-
已废弃,请使用 [source.include](https://modernjs.dev/configure/app/source/include.html) 代替,两者功能完全一致。
|
|
205
|
-
|
|
206
|
-
#### addExcludes
|
|
207
|
-
|
|
208
|
-
已废弃,请使用 [source.exclude](https://modernjs.dev/configure/app/source/exclude.html) 代替,两者功能完全一致。
|
|
209
|
-
|
|
210
|
-
### 调试 Babel 配置
|
|
211
|
-
|
|
212
|
-
当你通过 `tools.babel` 修改 `babel-loader` 配置后,可以在 [调试模式](https://rsbuild.rs/zh/guide/debug/debug-mode) 下查看最终生成的配置。
|
|
213
|
-
|
|
214
|
-
首先通过 `DEBUG=builder` 参数开启调试模式:
|
|
215
|
-
|
|
216
|
-
```bash
|
|
217
|
-
# 调试开发环境
|
|
218
|
-
DEBUG=builder pnpm dev
|
|
219
|
-
|
|
220
|
-
# 调试生产环境
|
|
221
|
-
DEBUG=builder pnpm build
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
然后打开生成的 `(webpack|rspack).config.web.js`,搜索 `babel-loader` 关键词,即可看到完整的 `babel-loader` 配置内容。
|