@modern-js/main-doc 2.58.1 → 2.58.3
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/apis/app/hooks/config/icon.mdx +1 -1
- package/docs/en/apis/app/runtime/web-server/unstable_middleware.mdx +2 -2
- package/docs/en/community/blog/_meta.json +1 -6
- package/docs/en/components/deploy.mdx +1 -1
- package/docs/en/configure/_meta.json +1 -1
- package/docs/en/configure/app/tools/esbuild.mdx +1 -1
- package/docs/en/configure/app/tools/swc.mdx +2 -2
- package/docs/en/guides/_meta.json +5 -0
- package/docs/en/guides/advanced-features/bff/_meta.json +1 -6
- package/docs/en/guides/advanced-features/rspack-start.mdx +1 -8
- package/docs/en/guides/advanced-features/ssr/_meta.json +1 -5
- package/docs/en/guides/basic-features/data/_meta.json +1 -4
- package/docs/en/guides/basic-features/data/data-fetch.mdx +1 -1
- package/docs/en/guides/basic-features/routes.mdx +1 -1
- package/docs/en/guides/concept/_meta.json +1 -4
- package/docs/en/guides/concept/entries.mdx +70 -46
- package/docs/en/guides/get-started/_meta.json +1 -7
- package/docs/en/guides/get-started/introduction.mdx +1 -1
- package/docs/en/guides/get-started/quick-start.mdx +0 -1
- package/docs/en/guides/get-started/tech-stack.mdx +0 -2
- package/docs/en/guides/get-started/upgrade.mdx +16 -2
- package/docs/en/guides/rsbuild-plugins/plugin-esbuild.mdx +205 -0
- package/docs/en/guides/rsbuild-plugins/plugin-swc.mdx +356 -0
- package/docs/en/guides/topic-detail/framework-plugin/_meta.json +1 -1
- package/docs/en/guides/topic-detail/generator/_meta.json +1 -1
- package/docs/en/guides/topic-detail/generator/create/_meta.json +1 -5
- package/docs/en/guides/topic-detail/generator/new/_meta.json +1 -5
- package/docs/en/guides/topic-detail/generator/plugin/_meta.json +1 -1
- package/docs/en/guides/troubleshooting/_meta.json +1 -6
- package/docs/zh/apis/app/hooks/config/icon.mdx +1 -1
- package/docs/zh/apis/app/runtime/web-server/unstable_middleware.mdx +2 -2
- package/docs/zh/community/blog/_meta.json +1 -6
- package/docs/zh/components/deploy.mdx +1 -1
- package/docs/zh/configure/_meta.json +1 -1
- package/docs/zh/configure/app/tools/esbuild.mdx +1 -1
- package/docs/zh/configure/app/tools/swc.mdx +2 -2
- package/docs/zh/guides/_meta.json +5 -0
- package/docs/zh/guides/advanced-features/bff/_meta.json +1 -6
- package/docs/zh/guides/advanced-features/rspack-start.mdx +1 -8
- package/docs/zh/guides/advanced-features/ssr/_meta.json +1 -5
- package/docs/zh/guides/basic-features/data/_meta.json +1 -4
- package/docs/zh/guides/basic-features/data/data-fetch.mdx +3 -4
- package/docs/zh/guides/basic-features/routes.mdx +1 -1
- package/docs/zh/guides/concept/_meta.json +1 -4
- package/docs/zh/guides/concept/entries.mdx +76 -57
- package/docs/zh/guides/get-started/_meta.json +1 -7
- package/docs/zh/guides/get-started/introduction.mdx +2 -2
- package/docs/zh/guides/get-started/quick-start.mdx +0 -1
- package/docs/zh/guides/get-started/tech-stack.mdx +0 -2
- package/docs/zh/guides/get-started/upgrade.mdx +15 -1
- package/docs/zh/guides/rsbuild-plugins/plugin-esbuild.mdx +201 -0
- package/docs/zh/guides/rsbuild-plugins/plugin-swc.mdx +344 -0
- package/docs/zh/guides/topic-detail/framework-plugin/_meta.json +1 -1
- package/docs/zh/guides/topic-detail/generator/_meta.json +1 -1
- package/docs/zh/guides/topic-detail/generator/create/_meta.json +1 -5
- package/docs/zh/guides/topic-detail/generator/new/_meta.json +1 -5
- package/docs/zh/guides/topic-detail/generator/plugin/_meta.json +1 -1
- package/docs/zh/guides/troubleshooting/_meta.json +1 -6
- package/package.json +4 -4
- package/rspress.config.ts +2 -2
- package/src/components/ContentCard/index.tsx +1 -1
- package/src/components/FeatureLayout/index.tsx +1 -1
- package/src/components/Footer/index.tsx +2 -2
- package/src/components/ListCard/index.tsx +1 -1
- package/src/components/Sandpack/index.tsx +5 -3
- package/src/components/SecondaryTitle/index.tsx +1 -1
- package/src/components/ShowcaseList/index.tsx +1 -1
- package/src/i18n/index.ts +1 -1
- package/src/i18n/zhCN.ts +1 -1
- package/src/index.ts +0 -1
- package/src/pages/index.tsx +15 -7
- package/src/.eslintrc.json +0 -3
@@ -0,0 +1,356 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 2
|
3
|
+
---
|
4
|
+
|
5
|
+
# SWC Plugin
|
6
|
+
|
7
|
+
import SWC from '@modern-js/builder-doc/docs/en/shared/swc.md';
|
8
|
+
|
9
|
+
<SWC />
|
10
|
+
|
11
|
+
## Usage Scenarios
|
12
|
+
|
13
|
+
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.
|
14
|
+
|
15
|
+
### Rspack Scenario
|
16
|
+
|
17
|
+
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.
|
18
|
+
|
19
|
+
If you have configured the current SWC plugin when using Rspack, it will not have any effect.
|
20
|
+
|
21
|
+
### Babel Plugins
|
22
|
+
|
23
|
+
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.
|
24
|
+
|
25
|
+
For most common Babel plugins, you can find corresponding replacements in SWC, such as:
|
26
|
+
|
27
|
+
- `@babel/preset-env`: use [presetEnv](#presetenv) instead.
|
28
|
+
- `@babel/preset-react`: use [presetReact](#presetreact) instead.
|
29
|
+
- `babel-plugin-import`: use [source.transformImport](/configure/app/source/transform-import) instead.
|
30
|
+
- `babel-plugin-lodash`: use [extensions.lodash](#extensionslodash) instead.
|
31
|
+
- `@emotion/babel-plugin`: use [extensions.emotion](#extensionsemotion) instead.
|
32
|
+
- `babel-plugin-styled-components`: use [extensions.styledComponents](#extensionsstyledcomponents) instead.
|
33
|
+
- `@babel/plugin-react-transform-remove-prop-types`: use [reactUtils.removePropTypes](#extensionsreactutils) instead.
|
34
|
+
|
35
|
+
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.
|
36
|
+
|
37
|
+
### Bundle Size
|
38
|
+
|
39
|
+
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.
|
40
|
+
|
41
|
+
For a detailed comparison between minifiers, see [minification-benchmarks](https://github.com/privatenumber/minification-benchmarks).
|
42
|
+
|
43
|
+
## Quick Start
|
44
|
+
|
45
|
+
### Used in Modern.js framework
|
46
|
+
|
47
|
+
The Modern.js framework integrates the Builder's SWC plugin, and you can use it in the following ways:
|
48
|
+
|
49
|
+
import EnableSWC from '@modern-js/builder-doc/docs/en/shared/enableSwc.md';
|
50
|
+
|
51
|
+
<EnableSWC />
|
52
|
+
|
53
|
+
That's it! Now you can use SWC transformation and minification in your project.
|
54
|
+
|
55
|
+
## Config
|
56
|
+
|
57
|
+
- **Type:**
|
58
|
+
|
59
|
+
```ts
|
60
|
+
type PluginConfig =
|
61
|
+
| ObjPluginConfig
|
62
|
+
| ((
|
63
|
+
config: ObjPluginConfig,
|
64
|
+
utils: { mergeConfig: typeof lodash.merge; setConfig: typeof lodash.set },
|
65
|
+
) => ObjPluginConfig | void);
|
66
|
+
|
67
|
+
// SwcOptions is configurations of swc https://swc.rs/docs/configuration/compilation
|
68
|
+
interface ObjPluginConfig extends SwcOptions {
|
69
|
+
presetReact?: ReactConfig;
|
70
|
+
presetEnv?: EnvConfig;
|
71
|
+
jsMinify?: boolean | JsMinifyOptions;
|
72
|
+
cssMinify?: boolean | CssMinifyOptions;
|
73
|
+
extensions?: Extensions;
|
74
|
+
overrides?: Overrides;
|
75
|
+
}
|
76
|
+
```
|
77
|
+
|
78
|
+
The plugin configuration is based on the SWC configuration. In order to simplify some deep-level configurations and improve development experience, certain extensions have been made. When using object-based configuration, for example you can use `presetReact` and `presetEnv` to quickly configure React-related features and syntax downgrading. Other configurations will also be directly passed through to SWC.
|
79
|
+
|
80
|
+
When using function-based configuration, the default configuration generated by the plugin will be passed in, allowing you to modify it or return a new configuration.
|
81
|
+
|
82
|
+
### presetReact
|
83
|
+
|
84
|
+
- **Type:** [presetReact](https://swc.rs/docs/configuration/compilation#jsctransformreact) in SWC.
|
85
|
+
|
86
|
+
Ported from `@babel/preset-react`. The value you passed will be merged with default option.
|
87
|
+
|
88
|
+
By default, the plugin will set `runtime` field based on your `react` version, if `react` version is newer than 17.0.0, it will be set to `automatic`, otherwish `classic`.
|
89
|
+
|
90
|
+
### presetEnv
|
91
|
+
|
92
|
+
- **Type:** [presetEnv](https://swc.rs/docs/configuration/supported-browsers#options) in SWC.
|
93
|
+
|
94
|
+
Ported from `@babel/preset-env`. The value you passed will be merged with default option.
|
95
|
+
|
96
|
+
Default option is:
|
97
|
+
|
98
|
+
```ts
|
99
|
+
{
|
100
|
+
targets: '', // automatic get browserslist from your project, so you don't have to set this field
|
101
|
+
mode: 'usage',
|
102
|
+
}
|
103
|
+
```
|
104
|
+
|
105
|
+
### jsMinify
|
106
|
+
|
107
|
+
- **Type:** `boolean` or [compress configuration](https://terser.org/docs/api-reference.html#compress-options).
|
108
|
+
- **Default:** `{ compress: {}, mangle: true }`.
|
109
|
+
|
110
|
+
If set it to `false`, then SWC minification will be disabled, if set it to `true` then will it applies default option. If you pass an object, then this object will be merged with default option.
|
111
|
+
|
112
|
+
### cssMinify
|
113
|
+
|
114
|
+
- **Type:**: `boolean`
|
115
|
+
- **Default:**: `true`
|
116
|
+
|
117
|
+
Whether enable to compress CSS files by SWC. If enabled, it will improve the performance of CSS compression, but the compression ratio will be slightly reduced.
|
118
|
+
|
119
|
+
### overrides
|
120
|
+
|
121
|
+
- **Type:**
|
122
|
+
|
123
|
+
```ts
|
124
|
+
interface Overrides extends SwcOptions {
|
125
|
+
test: RegExp;
|
126
|
+
include: RegExp[];
|
127
|
+
exclude: RegExp[];
|
128
|
+
}
|
129
|
+
```
|
130
|
+
|
131
|
+
- **Default:** `undefined`
|
132
|
+
|
133
|
+
Specify special configuration for specific modules. For example if you want to set ie 11 target for `foo.ts`:
|
134
|
+
|
135
|
+
```ts
|
136
|
+
{
|
137
|
+
test: /foo.ts/,
|
138
|
+
env: { targets: 'ie 11' }
|
139
|
+
}
|
140
|
+
```
|
141
|
+
|
142
|
+
This will merged into the default configuration, and do not affect other modules.
|
143
|
+
|
144
|
+
### extensions
|
145
|
+
|
146
|
+
- **Type:** `Object`
|
147
|
+
|
148
|
+
Some plugins ported from Babel.
|
149
|
+
|
150
|
+
#### extensions.reactUtils
|
151
|
+
|
152
|
+
- **Type:** `Object`
|
153
|
+
|
154
|
+
```ts
|
155
|
+
type ReactUtilsOptions = {
|
156
|
+
autoImportReact?: boolean;
|
157
|
+
removeEffect?: boolean;
|
158
|
+
removePropTypes?: {
|
159
|
+
mode?: 'remove' | 'unwrap' | 'unsafe-wrap';
|
160
|
+
removeImport?: boolean;
|
161
|
+
ignoreFilenames?: string[];
|
162
|
+
additionalLibraries?: string[];
|
163
|
+
classNameMatchers?: string[];
|
164
|
+
};
|
165
|
+
};
|
166
|
+
```
|
167
|
+
|
168
|
+
Some little help utils for `React`.
|
169
|
+
|
170
|
+
**reactUtils.autoImportReact**
|
171
|
+
|
172
|
+
- **Type:** `boolean`
|
173
|
+
|
174
|
+
Automatically import `React` as global variable, eg: `import React from 'react'`.
|
175
|
+
Mostly used for generated `React.createElement`.
|
176
|
+
|
177
|
+
**reactUtils.removeEffect**
|
178
|
+
|
179
|
+
- **Type:** `boolean`
|
180
|
+
|
181
|
+
Remove `useEffect` call.
|
182
|
+
|
183
|
+
**reactUtils.removePropTypes**
|
184
|
+
|
185
|
+
- **Type:**
|
186
|
+
|
187
|
+
```ts
|
188
|
+
type RemovePropTypesOptions = {
|
189
|
+
mode?: 'remove' | 'unwrap' | 'unsafe-wrap';
|
190
|
+
removeImport?: boolean;
|
191
|
+
ignoreFilenames?: string[];
|
192
|
+
additionalLibraries?: string[];
|
193
|
+
classNameMatchers?: string[];
|
194
|
+
};
|
195
|
+
```
|
196
|
+
|
197
|
+
Remove `React` runtime type checking. This is ported from [@babel/plugin-react-transform-remove-prop-types](https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types), All the configurations remain the same.
|
198
|
+
|
199
|
+
#### extensions.lodash
|
200
|
+
|
201
|
+
- **Type:**
|
202
|
+
|
203
|
+
```ts
|
204
|
+
type LodashOptions = {
|
205
|
+
cwd?: string;
|
206
|
+
ids?: string[];
|
207
|
+
};
|
208
|
+
```
|
209
|
+
|
210
|
+
- **Default:**
|
211
|
+
|
212
|
+
```ts
|
213
|
+
const defaultOptions = {
|
214
|
+
cwd: process.cwd(),
|
215
|
+
ids: ['lodash', 'lodash-es'],
|
216
|
+
};
|
217
|
+
```
|
218
|
+
|
219
|
+
Ported from [babel-plugin-lodash](https://github.com/lodash/babel-plugin-lodash), it is used to automatically convert references to Lodash into on-demand imports, thereby reducing the bundle size of Lodash code.
|
220
|
+
|
221
|
+
```ts
|
222
|
+
// Input
|
223
|
+
import { get, throttle } from 'lodash';
|
224
|
+
|
225
|
+
// Output
|
226
|
+
import get from 'lodash/get';
|
227
|
+
import throttle from 'lodash/throttle';
|
228
|
+
```
|
229
|
+
|
230
|
+
#### extensions.styledComponents
|
231
|
+
|
232
|
+
- **Type:**
|
233
|
+
|
234
|
+
```ts
|
235
|
+
boolean | {
|
236
|
+
// Enabled by default in development, disabled in production to reduce file size,
|
237
|
+
// setting this will override the default for all environments.
|
238
|
+
displayName?: boolean,
|
239
|
+
// Enabled by default.
|
240
|
+
ssr?: boolean,
|
241
|
+
// Enabled by default.
|
242
|
+
fileName?: boolean,
|
243
|
+
// Empty by default.
|
244
|
+
topLevelImportPaths?: string[],
|
245
|
+
// Defaults to ["index"].
|
246
|
+
meaninglessFileNames?: string[],
|
247
|
+
// Enabled by default.
|
248
|
+
cssProp?: boolean,
|
249
|
+
// Empty by default.
|
250
|
+
namespace?: string,
|
251
|
+
};
|
252
|
+
```
|
253
|
+
|
254
|
+
This is ported by Next.js team from [babel-plugin-styled-components](https://github.com/styled-components/babel-plugin-styled-components).
|
255
|
+
|
256
|
+
#### extensions.emotion
|
257
|
+
|
258
|
+
- **Type:**
|
259
|
+
|
260
|
+
```ts
|
261
|
+
boolean | {
|
262
|
+
// default is true. It will be disabled when build type is production.
|
263
|
+
sourceMap?: boolean,
|
264
|
+
// default is 'dev-only'.
|
265
|
+
autoLabel?: 'never' | 'dev-only' | 'always',
|
266
|
+
// default is '[local]'.
|
267
|
+
// Allowed values: `[local]` `[filename]` and `[dirname]`
|
268
|
+
// This option only works when autoLabel is set to 'dev-only' or 'always'.
|
269
|
+
// It allows you to define the format of the resulting label.
|
270
|
+
// The format is defined via string where variable parts are enclosed in square brackets [].
|
271
|
+
// For example labelFormat: "my-classname--[local]", where [local] will be replaced with the name of the variable the result is assigned to.
|
272
|
+
labelFormat?: string,
|
273
|
+
// default is undefined.
|
274
|
+
// This option allows you to tell the compiler what imports it should
|
275
|
+
// look at to determine what it should transform so if you re-export
|
276
|
+
// Emotion's exports, you can still use transforms.
|
277
|
+
importMap?: {
|
278
|
+
[packageName: string]: {
|
279
|
+
[exportName: string]: {
|
280
|
+
canonicalImport?: [string, string],
|
281
|
+
styledBaseImport?: [string, string],
|
282
|
+
}
|
283
|
+
}
|
284
|
+
},
|
285
|
+
}
|
286
|
+
```
|
287
|
+
|
288
|
+
This is ported by Next.js team from [@emotion/babel-plugin](https://www.npmjs.com/package/@emotion/babel-plugin)
|
289
|
+
|
290
|
+
#### extensions.pluginImport
|
291
|
+
|
292
|
+
:::tip
|
293
|
+
Builder provides the [source.transformImport](/configure/app/source/transform-import) config, so you don't need to configure `extensions.pluginImport` manually.
|
294
|
+
:::
|
295
|
+
|
296
|
+
Ported from [babel-plugin-import](https://github.com/umijs/babel-plugin-import), configurations are the same.
|
297
|
+
|
298
|
+
Some configurations can be passed in functions, such as `customName`, `customStyleName`. These JavaScript functions will be called by Rust through Node-API, which will cause some performance overhead.
|
299
|
+
|
300
|
+
Some simple function logic can be replaced by template language. Therefore, the configuration of function items such as `customName`, `customStyleName` can also be passed in strings as templates to replace functions and improve performance.
|
301
|
+
|
302
|
+
For example:
|
303
|
+
|
304
|
+
```ts
|
305
|
+
import { MyButton as Btn } from 'foo';
|
306
|
+
```
|
307
|
+
|
308
|
+
Apply following configurations:
|
309
|
+
|
310
|
+
```ts
|
311
|
+
PluginSWC({
|
312
|
+
extensions: {
|
313
|
+
pluginImport: [
|
314
|
+
{
|
315
|
+
libraryName: 'foo',
|
316
|
+
customName: 'foo/es/{{ member }}',
|
317
|
+
},
|
318
|
+
],
|
319
|
+
},
|
320
|
+
});
|
321
|
+
```
|
322
|
+
|
323
|
+
`{{ member }}` will be replaced by the imported specifier:
|
324
|
+
|
325
|
+
```ts
|
326
|
+
import Btn from 'foo/es/MyButton';
|
327
|
+
```
|
328
|
+
|
329
|
+
Template `customName: 'foo/es/{{ member }}'` is the same as `` customName: (member) => `foo/es/${member}` ``, but template value has no performance overhead of Node-API.
|
330
|
+
|
331
|
+
The template used here is [handlebars](https://handlebarsjs.com). There are some useful builtin tools, Take the above import statement as an example:
|
332
|
+
|
333
|
+
```ts
|
334
|
+
PluginSWC({
|
335
|
+
extensions: {
|
336
|
+
pluginImport: [
|
337
|
+
{
|
338
|
+
libraryName: 'foo',
|
339
|
+
customName: 'foo/es/{{ kebabCase member }}',
|
340
|
+
},
|
341
|
+
],
|
342
|
+
},
|
343
|
+
});
|
344
|
+
```
|
345
|
+
|
346
|
+
Transformed to:
|
347
|
+
|
348
|
+
```ts
|
349
|
+
import Btn from 'foo/es/my-button';
|
350
|
+
```
|
351
|
+
|
352
|
+
In addition to `kebabCase`, there are `cameraCase`, `snakeCase`, `upperCase` and `lowerCase` can be used as well.
|
353
|
+
|
354
|
+
## Limitation
|
355
|
+
|
356
|
+
Do not support `@babel/plugin-transform-runtime` and other custom Babel plugins.
|
@@ -91,7 +91,7 @@ import {
|
|
91
91
|
} from '@modern-js/runtime/server';
|
92
92
|
import type { Vars } from '../shared/index';
|
93
93
|
|
94
|
-
const setPayload: UnstableMiddleware = async (
|
94
|
+
const setPayload: UnstableMiddleware<Vars> = async (
|
95
95
|
c: UnstableMiddlewareContext<Vars>,
|
96
96
|
next,
|
97
97
|
) => {
|
@@ -100,7 +100,7 @@ const setPayload: UnstableMiddleware = async (
|
|
100
100
|
await next();
|
101
101
|
};
|
102
102
|
|
103
|
-
export const unstableMiddleware: UnstableMiddleware[] = [setPayload];
|
103
|
+
export const unstableMiddleware: UnstableMiddleware<Vars>[] = [setPayload];
|
104
104
|
```
|
105
105
|
|
106
106
|
```ts title="src/routes/page.data.ts"
|
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
本地验证完成后,可以参考 [部署](/guides/basic-features/deploy.html) 一节,将项目部署到服务器上。
|
@@ -19,7 +19,7 @@ import SWC from '@modern-js/builder-doc/docs/zh/shared/swc.md';
|
|
19
19
|
|
20
20
|
## 在 Rspack 模式下使用
|
21
21
|
|
22
|
-
通过 `tools.swc` 可以设置 Rspack [builtin:swc-loader](https://
|
22
|
+
通过 `tools.swc` 可以设置 Rspack [builtin:swc-loader](https://rspack.dev/zh/guide/features/builtin-swc-loader) 的选项。
|
23
23
|
|
24
24
|
```
|
25
25
|
import { defineConfig } from '@modern-js/app-tools';
|
@@ -77,4 +77,4 @@ export default defineConfig({
|
|
77
77
|
});
|
78
78
|
```
|
79
79
|
|
80
|
-
完整配置项请参考 [
|
80
|
+
完整配置项请参考 [SWC 插件配置](/guides/rsbuild-plugins/plugin-swc.html#配置)。
|
@@ -22,13 +22,6 @@ import InitRspackApp from '@site-docs/components/init-rspack-app';
|
|
22
22
|
|
23
23
|
项目创建完成后,在项目中执行 `pnpm run dev` 即可体验项目,更多信息可参考[快速上手](/guides/get-started/quick-start.html)。
|
24
24
|
|
25
|
-
:::tip
|
26
|
-
在使用 Rspack 作为打包工具时,由于部分能力尚在开发中,以下 features 暂时无法使用,我们将在未来提供支持:
|
27
|
-
|
28
|
-
- 客户端渲染(CSR)使用 [useLoader](/guides/basic-features/data/data-fetch.html)
|
29
|
-
|
30
|
-
:::
|
31
|
-
|
32
25
|
## 开启 Rspack 构建
|
33
26
|
|
34
27
|
从 Modern.js MAJOR_VERSION.46.0 版本起,在一个已有的 Modern.js 项目中,你仅需在 `modern.config.ts` 中添加以下配置,即可启用 Rspack 构建:
|
@@ -86,7 +79,7 @@ export default {
|
|
86
79
|
|
87
80
|
## 修改转译配置
|
88
81
|
|
89
|
-
Modern.js 在 Rspack 模式下使用 Rspack [builtin:swc-loader](https://
|
82
|
+
Modern.js 在 Rspack 模式下使用 Rspack [builtin:swc-loader](https://rspack.dev/zh/guide/features/builtin-swc-loader) 进行代码转译。
|
90
83
|
|
91
84
|
Modern.js 已对 `builtin:swc-loader` 的常见配置提供了更方便的配置方式,如:通过 [source.transformImport](/configure/app/source/transform-import) 配置组件库按需引入。如果对 `builtin:swc-loader` 有自定义配置的需求,可通过 [tools.swc](/configure/app/tools/swc) 进行配置:
|
92
85
|
|
@@ -67,9 +67,9 @@ export const loader = async (): Promise<ProfileData> => {
|
|
67
67
|
|
68
68
|
:::
|
69
69
|
|
70
|
-
在 CSR
|
70
|
+
在 CSR 项目中,`loader` 函数会在客户端执行,`loader` 函数内可以使用浏览器的 API(但通常不需要,也不推荐)。
|
71
71
|
|
72
|
-
在 SSR
|
72
|
+
在 SSR 项目中,不管是首屏,还是在客户端的导航,`loader` 函数只会在服务端执行,这里可以调用任意的 Node.js API,同时这里使用的任何依赖和代码都不会包含在客户端的 bundle 中。
|
73
73
|
|
74
74
|
:::info
|
75
75
|
在以后的版本中,Modern.js 可能会支持在 CSR 环境下,`loader` 函数也在服务端运行,以提高性能和安全性,所以这里建议尽可能地保证 `loader` 的纯粹,只做数据获取的场景。
|
@@ -499,8 +499,7 @@ export const loader = async (): Promise<ProfileData> => {
|
|
499
499
|
**`useLoader`** 是 Modern.js 老版本中的 API。该 API 是一个 React Hook,专门提供给 SSR 应用使用,让开发者能同构的在组件中获取数据。
|
500
500
|
|
501
501
|
:::tip
|
502
|
-
CSR 的项目没有必要使用 `useLoader`
|
503
|
-
|
502
|
+
CSR 的项目没有必要使用 `useLoader` 获取数据,且在使用 Rspack 作为打包工具时,`useLoader` 不支持使用。
|
504
503
|
:::
|
505
504
|
|
506
505
|
以下是一个最简单的例子:
|
@@ -381,7 +381,7 @@ export default () => {
|
|
381
381
|
|
382
382
|
`routes/` 下每一层目录中,开发者同样可以定义一个 `error.tsx` 文件,默认导出一个 `<ErrorBoundary>` 组件。
|
383
383
|
|
384
|
-
当有路由目录下存在该组件时,组件渲染出错会被 `ErrorBoundary`
|
384
|
+
当有路由目录下存在该组件时,组件渲染出错会被 `ErrorBoundary` 组件捕获。
|
385
385
|
|
386
386
|
`<ErrorBoundary>` 可以返回出错时的 UI 视图,当前层级未声明 `<ErrorBoundary>` 组件时,错误会向上冒泡到更上层的组件,直到被捕获或抛出错误。同时,当组件出错时,只会影响捕获到该错误的路由组件及子组件,其他组件的状态和视图不受影响,可以继续交互。
|
387
387
|
|