@modern-js/main-doc 2.5.0 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +1 -1
- package/en/apis/app/commands.mdx +297 -0
- package/en/apis/app/hooks/_category_.json +1 -1
- package/en/apis/app/hooks/src/routes.mdx +2 -2
- package/en/components/init-app.mdx +5 -5
- package/en/configure/app/bff/prefix.mdx +2 -3
- package/en/configure/app/bff/proxy.mdx +1 -1
- package/en/configure/app/dev/before-start-url.mdx +13 -0
- package/en/configure/app/dev/host.mdx +13 -0
- package/en/configure/app/output/ssg.mdx +3 -3
- package/en/configure/app/runtime/master-app.mdx +1 -1
- package/en/configure/app/source/entries-dir.mdx +1 -1
- package/en/configure/app/testing/transformer.mdx +1 -1
- package/en/configure/app/tools/jest.mdx +1 -1
- package/en/guides/advanced-features/rspack-start.mdx +69 -0
- package/en/guides/advanced-features/ssg.mdx +8 -8
- package/en/guides/basic-features/alias.mdx +4 -4
- package/en/guides/basic-features/data-fetch.mdx +4 -4
- package/en/guides/basic-features/routes.mdx +23 -7
- package/en/guides/concept/builder.mdx +1 -1
- package/en/guides/get-started/quick-start.mdx +2 -2
- package/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +1 -1
- package/en/tutorials/first-app/c07-container.mdx +17 -17
- package/en/tutorials/first-app/c08-entries.mdx +23 -23
- package/package.json +3 -3
- package/scripts/summary.en.json +1 -1
- package/scripts/summary.zh.json +1 -1
- package/zh/apis/app/commands.mdx +299 -0
- package/zh/apis/app/hooks/_category_.json +1 -1
- package/zh/apis/app/hooks/src/routes.mdx +2 -2
- package/zh/components/init-app.mdx +5 -5
- package/zh/configure/app/bff/prefix.mdx +1 -1
- package/zh/configure/app/bff/proxy.mdx +1 -1
- package/zh/configure/app/dev/before-start-url.mdx +13 -0
- package/zh/configure/app/dev/host.mdx +13 -0
- package/zh/configure/app/output/ssg.mdx +3 -3
- package/zh/configure/app/source/entries-dir.mdx +1 -1
- package/zh/guides/advanced-features/rspack-start.mdx +69 -0
- package/zh/guides/advanced-features/ssg.mdx +8 -8
- package/zh/guides/advanced-features/ssr.mdx +3 -2
- package/zh/guides/basic-features/alias.mdx +4 -4
- package/zh/guides/basic-features/data-fetch.mdx +4 -4
- package/zh/guides/basic-features/routes.mdx +23 -7
- package/zh/guides/concept/builder.mdx +2 -2
- package/zh/guides/get-started/quick-start.mdx +2 -2
- package/zh/guides/topic-detail/micro-frontend/c03-main-app.mdx +1 -1
- package/zh/tutorials/first-app/c07-container.mdx +17 -17
- package/zh/tutorials/first-app/c08-entries.mdx +23 -23
- package/en/apis/app/commands/_category_.json +0 -5
- package/en/apis/app/commands/build.mdx +0 -39
- package/en/apis/app/commands/dev.mdx +0 -61
- package/en/apis/app/commands/inspect.mdx +0 -61
- package/en/apis/app/commands/lint.mdx +0 -19
- package/en/apis/app/commands/new.mdx +0 -55
- package/en/apis/app/commands/serve.mdx +0 -27
- package/en/apis/app/commands/test.mdx +0 -35
- package/en/apis/app/commands/upgrade.mdx +0 -18
- package/zh/apis/app/commands/_category_.json +0 -5
- package/zh/apis/app/commands/build.mdx +0 -39
- package/zh/apis/app/commands/dev.mdx +0 -61
- package/zh/apis/app/commands/inspect.mdx +0 -61
- package/zh/apis/app/commands/lint.mdx +0 -19
- package/zh/apis/app/commands/new.mdx +0 -54
- package/zh/apis/app/commands/serve.mdx +0 -27
- package/zh/apis/app/commands/test.mdx +0 -35
- package/zh/apis/app/commands/upgrade.mdx +0 -18
package/.turbo/turbo-build.log
CHANGED
@@ -0,0 +1,297 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 1
|
3
|
+
---
|
4
|
+
|
5
|
+
# Commands
|
6
|
+
|
7
|
+
Modern.js has some built-in commands that can help you quickly start a development server, build production environment code, and more.
|
8
|
+
|
9
|
+
Through this chapter, you can learn about the built-in commands of Modern.js and how to use them.
|
10
|
+
|
11
|
+
## modern dev
|
12
|
+
|
13
|
+
The `modern dev` command is used to start a local development server and compile the source code in the development environment.
|
14
|
+
|
15
|
+
```bash
|
16
|
+
Usage: modern dev [options]
|
17
|
+
|
18
|
+
Options:
|
19
|
+
-e --entry <entry> compiler by entry
|
20
|
+
-c --config <config> configuration file path, which can be a relative path or an absolute path
|
21
|
+
-h, --help show command help
|
22
|
+
--analyze analyze the bundle and view size of each module
|
23
|
+
--api-only only start API service
|
24
|
+
```
|
25
|
+
|
26
|
+
After running `modern dev`, Modern.js will watch source file changes and apply hot module replacement.
|
27
|
+
|
28
|
+
```bash
|
29
|
+
$ modern dev
|
30
|
+
|
31
|
+
info Starting dev server...
|
32
|
+
info App running at:
|
33
|
+
|
34
|
+
> Local: http://localhost:8080/
|
35
|
+
> Network: http://192.168.0.1:8080/
|
36
|
+
```
|
37
|
+
|
38
|
+
### Compile Partial Pages
|
39
|
+
|
40
|
+
In multi-page (MPA) projects, the `--entry` option can be added to specify one or more pages to compile. In this way, only part of the code in the project will be compiled, and the dev startup speed will be faster.
|
41
|
+
|
42
|
+
For example, execute `modern dev --entry`, the entry selector will be displayed in the command line interface:
|
43
|
+
|
44
|
+
```bash
|
45
|
+
$ modern dev --entry
|
46
|
+
|
47
|
+
? Please select the entry that needs to be built
|
48
|
+
❯ ◯ foo
|
49
|
+
◯ bar
|
50
|
+
◯ baz
|
51
|
+
```
|
52
|
+
|
53
|
+
For example, if you select the `foo` entry, only the code related to the `foo` entry will be compiled, and the code of other pages will not be compiled.
|
54
|
+
|
55
|
+
### Specify the page by parameter
|
56
|
+
|
57
|
+
You can also specify the page name through parameters after `--entry`, and the names of multiple pages can be separated by commas.
|
58
|
+
|
59
|
+
```bash
|
60
|
+
# Compile foo page
|
61
|
+
modern dev --entry foo
|
62
|
+
|
63
|
+
# Compile foo and bar pages
|
64
|
+
modern dev --entry foo,bar
|
65
|
+
```
|
66
|
+
|
67
|
+
## modern start
|
68
|
+
|
69
|
+
`modern start` is an alias of `modern dev` command, the usage of the two are exactly the same.
|
70
|
+
|
71
|
+
## modern build
|
72
|
+
|
73
|
+
The `modern build` command will build a production-ready product in the `dist/` directory by default. You can specify the output directory by modifying the configuration [`output.distPath`](/configure/app/output/dist-path).
|
74
|
+
|
75
|
+
```bash
|
76
|
+
Usage: modern build [options]
|
77
|
+
|
78
|
+
Options:
|
79
|
+
-c --config <config> configuration file path, which can be a relative path or an absolute path
|
80
|
+
-h, --help show command help
|
81
|
+
--analyze analyze the bundle and view size of each module
|
82
|
+
```
|
83
|
+
|
84
|
+
### Analyze Bundle
|
85
|
+
|
86
|
+
execute `npx modern build --analyze` command,can produce an HTML file that analyzes the volume of the bundle while packaging the production code:
|
87
|
+
|
88
|
+
```
|
89
|
+
Bundle Analyzer saved report to /example/dist/report.html
|
90
|
+
File sizes after production build:
|
91
|
+
|
92
|
+
122.35 KB dist/static/js/885.1d4fbe5a.js
|
93
|
+
2.3 KB dist/static/js/main.4b8e8d64.js
|
94
|
+
761 B dist/static/js/runtime-main.edb7cf35.js
|
95
|
+
645 B dist/static/css/main.0dd3ecc1.css
|
96
|
+
```
|
97
|
+
|
98
|
+
Open the above HTML file in the browser, you can see the tile diagram of the packaged product, and perform package volume analysis and optimization:
|
99
|
+
|
100
|
+
<img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/mwa-build-analyze-8784f762c1ab0cb20935829d5f912c4c.png" />
|
101
|
+
|
102
|
+
> this features based on [webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer).
|
103
|
+
|
104
|
+
## modern new
|
105
|
+
|
106
|
+
The `modern new` command is used to enable features in an existing project.
|
107
|
+
|
108
|
+
For example, add application entry, enable some optional features such as Tailwind CSS, micro frontend, etc.
|
109
|
+
|
110
|
+
```bash
|
111
|
+
Usage: modern new [options]
|
112
|
+
|
113
|
+
Options:
|
114
|
+
-d, --debug using debug mode to log something (default: false)
|
115
|
+
-c, --config <config> set default generator config(json string)
|
116
|
+
--dist-tag <tag> use specified tag version for its generator
|
117
|
+
--registry set npm registry url to run npm command
|
118
|
+
-h, --help show command help
|
119
|
+
```
|
120
|
+
|
121
|
+
### Add Entry
|
122
|
+
|
123
|
+
In the project, execute the `new` command to add entries as follows:
|
124
|
+
|
125
|
+
```bash
|
126
|
+
$ npx modern new
|
127
|
+
? Action Create project element
|
128
|
+
? Create project element New "entry"
|
129
|
+
? Entry name entry
|
130
|
+
```
|
131
|
+
|
132
|
+
### Enable Features
|
133
|
+
|
134
|
+
In the project, execute the `new` command to enable features as follows:
|
135
|
+
|
136
|
+
```bash
|
137
|
+
$ npx modern new
|
138
|
+
? Action Enable features
|
139
|
+
? Enable features (Use arrow keys)
|
140
|
+
❯ Enable Tailwind CSS
|
141
|
+
Enable BFF
|
142
|
+
Enable SSG
|
143
|
+
Enable Micro Frontend
|
144
|
+
Enable Unit Test / Integration Test
|
145
|
+
Enable Visual Testing (Storybook)
|
146
|
+
```
|
147
|
+
|
148
|
+
:::tip
|
149
|
+
The `--config` parameter needs to use a JSON string.
|
150
|
+
|
151
|
+
pnpm does not support the use of JSON strings as parameter values currently. Use `npm new` to turn on.【[Relate Issue](https://github.com/pnpm/pnpm/issues/3876)】
|
152
|
+
:::
|
153
|
+
|
154
|
+
## modern serve
|
155
|
+
|
156
|
+
Usually use the `modern serve` command to enable project run in the production environment, and you need to execute the [`build'](/apis/app/commands#modern-build) command in advance to build the outputs.
|
157
|
+
|
158
|
+
```bash
|
159
|
+
Usage: modern serve [options]
|
160
|
+
|
161
|
+
Options:
|
162
|
+
-c --config <config> configuration file path, which can be a relative path or an absolute path
|
163
|
+
-h, --help show command help
|
164
|
+
--api-only only run API service
|
165
|
+
```
|
166
|
+
|
167
|
+
By default, the project will run in `localhost:8080`, you can modify the Server port number with `server.port`:
|
168
|
+
|
169
|
+
```js
|
170
|
+
export default defineConfig({
|
171
|
+
server: {
|
172
|
+
port: 8081,
|
173
|
+
},
|
174
|
+
});
|
175
|
+
```
|
176
|
+
|
177
|
+
## modern upgrade
|
178
|
+
|
179
|
+
Execute the command `npx modern upgrade` in the project, by default, dependencies in the `package.json` are updated to the latest version.
|
180
|
+
|
181
|
+
```
|
182
|
+
Usage: modern upgrade [options]
|
183
|
+
|
184
|
+
Options:
|
185
|
+
--registry <registry> specify npm registry (default: "")
|
186
|
+
-d,--debug using debug mode to log something (default: false)
|
187
|
+
--cwd <cwd> app directory (default: "")
|
188
|
+
-h, --help show command help
|
189
|
+
```
|
190
|
+
|
191
|
+
## modern inspect
|
192
|
+
|
193
|
+
The `modern inspect` command is used to view the [Modern.js Builder config](https://modernjs.dev/builder/en/guide/basic/builder-config.html) and webpack config of the project.
|
194
|
+
|
195
|
+
```
|
196
|
+
Usage: modern inspect [options]
|
197
|
+
|
198
|
+
Options:
|
199
|
+
--env <env> view the configuration in the target environment (default: "development")
|
200
|
+
--output <output> Specify the path to output in the dist (default: "/")
|
201
|
+
--verbose Show the full function in the result
|
202
|
+
-c --config <config> configuration file path, which can be a relative path or an absolute path
|
203
|
+
-h, --help show command help
|
204
|
+
```
|
205
|
+
|
206
|
+
After executing the command `npx modern inspect` in the project root directory, the following files will be generated in the `dist` directory of the project:
|
207
|
+
|
208
|
+
- `builder.config.js`: The Modern.js Builder config to use at build time.
|
209
|
+
- `webpack.config.web.js`: The webpack config used by to use at build time.
|
210
|
+
|
211
|
+
```bash
|
212
|
+
➜ npx modern inspect
|
213
|
+
|
214
|
+
Inspect config succeed, open following files to view the content:
|
215
|
+
|
216
|
+
- Builder Config: /root/my-project/dist/builder.config.js
|
217
|
+
- Webpack Config (web): /root/my-project/dist/webpack.config.web.js
|
218
|
+
```
|
219
|
+
|
220
|
+
### Configuration Env
|
221
|
+
|
222
|
+
By default, the inspect command will output the development configs, you can use the `--env production` option to output the production configs:
|
223
|
+
|
224
|
+
```bash
|
225
|
+
modern inspect --env production
|
226
|
+
```
|
227
|
+
|
228
|
+
### Verbose content
|
229
|
+
|
230
|
+
By default, the inspect command will omit the function content in the config object, you can use the `--verbose` option to output the full content of the function:
|
231
|
+
|
232
|
+
```bash
|
233
|
+
modern inspect --verbose
|
234
|
+
```
|
235
|
+
|
236
|
+
### SSR Configuration
|
237
|
+
|
238
|
+
If the project has enabled SSR, an additional `webpack.config.node.js` file will be generated in the `dist/`, corresponding to the webpack configuration at SSR build time.
|
239
|
+
|
240
|
+
```bash
|
241
|
+
➜ npx modern inspect
|
242
|
+
|
243
|
+
Inspect config succeed, open following files to view the content:
|
244
|
+
|
245
|
+
- Builder Config: /root/my-project/dist/builder.config.js
|
246
|
+
- Webpack Config (web): /root/my-project/dist/webpack.config.web.js
|
247
|
+
- Webpack Config (node): /root/my-project/dist/webpack.config.node.js
|
248
|
+
```
|
249
|
+
|
250
|
+
## modern lint
|
251
|
+
|
252
|
+
Run ESLint to check the syntax of the code.
|
253
|
+
|
254
|
+
```bash
|
255
|
+
Usage: modern lint [options] [...files]
|
256
|
+
|
257
|
+
Options:
|
258
|
+
--no-fix disable auto fix source file
|
259
|
+
-h, --help display help for command
|
260
|
+
```
|
261
|
+
|
262
|
+
Normally, only the part of the code modified by this commit needs to be checked by `lint-staged` during the `git commit` phase.
|
263
|
+
|
264
|
+
- `--no-fix` close auto fix by lint.
|
265
|
+
|
266
|
+
## modern test
|
267
|
+
|
268
|
+
`modern test` command will automatically run the test cases.
|
269
|
+
|
270
|
+
```bash
|
271
|
+
Usage: modern test [options]
|
272
|
+
|
273
|
+
Options:
|
274
|
+
-h, --help show command help
|
275
|
+
```
|
276
|
+
|
277
|
+
:::tip
|
278
|
+
`modern test` command need to execute the `new` command in advance to enable the `unit test/integration test`.
|
279
|
+
:::
|
280
|
+
|
281
|
+
The effect is as follows:
|
282
|
+
|
283
|
+
```bash
|
284
|
+
$ npx modern test
|
285
|
+
PASS src/tests/index.test.ts
|
286
|
+
The add method
|
287
|
+
✓ should work fine. (2ms)
|
288
|
+
|
289
|
+
Test Suites: 1 passed, 1 total
|
290
|
+
Tests: 1 passed, 1 total
|
291
|
+
Snapshots: 0 total
|
292
|
+
Time: 0.994 s, estimated 1 s
|
293
|
+
```
|
294
|
+
|
295
|
+
:::info
|
296
|
+
Files match `*.test.(js|ts)` in `api/` or `src/` folders will be recognized as test cases by default.
|
297
|
+
:::
|
@@ -30,11 +30,11 @@ Now, the project structure is as follows:
|
|
30
30
|
```
|
31
31
|
.
|
32
32
|
├── src
|
33
|
-
│
|
34
|
-
│
|
35
|
-
│
|
36
|
-
│
|
37
|
-
│
|
33
|
+
│ ├── modern-app-env.d.ts
|
34
|
+
│ └── routes
|
35
|
+
│ ├── index.css
|
36
|
+
│ ├── layout.tsx
|
37
|
+
│ └── page.tsx
|
38
38
|
├── modern.config.ts
|
39
39
|
├── package.json
|
40
40
|
├── pnpm-lock.yaml
|
@@ -7,9 +7,8 @@ sidebar_label: prefix
|
|
7
7
|
- **Type:** `string`
|
8
8
|
- **Default:** `/api`
|
9
9
|
|
10
|
-
:::caution
|
11
|
-
First you need to enable the "BFF" function using [new](/apis/app/commands
|
12
|
-
|
10
|
+
:::caution
|
11
|
+
First you need to enable the "BFF" function using [new](/apis/app/commands#modern-new) command.
|
13
12
|
:::
|
14
13
|
|
15
14
|
By default, the route access BFF prefix's directory is `/api`, with the following directory structure:
|
@@ -8,7 +8,7 @@ sidebar_label: proxy
|
|
8
8
|
- **Default:** `{}`
|
9
9
|
|
10
10
|
:::caution Caution
|
11
|
-
First you need to enable the "BFF" function using [new](/apis/app/commands
|
11
|
+
First you need to enable the "BFF" function using [new](/apis/app/commands#modern-new) command.
|
12
12
|
|
13
13
|
:::
|
14
14
|
|
@@ -0,0 +1,13 @@
|
|
1
|
+
---
|
2
|
+
sidebar_label: beforeStartUrl
|
3
|
+
---
|
4
|
+
|
5
|
+
# dev.beforeStartUrl
|
6
|
+
|
7
|
+
:::tip
|
8
|
+
This config is provided by Modern.js Builder, more detail can see [dev.beforeStartUrl](https://modernjs.dev/builder/en/api/config-dev.html#devbeforestarturl).
|
9
|
+
:::
|
10
|
+
|
11
|
+
import Main from '@modern-js/builder-doc/docs/en/config/dev/beforeStartUrl.md'
|
12
|
+
|
13
|
+
<Main />
|
@@ -0,0 +1,13 @@
|
|
1
|
+
---
|
2
|
+
sidebar_label: host
|
3
|
+
---
|
4
|
+
|
5
|
+
# dev.host
|
6
|
+
|
7
|
+
:::tip
|
8
|
+
This config is provided by Modern.js Builder, more detail can see [dev.host](https://modernjs.dev/builder/en/api/config-dev.html#devhost).
|
9
|
+
:::
|
10
|
+
|
11
|
+
import Main from '@modern-js/builder-doc/docs/en/config/dev/host.md'
|
12
|
+
|
13
|
+
<Main />
|
@@ -66,7 +66,7 @@ Also using the above configuration, after executing `pnpm run build`, only the e
|
|
66
66
|
```bash
|
67
67
|
.
|
68
68
|
├── src
|
69
|
-
│
|
69
|
+
│ ├── entryA
|
70
70
|
│ │ └── routes
|
71
71
|
│ │ ├── layout.tsx
|
72
72
|
│ │ ├── page.tsx
|
@@ -75,8 +75,8 @@ Also using the above configuration, after executing `pnpm run build`, only the e
|
|
75
75
|
│ │ ├── page.tsx
|
76
76
|
│ │ └── profile
|
77
77
|
│ │ └── page.tsx
|
78
|
-
│
|
79
|
-
│
|
78
|
+
│ └── entryB
|
79
|
+
│ └── App.tsx
|
80
80
|
```
|
81
81
|
|
82
82
|
By default, all entryA entrances are rendered at build time after setting `output.ssg` to `true`. You can configure `false` to cancel the default behavior of the specified entries. For example, to cancel the rendering of the `entryA` at build time:
|
@@ -7,7 +7,7 @@ sidebar_label: masterApp
|
|
7
7
|
- **Type:** `Object`
|
8
8
|
|
9
9
|
:::info
|
10
|
-
First you need to enable the "micro frontend" function using [new command](/apis/app/commands
|
10
|
+
First you need to enable the "micro frontend" function using [new command](/apis/app/commands#modern-new).
|
11
11
|
:::
|
12
12
|
|
13
13
|
## Example
|
@@ -9,7 +9,7 @@ sidebar_position: 1
|
|
9
9
|
- **Default:** `babel-jest`
|
10
10
|
|
11
11
|
:::caution Caution
|
12
|
-
First you need to enable the "Unit Test" function using [new](/apis/app/commands
|
12
|
+
First you need to enable the "Unit Test" function using [new](/apis/app/commands#modern-new) command.
|
13
13
|
|
14
14
|
:::
|
15
15
|
|
@@ -9,7 +9,7 @@ sidebar_label: jest
|
|
9
9
|
- **Default:** `{}`
|
10
10
|
|
11
11
|
:::caution Caution
|
12
|
-
First you need to enable the "Unit Test" function using [new](/apis/app/commands
|
12
|
+
First you need to enable the "Unit Test" function using [new](/apis/app/commands#modern-new) command.
|
13
13
|
|
14
14
|
:::
|
15
15
|
|
@@ -0,0 +1,69 @@
|
|
1
|
+
---
|
2
|
+
title: Rspack Start
|
3
|
+
sidebar_position: 12
|
4
|
+
---
|
5
|
+
|
6
|
+
# Rspack Start
|
7
|
+
|
8
|
+
[Rspack](https://www.rspack.org/) is a Rust-based web bundler developed by the ByteDance Web Infra team. The core architecture of Rspack is aligned with the implementation of webpack, and provides out-of-the-box support for commonly used build features.
|
9
|
+
|
10
|
+
Rspack optimizes compilation performance by:
|
11
|
+
|
12
|
+
- Highly LTO optimized Native code.
|
13
|
+
- Take full advantage of multi-core, and the entire compilation process is fully optimized for multi-threading.
|
14
|
+
- On-demand compilation based on request (Lazy Compilation), reducing the number of modules per compilation to improve the speed of cold start.
|
15
|
+
|
16
|
+
## Initializing an rspack project
|
17
|
+
|
18
|
+
The Modern.js generator provides an interactive question-and-answer interface to initialize a project. To create an rspack project, simply select the **rspack** build tool by running:
|
19
|
+
|
20
|
+
```bash
|
21
|
+
$ npx @modern-js/create myapp
|
22
|
+
? Please select the solution you want to create: Web App Solution
|
23
|
+
? Development Language: TS
|
24
|
+
? Package Management Tool: pnpm
|
25
|
+
? Build Tools: rspack
|
26
|
+
```
|
27
|
+
|
28
|
+
After the project is created, you can experience the project by running `pnpm run dev`. For more project information, please refer to [Quick Start](/guides/get-started/quick-start.html).
|
29
|
+
|
30
|
+
:::tip
|
31
|
+
When using rspack as the bundler, the following features are currently not supported:
|
32
|
+
|
33
|
+
- Server-side rendering (SSR)
|
34
|
+
- Static Site Generation (SSG)
|
35
|
+
- Micro Frontend.
|
36
|
+
- Storybook debugging.
|
37
|
+
|
38
|
+
:::
|
39
|
+
|
40
|
+
## Migrating from webpack to rspack
|
41
|
+
|
42
|
+
You can enable rspack build by running `pnpm run new`:
|
43
|
+
|
44
|
+
```bash
|
45
|
+
$ pnpm run new
|
46
|
+
? Action: Enable features
|
47
|
+
? Enable features: Enable rspack Build
|
48
|
+
```
|
49
|
+
|
50
|
+
After executing the command, enable the rspack build in `modern.config.ts`:
|
51
|
+
|
52
|
+
```ts title=modern.config.ts
|
53
|
+
import appTools, { defineConfig } from '@modern-js/app-tools';
|
54
|
+
|
55
|
+
export default defineConfig<'rspack'>({
|
56
|
+
runtime: {
|
57
|
+
router: true,
|
58
|
+
},
|
59
|
+
plugins: [
|
60
|
+
appTools({
|
61
|
+
bundler: 'experimental-rspack',
|
62
|
+
}),
|
63
|
+
],
|
64
|
+
});
|
65
|
+
```
|
66
|
+
|
67
|
+
:::tip
|
68
|
+
Migrating from webpack to rspack may have some differences in build and configuration capabilities. For more details, please refer to [Configuration Differences](https://modernjs.dev/builder/en/guide/advanced/rspack-start.html#migrating-from-webpack-to-rspack)
|
69
|
+
:::
|
@@ -37,14 +37,14 @@ For example, the following is a project directory structure using conventional r
|
|
37
37
|
```bash
|
38
38
|
.
|
39
39
|
├── src
|
40
|
-
│
|
41
|
-
│
|
42
|
-
│
|
43
|
-
│
|
44
|
-
│
|
45
|
-
│
|
46
|
-
│
|
47
|
-
│
|
40
|
+
│ └── routes
|
41
|
+
│ ├── layout.tsx
|
42
|
+
│ ├── page.tsx
|
43
|
+
│ └── user
|
44
|
+
│ ├── layout.tsx
|
45
|
+
│ ├── page.tsx
|
46
|
+
│ └── profile
|
47
|
+
│ └── page.tsx
|
48
48
|
```
|
49
49
|
|
50
50
|
The above file directory will generate the following three routes:
|
@@ -23,10 +23,10 @@ For example, import the modules from the `src/common/` directory in the `src/App
|
|
23
23
|
```bash
|
24
24
|
.
|
25
25
|
├── common
|
26
|
-
│
|
27
|
-
│
|
28
|
-
│
|
29
|
-
│
|
26
|
+
│ ├── styles
|
27
|
+
│ │ └── base.css
|
28
|
+
│ └── utils
|
29
|
+
│ └── index.ts
|
30
30
|
├── App.tsx
|
31
31
|
```
|
32
32
|
|
@@ -81,9 +81,9 @@ When a routing file is passed through `[]`, it is passed as a [dynamic route](/g
|
|
81
81
|
|
82
82
|
```tsx
|
83
83
|
// routes/user/[id]/page.loader.tsx
|
84
|
-
import {
|
84
|
+
import { LoaderFunctionArgs } from '@modern-js/runtime/router';
|
85
85
|
|
86
|
-
export default async ({ params }:
|
86
|
+
export default async ({ params }: LoaderFunctionArgs) => {
|
87
87
|
const { id } = params;
|
88
88
|
const res = await fetch(`https://api/user/${id}`);
|
89
89
|
return res.json();
|
@@ -100,9 +100,9 @@ A common usage scenario is to obtain query parameters via `request`:
|
|
100
100
|
|
101
101
|
```tsx
|
102
102
|
// routes/user/[id]/page.loader.ts
|
103
|
-
import {
|
103
|
+
import { LoaderFunctionArgs } from '@modern-js/runtime/router';
|
104
104
|
|
105
|
-
export default async ({ request }:
|
105
|
+
export default async ({ request }: LoaderFunctionArgs) => {
|
106
106
|
const url = new URL(request.url);
|
107
107
|
const userId = url.searchParams.get('id');
|
108
108
|
return queryUser(userId);
|
@@ -77,7 +77,7 @@ In order to facilitate the introduction of the relationship between `<Layout>` a
|
|
77
77
|
.
|
78
78
|
└── routes
|
79
79
|
├── blog
|
80
|
-
│
|
80
|
+
│ └── page.tsx
|
81
81
|
├── layout.tsx
|
82
82
|
├── page.tsx
|
83
83
|
└── user
|
@@ -124,9 +124,9 @@ With a file directory named `[]`, the generated route will be used as a dynamic
|
|
124
124
|
```
|
125
125
|
└── routes
|
126
126
|
├── [id]
|
127
|
-
│
|
127
|
+
│ └── page.tsx
|
128
128
|
├── blog
|
129
|
-
│
|
129
|
+
│ └── page.tsx
|
130
130
|
└── page.tsx
|
131
131
|
```
|
132
132
|
|
@@ -148,7 +148,7 @@ For example, the following directory structure:
|
|
148
148
|
└── routes
|
149
149
|
├── $.tsx
|
150
150
|
├── blog
|
151
|
-
│
|
151
|
+
│ └── page.tsx
|
152
152
|
└── page.tsx
|
153
153
|
```
|
154
154
|
|
@@ -215,9 +215,9 @@ When the component exists in the routing directory, all routing switches under t
|
|
215
215
|
.
|
216
216
|
└── routes
|
217
217
|
├── blog
|
218
|
-
│
|
219
|
-
│
|
220
|
-
│
|
218
|
+
│ ├── [id]
|
219
|
+
│ │ └── page.tsx
|
220
|
+
│ └── page.tsx
|
221
221
|
├── layout.tsx
|
222
222
|
├── loading.tsx
|
223
223
|
└── page.tsx
|
@@ -225,6 +225,22 @@ When the component exists in the routing directory, all routing switches under t
|
|
225
225
|
|
226
226
|
When a route jumps from `/` to `/blog`, if the JS Chunk of the `<Blog>` component is not loaded, the component UI exported in `loading.tsx` will be displayed first. But when jumping from `/blog` to `/blog/20220101`, it will not be displayed.
|
227
227
|
|
228
|
+
### Redirect
|
229
|
+
|
230
|
+
You can redirect routes by creating a [`data loader`](/guides/basic-features/data-fetch) file, Suppose you have the file `routes/user/page.tsx` and you want to redirect the route corresponding to this file, you can create the file `routes/user/page.loader.ts`:
|
231
|
+
|
232
|
+
```ts title="routes/user/page.loader.ts"
|
233
|
+
import { redirect } from "@edenx/runtime/router"
|
234
|
+
|
235
|
+
export default () => {
|
236
|
+
const user = await getUser();
|
237
|
+
if(!user){
|
238
|
+
return redirect('/login');
|
239
|
+
}
|
240
|
+
return null;
|
241
|
+
}
|
242
|
+
```
|
243
|
+
|
228
244
|
### ErrorBoundary
|
229
245
|
|
230
246
|
In each layer directory under `routes/`, the developer can also define a `error.tsx` file, and export a `<ErrorBoundary>` component by default.
|
@@ -14,7 +14,7 @@ From the perspective of building, Modern.js is divided into three layers, from t
|
|
14
14
|
|
15
15
|
- Upper-layer framework: Modern.js.
|
16
16
|
- Universal build engine: Modern.js Builder.
|
17
|
-
- Low-level bundlers: webpack and
|
17
|
+
- Low-level bundlers: webpack and rspack.
|
18
18
|
|
19
19
|
<img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/zq-uylkvT/ljhwZthlaukjlkulzlp/builder-layers-1117.png" style={{ maxWidth: '540px' }} />
|
20
20
|
|
@@ -129,9 +129,9 @@ The bundle is generated to `dist/` by default, and the directory structure is as
|
|
129
129
|
.
|
130
130
|
├── asset-manifest.json
|
131
131
|
├── html
|
132
|
-
│
|
132
|
+
│ └── main
|
133
133
|
├── loader-routes
|
134
|
-
│
|
134
|
+
│ └── main
|
135
135
|
├── modern.config.json
|
136
136
|
├── route.json
|
137
137
|
└── static
|