@modern-js/main-doc 0.0.0-nightly-20230918160602 → 0.0.0-nightly-20230922160540
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 +7 -6
- package/docs/en/components/debug-app.mdx +1 -3
- package/docs/en/components/global-proxy-config.mdx +4 -13
- package/docs/en/components/global-proxy.mdx +2 -4
- package/docs/en/components/init-app.mdx +1 -1
- package/docs/en/configure/app/server/base-url.mdx +0 -2
- package/docs/en/configure/app/source/entries.mdx +7 -6
- package/docs/en/guides/basic-features/alias.mdx +1 -1
- package/docs/en/guides/basic-features/css.mdx +1 -1
- package/docs/en/guides/basic-features/data-fetch.mdx +12 -12
- package/docs/en/guides/basic-features/routes.mdx +27 -15
- package/docs/en/guides/concept/entries.mdx +31 -30
- package/docs/en/guides/get-started/quick-start.mdx +8 -17
- package/docs/en/guides/get-started/upgrade.mdx +10 -4
- package/docs/en/guides/topic-detail/framework-plugin/implement.mdx +2 -0
- package/docs/en/tutorials/first-app/c02-component.mdx +4 -3
- package/docs/zh/apis/app/commands.mdx +7 -6
- package/docs/zh/components/debug-app.mdx +1 -3
- package/docs/zh/components/global-proxy-config.mdx +4 -13
- package/docs/zh/components/global-proxy.mdx +2 -4
- package/docs/zh/configure/app/server/base-url.mdx +0 -2
- package/docs/zh/configure/app/source/entries.mdx +7 -6
- package/docs/zh/guides/basic-features/alias.mdx +1 -1
- package/docs/zh/guides/basic-features/css.mdx +1 -1
- package/docs/zh/guides/basic-features/data-fetch.mdx +11 -12
- package/docs/zh/guides/basic-features/routes.mdx +26 -19
- package/docs/zh/guides/concept/entries.mdx +34 -32
- package/docs/zh/guides/get-started/quick-start.mdx +8 -17
- package/docs/zh/guides/get-started/upgrade.mdx +10 -4
- package/docs/zh/guides/topic-detail/framework-plugin/implement.mdx +2 -0
- package/docs/zh/tutorials/first-app/c02-component.mdx +4 -3
- package/package.json +7 -7
@@ -30,7 +30,6 @@ After running `modern dev`, Modern.js will watch source file changes and apply h
|
|
30
30
|
$ modern dev
|
31
31
|
|
32
32
|
info Starting dev server...
|
33
|
-
info App running at:
|
34
33
|
|
35
34
|
> Local: http://localhost:8080/
|
36
35
|
> Network: http://192.168.0.1:8080/
|
@@ -88,12 +87,14 @@ execute `npx modern build --analyze` command, can produce an HTML file that anal
|
|
88
87
|
|
89
88
|
```text
|
90
89
|
Bundle Analyzer saved report to /example/dist/report.html
|
91
|
-
File sizes after production build:
|
92
90
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
91
|
+
info Production file sizes:
|
92
|
+
|
93
|
+
File Size Gzipped
|
94
|
+
dist/static/js/lib-react.09721b5c.js 152.6 kB 49.0 kB
|
95
|
+
dist/html/main/index.html 5.8 kB 2.5 kB
|
96
|
+
dist/static/js/main.3568a38e.js 3.5 kB 1.4 kB
|
97
|
+
dist/static/css/main.03221f72.css 1.4 kB 741 B
|
97
98
|
```
|
98
99
|
|
99
100
|
Open the above HTML file in the browser, you can see the tile diagram of the bundled files, and perform package volume analysis and optimization:
|
@@ -6,12 +6,10 @@ $ pnpm run dev
|
|
6
6
|
> modern dev
|
7
7
|
|
8
8
|
info Starting dev server...
|
9
|
-
|
9
|
+
ready Client compiled in 50 ms
|
10
10
|
|
11
11
|
> Local: http://localhost:8080/
|
12
12
|
> Network: http://192.168.0.1:8080/
|
13
|
-
|
14
|
-
Client ✔ done in 76.10ms
|
15
13
|
```
|
16
14
|
|
17
15
|
Open `http://localhost:8000/` in your browser to see the page content.
|
@@ -7,16 +7,9 @@ This option is used to configure a global proxy based on [whistle](https://wprox
|
|
7
7
|
|
8
8
|
Before using this option, you need to install and register the `@modern-js/plugin-proxy` plugin:
|
9
9
|
|
10
|
-
|
11
|
-
# npm
|
12
|
-
npm add @modern-js/plugin-proxy -D
|
13
|
-
|
14
|
-
# yarn
|
15
|
-
yarn add @modern-js/plugin-proxy -D
|
10
|
+
import { PackageManagerTabs } from '@theme';
|
16
11
|
|
17
|
-
|
18
|
-
pnpm add @modern-js/plugin-proxy -D
|
19
|
-
```
|
12
|
+
<PackageManagerTabs command="add @modern-js/plugin-proxy -D" />
|
20
13
|
|
21
14
|
After the installation, please register the plugin in the `modern.config.ts` file:
|
22
15
|
|
@@ -75,13 +68,11 @@ module.exports = {
|
|
75
68
|
Execute `dev`, when the prompt is as follows, the proxy server starts successfully:
|
76
69
|
|
77
70
|
```bash
|
78
|
-
App running at:
|
79
|
-
|
80
71
|
Local: http://localhost:8080/
|
81
72
|
Network: http://192.168.0.1:8080/
|
82
73
|
|
83
|
-
|
84
|
-
|
74
|
+
info Starting proxy server.....
|
75
|
+
success Proxy server start on localhost:8899
|
85
76
|
```
|
86
77
|
|
87
78
|
Access the `localhost:8899` to view the Network and configure proxy rules on the UI interface:
|
@@ -11,13 +11,11 @@ Specific proxy rules can be set via the [`dev.proxy`](/configure/app/dev/proxy)
|
|
11
11
|
After installing the proxy plugin and configuring the proxy rules, run the `pnpm run dev` command:
|
12
12
|
|
13
13
|
```bash
|
14
|
-
App running at:
|
15
|
-
|
16
14
|
Local: http://localhost:8080/
|
17
15
|
Network: http://192.168.0.1:8080/
|
18
16
|
|
19
|
-
|
20
|
-
|
17
|
+
info Starting proxy server.....
|
18
|
+
success Proxy server start on localhost:8899
|
21
19
|
```
|
22
20
|
|
23
21
|
You can see that the proxy server has started successfully in the console.
|
@@ -14,7 +14,7 @@ After create the project, Modern.js will automatically install dependencies and
|
|
14
14
|
[INFO] git repository has been automatically created
|
15
15
|
[INFO] Success!
|
16
16
|
You can run the following command in the directory of the new project:
|
17
|
-
pnpm run dev # Starting
|
17
|
+
pnpm run dev # Starting dev server
|
18
18
|
pnpm run build # Build the app for production
|
19
19
|
pnpm run serve # Preview the production build locally
|
20
20
|
pnpm run lint # Run ESLint and automatically fix problems
|
@@ -39,8 +39,8 @@ import { defineConfig } from '@modern-js/app-tools';
|
|
39
39
|
export default defineConfig({
|
40
40
|
source: {
|
41
41
|
entries: {
|
42
|
-
// Specify a new entry named
|
43
|
-
|
42
|
+
// Specify a new entry named 'my-entry'
|
43
|
+
'my-entry': './src/home/test/index.ts',
|
44
44
|
},
|
45
45
|
disableDefaultEntries: true,
|
46
46
|
},
|
@@ -90,9 +90,10 @@ import { defineConfig } from '@modern-js/app-tools';
|
|
90
90
|
export default defineConfig({
|
91
91
|
source: {
|
92
92
|
entries: {
|
93
|
-
|
93
|
+
'my-entry': {
|
94
94
|
// entry file path
|
95
|
-
entry: './src/
|
95
|
+
entry: './src/my-page/index.tsx',
|
96
|
+
disableMount: true,
|
96
97
|
},
|
97
98
|
},
|
98
99
|
// Disable default entry scanning
|
@@ -111,8 +112,8 @@ If you want to disable the logic of Modern.js automatically generating entry fil
|
|
111
112
|
export default defineConfig({
|
112
113
|
source: {
|
113
114
|
entries: {
|
114
|
-
|
115
|
-
entry: './src/
|
115
|
+
'my-entry': {
|
116
|
+
entry: './src/my-page/index.tsx',
|
116
117
|
disableMount: true,
|
117
118
|
},
|
118
119
|
},
|
@@ -1,10 +1,11 @@
|
|
1
1
|
---
|
2
|
-
|
2
|
+
title: Data Fetching
|
3
|
+
sidebar_position: 3
|
3
4
|
---
|
4
5
|
|
5
6
|
# Data Fetching
|
6
7
|
|
7
|
-
Modern.js provides out-of-the-box data fetching capabilities,
|
8
|
+
Modern.js provides out-of-the-box data fetching capabilities,developers can fetch data in the project through these APIs.
|
8
9
|
|
9
10
|
It should be noted that these APIs do not help applications initiate requests, but rather help developers better manage data and improve project performance.
|
10
11
|
|
@@ -13,7 +14,7 @@ It should be noted that these APIs do not help applications initiate requests, b
|
|
13
14
|
Modern.js recommends using [conventional routing](/guides/basic-features/routes) for routing management. Through Modern.js's [conventional (nested) routing](/guides/basic-features/routes#conventional-routing), each routing component (`layout.ts` or `page.ts`) can have a same-named `loader` file. The `loader` file needs to export a function that will be executed before the component is rendered to provide data for the routing component.
|
14
15
|
|
15
16
|
:::info
|
16
|
-
Modern.js v1 supports fetching data via [useLoader](
|
17
|
+
Modern.js v1 supports fetching data via [useLoader](</guides/basic-features/data-fetch.html#useloader-(old-version)>), which is no longer the recommended usage. We do not recommend mixing the two except during the migration process.
|
17
18
|
|
18
19
|
:::
|
19
20
|
|
@@ -216,25 +217,24 @@ If you want to get the data returned by the `loader` in `entry1/routes/layout.ts
|
|
216
217
|
|
217
218
|
:::info
|
218
219
|
This feature is currently experimental and the API may change in the future.
|
219
|
-
Currently only supports CSR, please look forward to Streaming SSR.
|
220
220
|
:::
|
221
221
|
|
222
222
|
Create `user/layout.loader.ts` and add the following code:
|
223
223
|
|
224
224
|
```ts title="routes/user/layout.loader.ts"
|
225
|
-
import { defer } from
|
225
|
+
import { defer } from '@modern-js/runtime/router';
|
226
226
|
|
227
227
|
const loader = () =>
|
228
|
-
defer({
|
229
|
-
|
228
|
+
defer({
|
229
|
+
userInfo: new Promise(resolve => {
|
230
230
|
setTimeout(() => {
|
231
231
|
resolve({
|
232
232
|
age: 1,
|
233
|
-
name: 'user layout'
|
234
|
-
})
|
235
|
-
}, 1000)
|
236
|
-
})
|
237
|
-
})
|
233
|
+
name: 'user layout',
|
234
|
+
});
|
235
|
+
}, 1000);
|
236
|
+
}),
|
237
|
+
});
|
238
238
|
|
239
239
|
export default loader;
|
240
240
|
```
|
@@ -1,5 +1,5 @@
|
|
1
1
|
---
|
2
|
-
sidebar_position:
|
2
|
+
sidebar_position: 2
|
3
3
|
---
|
4
4
|
|
5
5
|
# Routing
|
@@ -140,6 +140,7 @@ To simplify the introduction of the relationship between `<Layout>` and `<Outlet
|
|
140
140
|
</UserLayout>
|
141
141
|
</Layout>
|
142
142
|
```
|
143
|
+
|
143
144
|
In summary, if there is a `layout.tsx` file under the sub-route's file directory, the `<Outlet>` in the parent `layout.tsx` will represent the `layout.tsx` in the sub-route file directory. Otherwise, it will represent the `page.tsx` in the sub-route file directory.
|
144
145
|
|
145
146
|
#### Page
|
@@ -148,12 +149,12 @@ All routes should end with the `<Page>` component. If the developer introduces t
|
|
148
149
|
|
149
150
|
#### Config
|
150
151
|
|
151
|
-
Each `Layout
|
152
|
+
Each `Layout`,`$` or `Page` file can define its own `config` file, such as `page.config.ts`. In this file, we have an conventinal on a named export called `handle`, which you can define any properties:
|
152
153
|
|
153
154
|
```ts title="routes/blog/page.config.ts"
|
154
155
|
export const handle = {
|
155
|
-
breadcrumbName: 'profile'
|
156
|
-
}
|
156
|
+
breadcrumbName: 'profile',
|
157
|
+
};
|
157
158
|
```
|
158
159
|
|
159
160
|
These properties as defined are available via the [`useMatches`](https://reactrouter.com/en/main/hooks/use-matches) hook:
|
@@ -161,12 +162,11 @@ These properties as defined are available via the [`useMatches`](https://reactro
|
|
161
162
|
```ts title="routes/layout.ts"
|
162
163
|
export default () => {
|
163
164
|
const matches = useMatches;
|
164
|
-
const breadcrumbs = matches.map(
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
}
|
165
|
+
const breadcrumbs = matches.map(
|
166
|
+
matchedRoute => matchedRoute?.handle?.breadcrumbName,
|
167
|
+
);
|
168
|
+
return <Breadcrumb names={breadcrumbs}></Breadcrumb>;
|
169
|
+
};
|
170
170
|
```
|
171
171
|
|
172
172
|
### Dynamic Routing
|
@@ -227,6 +227,7 @@ For example, the following directory structure:
|
|
227
227
|
```
|
228
228
|
|
229
229
|
When accessing any path that does not match(For example `/blog/a`), the `routes/$.tsx` component will be rendered, because there is `layout.tsx` here, the rendered UI is as follows.
|
230
|
+
|
230
231
|
```tsx
|
231
232
|
<RootLayout>
|
232
233
|
<BlogLayout>
|
@@ -364,6 +365,17 @@ export default () => {
|
|
364
365
|
};
|
365
366
|
```
|
366
367
|
|
368
|
+
If you want to redirect in a component,you can navigate by `useNavigate` hook, for example:
|
369
|
+
|
370
|
+
```ts title="routes/user/page.ts"
|
371
|
+
import { useNavigate } from '@modern-js/runtime/router';
|
372
|
+
|
373
|
+
export default () => {
|
374
|
+
const navigate = useNavigate();
|
375
|
+
navigate('/login');
|
376
|
+
};
|
377
|
+
```
|
378
|
+
|
367
379
|
### ErrorBoundary
|
368
380
|
|
369
381
|
In each directory under `routes/`, developers can also define an `error.tsx` file that exports an `<ErrorBoundary>` component by default.
|
@@ -391,7 +403,6 @@ export const ErrorBoundary = () => {
|
|
391
403
|
|
392
404
|
In each root `Layout` component (`routes/layout.ts`), you can dynamically define runtime configuration:
|
393
405
|
|
394
|
-
|
395
406
|
```tsx title="src/routes/layout.tsx"
|
396
407
|
// Define runtime config
|
397
408
|
import type { AppConfig } from '@modern-js/runtime';
|
@@ -489,6 +500,7 @@ To further improve the user experience and reduce loading time, Modern.js suppor
|
|
489
500
|
```
|
490
501
|
|
491
502
|
:::info
|
503
|
+
|
492
504
|
- This feature is currently only supported in Webpack projects and not yet supported in Rspack projects.
|
493
505
|
- Preloading data currently only preloads the data returned by the [Data Loader](/guides/basic-features/data-fetch) in SSR projects.
|
494
506
|
|
@@ -508,13 +520,13 @@ The `prefetch` attribute has three optional values:
|
|
508
520
|
- By using `render`, static assets will only be loaded when the system is idle, and will not compete with the static assets of the initial screen for network assets.
|
509
521
|
- In the SSR scenario, data will also be pre-fetched.
|
510
522
|
|
511
|
-
import Motivation from '@site-docs-en/components/convention-routing-motivation'
|
523
|
+
import Motivation from '@site-docs-en/components/convention-routing-motivation';
|
512
524
|
|
513
|
-
<Motivation/>
|
525
|
+
<Motivation />
|
514
526
|
|
515
|
-
import Practice from '@site-docs-en/components/routes-practice'
|
527
|
+
import Practice from '@site-docs-en/components/routes-practice';
|
516
528
|
|
517
|
-
<Practice/>
|
529
|
+
<Practice />
|
518
530
|
|
519
531
|
## Self-controlled Routing
|
520
532
|
|
@@ -32,7 +32,7 @@ The project initialized by Modern.js is a single entry (SPA) project, with the f
|
|
32
32
|
|
33
33
|
In a Modern.js project, you can easily switch from single entry to multiple entries by running `pnpm run new` in the project directory and creating an entry:
|
34
34
|
|
35
|
-
```
|
35
|
+
```text
|
36
36
|
? Please select the operation you want: Create Element
|
37
37
|
? Please select the type of element to create: New "entry"
|
38
38
|
? Please fill in the entry name: new-entry
|
@@ -56,7 +56,7 @@ After running the command, Modern.js will automatically generate a new entry dir
|
|
56
56
|
|
57
57
|
The original entry code has been moved to a directory with the same name as the `name` field in `package.json`, and a `new-entry` entry directory has been created.
|
58
58
|
|
59
|
-
|
59
|
+
You can run `pnpm run dev` to start the development server. At this point, you will see a new route named `/new-entry` added, and the existing page routes remain unchanged.
|
60
60
|
|
61
61
|
:::tip
|
62
62
|
Modern.js will use the entry with the same name as the `name` field in `package.json` as the main entry. The route of the main entry is `/`, and the route of other entries is `/{entryName}`.
|
@@ -76,7 +76,9 @@ import EntryMode from '@site-docs-en/components/entry-mode.mdx';
|
|
76
76
|
By default, Modern.js scans the files under `src/` before starting the project, identifies the entry, and generates the corresponding server-side route.
|
77
77
|
|
78
78
|
:::tip
|
79
|
-
|
79
|
+
|
80
|
+
- You can custom the recognition directory for page entries by using [source.entriesDir](/configure/app/source/entries-dir).
|
81
|
+
- If you need to customize the entry points, please refer to [Custom Entries](#custom-entries).
|
80
82
|
|
81
83
|
:::
|
82
84
|
|
@@ -98,7 +100,7 @@ When the project is not a single entry application, Modern.js will further look
|
|
98
100
|
|
99
101
|
### Framework Mode Entry
|
100
102
|
|
101
|
-
|
103
|
+
The framework mode refers to the need to use the framework capabilities of Modern.js, such as nested routing, SSR, and integrated BFF, etc. Under this kind of entry convention, the entry defined by the developer is not the actual compilation entry. When Modern.js is launched, it generates a wrapped entry, and the real entry can be found at `node_modules/.modern/[entryName]/index.js`.
|
102
104
|
|
103
105
|
#### Conventional Routing
|
104
106
|
|
@@ -146,7 +148,7 @@ export default (App: React.ComponentType, bootstrap: () => void) => {
|
|
146
148
|
// do something before bootstrap...
|
147
149
|
initSomething().then(() => {
|
148
150
|
bootstrap();
|
149
|
-
})
|
151
|
+
});
|
150
152
|
};
|
151
153
|
```
|
152
154
|
|
@@ -181,9 +183,11 @@ export default AppWrapper;
|
|
181
183
|
|
182
184
|
### Build Mode Entry
|
183
185
|
|
184
|
-
Build mode refers to
|
186
|
+
Build mode refers to the mode where the entry point of the page is not automatically generated by Modern.js, but is fully defined by the developers themselves.
|
187
|
+
|
188
|
+
When there is an `index.[jt]sx` file in the entry directory and it is not exported as a function using `export default`, this type of file will be recognized as the entry module for webpack or Rspack.
|
185
189
|
|
186
|
-
|
190
|
+
In this case, Modern.js will not generate the entry code automatically. Therefore, you need to manually mount the component to the DOM node, for example:
|
187
191
|
|
188
192
|
```js title=src/index.jsx
|
189
193
|
import React from 'react';
|
@@ -193,42 +197,39 @@ import App from './App';
|
|
193
197
|
ReactDOM.render(<App />, document.getElementById('root'));
|
194
198
|
```
|
195
199
|
|
196
|
-
Modern.js
|
200
|
+
This approach is equivalent to enabling the [source.entries.disableMount](/configure/app/source/entries) option in Modern.js. When you use this approach, **you will not be able to use the runtime capabilities of the Modern.js framework**, such as the `runtime` configuration in the modern.config.js file will no longer take effect.
|
197
201
|
|
198
|
-
##
|
202
|
+
## Custom Entries
|
199
203
|
|
200
|
-
|
204
|
+
In some cases, you may need to customize the entry configuration instead of using the entry conventions provided by Modern.js.
|
201
205
|
|
202
|
-
|
206
|
+
For example, if you want to migrate a non-Modern.js project to Modern.js and it is not structured according to Modern.js directory structure, there might be some migration costs involved in changing it to the conventional structure. In such cases, you can custom the entries.
|
203
207
|
|
204
|
-
|
205
|
-
export default defineConfig({
|
206
|
-
source: {
|
207
|
-
entries: {
|
208
|
-
// Specify a new entry named entry_customize
|
209
|
-
entry_customize: './src/home/test/index.ts',
|
210
|
-
},
|
211
|
-
// Disable default ingress scanning
|
212
|
-
disableDefaultEntries: true,
|
213
|
-
},
|
214
|
-
});
|
215
|
-
```
|
208
|
+
Modern.js provides the following configuration options that you can set in [modern.config.ts](/configure/app/usage):
|
216
209
|
|
217
|
-
|
210
|
+
- [source.entries](/configure/app/source/entries): Used to set custom entry objects.
|
211
|
+
- [source.disableDefaultEntries](/configure/app/source/disable-default-entries): Used to disable Modern.js's default entry scanning behavior. When you use custom entries, parts of your project structure might coincidentally match the Modern.js conventional directory structure, but you may not want Modern.js to generate entry configurations for them. Enabling this option can help avoid this issue.
|
218
212
|
|
219
|
-
|
213
|
+
### Example
|
220
214
|
|
221
|
-
|
215
|
+
Here is an example of a custom entry point. You can also refer to the documentation of the corresponding configuration options for more usage.
|
222
216
|
|
223
217
|
```ts title="modern.config.ts"
|
224
218
|
export default defineConfig({
|
225
219
|
source: {
|
220
|
+
entries: {
|
221
|
+
// Specify an entry named 'my-entry'
|
222
|
+
'my-entry': {
|
223
|
+
// Path to the entry module
|
224
|
+
entry: './src/my-page/index.tsx',
|
225
|
+
// Disable automatic generation of entry code by Modern.js
|
226
|
+
disableMount: true,
|
227
|
+
},
|
228
|
+
},
|
229
|
+
// Disable entry scanning behavior
|
226
230
|
disableDefaultEntries: true,
|
227
231
|
},
|
228
232
|
});
|
229
233
|
```
|
230
234
|
|
231
|
-
|
232
|
-
For detailed usage, please refer to [source.entries](/configure/app/source/entries) and [source.disableDefaultEntries](/configure/app/source/disable-default-entries).
|
233
|
-
|
234
|
-
:::
|
235
|
+
Note that when you enable `disableMount`, **you won't be able to use the runtime capabilities of the Modern.js framework**, such as the `runtime` configuration in the modern.config.ts file.
|
@@ -91,23 +91,15 @@ $ pnpm run build
|
|
91
91
|
|
92
92
|
> modern build
|
93
93
|
|
94
|
-
info
|
95
|
-
|
96
|
-
info
|
94
|
+
info Staring production build...
|
95
|
+
ready Client compiled in 50 ms
|
96
|
+
info Production file sizes:
|
97
97
|
|
98
98
|
File Size Gzipped
|
99
|
-
dist/static/js/lib-
|
100
|
-
dist/
|
101
|
-
dist/static/js/
|
102
|
-
dist/static/
|
103
|
-
dist/html/main/index.html 5.84 kB 2.57 kB
|
104
|
-
dist/static/js/main.3568a38e.js 3.57 kB 1.44 kB
|
105
|
-
dist/static/css/async/304.c3c481a5.css 2.62 kB 874 B
|
106
|
-
dist/asset-manifest.json 1.48 kB 349 B
|
107
|
-
dist/static/js/async/304.c45706bc.js 1.4 kB 575 B
|
108
|
-
dist/static/js/async/509.fcb06e14.js 283 B 230 B
|
109
|
-
|
110
|
-
Client ✔ done in 3.57s
|
99
|
+
dist/static/js/lib-react.09721b5c.js 152.6 kB 49.0 kB
|
100
|
+
dist/html/main/index.html 5.8 kB 2.5 kB
|
101
|
+
dist/static/js/main.3568a38e.js 3.5 kB 1.4 kB
|
102
|
+
dist/static/css/main.03221f72.css 1.4 kB 741 B
|
111
103
|
```
|
112
104
|
|
113
105
|
By default, the build artifacts are generated in `dist/`, with the following directory structure:
|
@@ -135,8 +127,7 @@ $ pnpm run serve
|
|
135
127
|
|
136
128
|
> modern serve
|
137
129
|
|
138
|
-
Starting
|
139
|
-
info App running at:
|
130
|
+
info Starting production server...
|
140
131
|
|
141
132
|
> Local: http://localhost:8080/
|
142
133
|
> Network: http://192.168.0.1:8080/
|
@@ -8,11 +8,13 @@ sidebar_position: 3
|
|
8
8
|
|
9
9
|
Modern.js provides the `upgrade` command to support upgrading the project to the latest version of Modern.js.
|
10
10
|
|
11
|
-
Run `
|
11
|
+
Run the `upgrade` script in the project:
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
import { PackageManagerTabs } from '@theme';
|
14
|
+
|
15
|
+
<PackageManagerTabs command="run upgrade" />
|
15
16
|
|
17
|
+
```bash
|
16
18
|
> modern upgrade
|
17
19
|
|
18
20
|
[INFO] [Project Type]: Web App
|
@@ -22,11 +24,15 @@ $ pnpm run upgrade
|
|
22
24
|
|
23
25
|
You can see that the dependencies in the project's `package.json` have been updated to the latest version.
|
24
26
|
|
27
|
+
:::tip
|
28
|
+
If the `upgrade` command is not declared in the project's `package.json`, you can execute `npx modern upgrade` as an equivalent alternative.
|
29
|
+
:::
|
30
|
+
|
25
31
|
## Upgrade to a specified version
|
26
32
|
|
27
33
|
All packages of Modern.js are currently released with a **uniform version number**.
|
28
34
|
|
29
|
-
import ReleaseNote from
|
35
|
+
import ReleaseNote from '@site-docs-en/components/release-note';
|
30
36
|
|
31
37
|
<ReleaseNote />
|
32
38
|
|
@@ -170,6 +170,8 @@ export const myPlugin = (): CliPlugin => ({
|
|
170
170
|
});
|
171
171
|
```
|
172
172
|
|
173
|
+
Note that the setup function of the next plugin is not executed until the async setup function of the current plugin has finished. Therefore, you should avoid performing time-consuming asynchronous operations in the setup function to avoid slowing down the startup performance of the CLI.
|
174
|
+
|
173
175
|
## Adding Plugins
|
174
176
|
|
175
177
|
Custom plugins can be used by following the instructions in the [plugins](/configure/app/plugins) section of the documentation. Below is the recommended way to implement plugins in Modern.js.
|
@@ -1,6 +1,7 @@
|
|
1
1
|
---
|
2
2
|
title: Add UI Components
|
3
3
|
---
|
4
|
+
|
4
5
|
# Add UI Components
|
5
6
|
|
6
7
|
In the previous chapter, we learned how to initialize a project and use configuration to modify the default behavior of Modern.js.
|
@@ -9,9 +10,9 @@ In this chapter, we continue to use the project code of the previous chapter and
|
|
9
10
|
|
10
11
|
In order to do better UI display and interaction, we introduce the component library [Antd](https://ant.design/index-cn) to develop, and use the `<List>` component instead of the primitive list. Add dependency first:
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
import { PackageManagerTabs } from '@theme';
|
14
|
+
|
15
|
+
<PackageManagerTabs command="add antd" />
|
15
16
|
|
16
17
|
Modify `src/routes/page.tsx` to import components at the top:
|
17
18
|
|
@@ -30,7 +30,6 @@ Options:
|
|
30
30
|
$ modern dev
|
31
31
|
|
32
32
|
info Starting dev server...
|
33
|
-
info App running at:
|
34
33
|
|
35
34
|
> Local: http://localhost:8080/
|
36
35
|
> Network: http://192.168.0.1:8080/
|
@@ -88,12 +87,14 @@ Options:
|
|
88
87
|
|
89
88
|
```
|
90
89
|
Bundle Analyzer saved report to /example/dist/report.html
|
91
|
-
File sizes after production build:
|
92
90
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
91
|
+
info Production file sizes:
|
92
|
+
|
93
|
+
File Size Gzipped
|
94
|
+
dist/static/js/lib-react.09721b5c.js 152.6 kB 49.0 kB
|
95
|
+
dist/html/main/index.html 5.8 kB 2.5 kB
|
96
|
+
dist/static/js/main.3568a38e.js 3.5 kB 1.4 kB
|
97
|
+
dist/static/css/main.03221f72.css 1.4 kB 741 B
|
97
98
|
```
|
98
99
|
|
99
100
|
手动在浏览器中打开上述 HTML 文件,可以看到打包产物的瓦片图,并进行包体积分析和优化:
|
@@ -6,12 +6,10 @@ $ pnpm run dev
|
|
6
6
|
> modern dev
|
7
7
|
|
8
8
|
info Starting dev server...
|
9
|
-
|
9
|
+
ready Client compiled in 50 ms
|
10
10
|
|
11
11
|
> Local: http://localhost:8080/
|
12
12
|
> Network: http://192.168.0.1:8080/
|
13
|
-
|
14
|
-
Client ✔ done in 76.10ms
|
15
13
|
```
|
16
14
|
|
17
15
|
在浏览器中打开 `http://localhost:8000/`,可以看到页面内容。
|
@@ -7,16 +7,9 @@
|
|
7
7
|
|
8
8
|
使用该选项前,你需要提前安装和注册 `@modern-js/plugin-proxy` 插件:
|
9
9
|
|
10
|
-
|
11
|
-
# npm
|
12
|
-
npm add @modern-js/plugin-proxy -D
|
13
|
-
|
14
|
-
# yarn
|
15
|
-
yarn add @modern-js/plugin-proxy -D
|
10
|
+
import { PackageManagerTabs } from '@theme';
|
16
11
|
|
17
|
-
|
18
|
-
pnpm add @modern-js/plugin-proxy -D
|
19
|
-
```
|
12
|
+
<PackageManagerTabs command="add @modern-js/plugin-proxy -D" />
|
20
13
|
|
21
14
|
安装完成后,在 `modern.config.ts` 文件中注册插件:
|
22
15
|
|
@@ -75,13 +68,11 @@ module.exports = {
|
|
75
68
|
执行 `dev`, 提示如下时,即代理服务器启动成功:
|
76
69
|
|
77
70
|
```bash
|
78
|
-
App running at:
|
79
|
-
|
80
71
|
Local: http://localhost:8080/
|
81
72
|
Network: http://192.168.0.1:8080/
|
82
73
|
|
83
|
-
|
84
|
-
|
74
|
+
info Starting proxy server.....
|
75
|
+
success Proxy server start on localhost:8899
|
85
76
|
```
|
86
77
|
|
87
78
|
访问 `localhost:8899`, 可以在 UI 界面上查看 Network 以及配置代理规则:
|
@@ -11,13 +11,11 @@ Modern.js 提供了开箱即用的全局代理插件 `@modern-js/plugin-proxy`
|
|
11
11
|
安装代理插件并配置代理规则后, 执行 `pnpm run dev` 命令:
|
12
12
|
|
13
13
|
```bash
|
14
|
-
App running at:
|
15
|
-
|
16
14
|
Local: http://localhost:8080/
|
17
15
|
Network: http://192.168.0.1:8080/
|
18
16
|
|
19
|
-
|
20
|
-
|
17
|
+
info Starting proxy server.....
|
18
|
+
success Proxy server start on localhost:8899
|
21
19
|
```
|
22
20
|
|
23
21
|
在控制台中可以看到代理服务器成功启动。
|
@@ -39,8 +39,8 @@ import { defineConfig } from '@modern-js/app-tools';
|
|
39
39
|
export default defineConfig({
|
40
40
|
source: {
|
41
41
|
entries: {
|
42
|
-
// 指定一个名称为
|
43
|
-
|
42
|
+
// 指定一个名称为 'my-entry' 的新入口
|
43
|
+
'my-entry': './src/home/test/index.ts',
|
44
44
|
},
|
45
45
|
// 禁用默认入口扫描
|
46
46
|
disableDefaultEntries: true,
|
@@ -92,9 +92,10 @@ import { defineConfig } from '@modern-js/app-tools';
|
|
92
92
|
export default defineConfig({
|
93
93
|
source: {
|
94
94
|
entries: {
|
95
|
-
|
95
|
+
'my-entry': {
|
96
96
|
// 入口文件路径
|
97
|
-
entry: './src/
|
97
|
+
entry: './src/my-page/index.tsx',
|
98
|
+
disableMount: true,
|
98
99
|
},
|
99
100
|
},
|
100
101
|
// 禁用默认入口扫描
|
@@ -113,8 +114,8 @@ export default defineConfig({
|
|
113
114
|
export default defineConfig({
|
114
115
|
source: {
|
115
116
|
entries: {
|
116
|
-
|
117
|
-
entry: './src/
|
117
|
+
'my-entry': {
|
118
|
+
entry: './src/my-page/index.tsx',
|
118
119
|
disableMount: true,
|
119
120
|
},
|
120
121
|
},
|
@@ -1,10 +1,11 @@
|
|
1
1
|
---
|
2
|
-
|
2
|
+
title: 数据获取
|
3
|
+
sidebar_position: 3
|
3
4
|
---
|
4
5
|
|
5
6
|
# 数据获取
|
6
7
|
|
7
|
-
Modern.js 中提供了开箱即用的数据获取能力,开发者可以通过这些 API
|
8
|
+
Modern.js 中提供了开箱即用的数据获取能力,开发者可以通过这些 API,在项目中获取数据。
|
8
9
|
|
9
10
|
需要注意的是,这些 API 并不帮助应用发起请求,而是帮助开发者更好地管理数据,提升项目的性能。
|
10
11
|
|
@@ -216,26 +217,25 @@ export default function UserLayout() {
|
|
216
217
|
|
217
218
|
:::info
|
218
219
|
此功能目前是实验性质,后续 API 可能有调整。
|
219
|
-
目前仅支持 CSR,敬请期待 Streaming SSR。
|
220
220
|
|
221
221
|
:::
|
222
222
|
|
223
223
|
创建 `user/layout.loader.ts`,并添加以下代码:
|
224
224
|
|
225
225
|
```ts title="routes/user/layout.loader.ts"
|
226
|
-
import { defer } from
|
226
|
+
import { defer } from '@modern-js/runtime/router';
|
227
227
|
|
228
228
|
const loader = () =>
|
229
|
-
defer({
|
230
|
-
|
229
|
+
defer({
|
230
|
+
userInfo: new Promise(resolve => {
|
231
231
|
setTimeout(() => {
|
232
232
|
resolve({
|
233
233
|
age: 1,
|
234
|
-
name: 'user layout'
|
235
|
-
})
|
236
|
-
}, 1000)
|
237
|
-
})
|
238
|
-
})
|
234
|
+
name: 'user layout',
|
235
|
+
});
|
236
|
+
}, 1000);
|
237
|
+
}),
|
238
|
+
});
|
239
239
|
|
240
240
|
export default loader;
|
241
241
|
```
|
@@ -346,7 +346,6 @@ export default async (): Promise<ProfileData> => {
|
|
346
346
|
|
347
347
|
在 SSR 项目中,每个 `loader` 也是一个服务端接口,我们推荐使用 `loader` 替代 http method 为 `get` 的 BFF 函数,作为接口层,避免多一层转发和执行。
|
348
348
|
|
349
|
-
|
350
349
|
## useLoader(旧版)
|
351
350
|
|
352
351
|
**`useLoader`** 是 Modern.js 老版本中的 API。该 API 是一个 React Hook,专门提供给 SSR 应用使用,让开发者能同构的在组件中获取数据。
|
@@ -1,5 +1,5 @@
|
|
1
1
|
---
|
2
|
-
sidebar_position:
|
2
|
+
sidebar_position: 2
|
3
3
|
---
|
4
4
|
|
5
5
|
# 路由方案
|
@@ -151,13 +151,13 @@ export default () => {
|
|
151
151
|
|
152
152
|
#### Config
|
153
153
|
|
154
|
-
每个 `Layout
|
154
|
+
每个 `Layout`, `$` 或 `Page` 文件都可以定义一个自己的 `config` 文件,如 `page.config.ts`,该文件中我们约定了一个具名导出 `handle`,
|
155
155
|
这个字段中你可以定义任意属性:
|
156
156
|
|
157
157
|
```ts title="routes/page.config.ts"
|
158
158
|
export const handle = {
|
159
|
-
breadcrumbName: 'profile'
|
160
|
-
}
|
159
|
+
breadcrumbName: 'profile',
|
160
|
+
};
|
161
161
|
```
|
162
162
|
|
163
163
|
定义的这些属性可以通过 [`useMatches`](https://reactrouter.com/en/main/hooks/use-matches) hook 获取:
|
@@ -165,15 +165,13 @@ export const handle = {
|
|
165
165
|
```ts title="routes/layout.ts"
|
166
166
|
export default () => {
|
167
167
|
const matches = useMatches;
|
168
|
-
const breadcrumbs = matches.map(
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
}
|
168
|
+
const breadcrumbs = matches.map(
|
169
|
+
matchedRoute => matchedRoute?.handle?.breadcrumbName,
|
170
|
+
);
|
171
|
+
return <Breadcrumb names={breadcrumbs}></Breadcrumb>;
|
172
|
+
};
|
174
173
|
```
|
175
174
|
|
176
|
-
|
177
175
|
### 动态路由
|
178
176
|
|
179
177
|
通过 `[]` 命名的文件目录,生成的路由会作为动态路由。例如以下文件目录:
|
@@ -233,6 +231,7 @@ export default () => {
|
|
233
231
|
```
|
234
232
|
|
235
233
|
当访问任何匹配不到的路径时(如 `/blog/a`),都会渲染 `routes/$.tsx` 组件,因为这里有 `layout.tsx`,渲染的 UI 如下:
|
234
|
+
|
236
235
|
```tsx
|
237
236
|
<RootLayout>
|
238
237
|
<BlogLayout>
|
@@ -254,7 +253,6 @@ params['*']; // => 'aaa/bbb'
|
|
254
253
|
|
255
254
|
`$.tsx` 可以加入到 `routes` 目录下的任意目录中,一个常见的使用示例是添加 `routes/$.tsx` 文件去定制任意层级的 404 内容。
|
256
255
|
|
257
|
-
|
258
256
|
### 无路径布局
|
259
257
|
|
260
258
|
当目录名以 \_\_ 开头时,对应的目录名不会转换为实际的路由路径,例如以下文件目录:
|
@@ -368,6 +366,17 @@ export default () => {
|
|
368
366
|
};
|
369
367
|
```
|
370
368
|
|
369
|
+
在组件内做重定向,则可以通过 `useNavigate` hook,示例如下:
|
370
|
+
|
371
|
+
```ts title="routes/user/page.ts"
|
372
|
+
import { useNavigate } from '@modern-js/runtime/router';
|
373
|
+
|
374
|
+
export default () => {
|
375
|
+
const navigate = useNavigate();
|
376
|
+
navigate('/login');
|
377
|
+
};
|
378
|
+
```
|
379
|
+
|
371
380
|
### 错误处理
|
372
381
|
|
373
382
|
`routes/` 下每一层目录中,开发者同样可以定义一个 `error.tsx` 文件,默认导出一个 `<ErrorBoundary>` 组件。
|
@@ -493,6 +502,7 @@ export const init = (context: RuntimeContext) => {
|
|
493
502
|
```
|
494
503
|
|
495
504
|
:::info
|
505
|
+
|
496
506
|
- 该功能目前仅在 Webpack 项目中支持,Rspack 项目暂不支持。
|
497
507
|
- 对数据的预加载目前只会预加载 SSR 项目中 [Data Loader](/guides/basic-features/data-fetch) 中返回的数据。
|
498
508
|
|
@@ -512,14 +522,13 @@ export const init = (context: RuntimeContext) => {
|
|
512
522
|
- 使用 `render`,仅在空闲时对静态资源进行加载,不会与首屏静态资源抢占网络。
|
513
523
|
- 在 SSR 场景下,也会对数据进行预取。
|
514
524
|
|
515
|
-
import Motivation from '@site-docs/components/convention-routing-motivation'
|
516
|
-
|
517
|
-
<Motivation/>
|
525
|
+
import Motivation from '@site-docs/components/convention-routing-motivation';
|
518
526
|
|
519
|
-
|
527
|
+
<Motivation />
|
520
528
|
|
521
|
-
|
529
|
+
import Practice from '@site-docs/components/routes-practice';
|
522
530
|
|
531
|
+
<Practice />
|
523
532
|
|
524
533
|
## 自控式路由
|
525
534
|
|
@@ -569,5 +578,3 @@ export default defineConfig({
|
|
569
578
|
```
|
570
579
|
|
571
580
|
如上述配置, 如果关闭了 [`runtime.router`](/configure/app/runtime/router) 配置,并直接使用 `react-router-dom` 进行项目路由管理时,还需要根据 React Router 文档自行包裹 `Provider`。
|
572
|
-
|
573
|
-
|
@@ -6,9 +6,9 @@ sidebar_position: 1
|
|
6
6
|
|
7
7
|
通过本章节,你可以了解到 Modern.js 中的入口约定,以及如何自定义入口。
|
8
8
|
|
9
|
-
##
|
9
|
+
## 什么是入口
|
10
10
|
|
11
|
-
|
11
|
+
**入口(Entry)指的是一个页面的起始模块。**
|
12
12
|
|
13
13
|
在 Modern.js 项目中,每一个入口对应一个独立的页面,也对应一条服务端路由。默认情况下,Modern.js 会基于目录约定来自动确定页面的入口,同时也支持通过配置项来自定义入口。
|
14
14
|
|
@@ -56,7 +56,7 @@ Modern.js 初始化的项目是单入口的(SPA),项目结构如下:
|
|
56
56
|
|
57
57
|
原本的入口代码被移动到了和 `package.json` 中 `name` 同名的目录下,并创建了 `new-entry` 入口目录。
|
58
58
|
|
59
|
-
|
59
|
+
你可以执行 `pnpm run dev` 启动开发服务,此时可以看到新增了一条名为 `/new-entry` 的路由,并且原有页面的路由并未发生变化。
|
60
60
|
|
61
61
|
:::tip
|
62
62
|
Modern.js 会将与 `package.json` 文件中 `name` 字段同名的入口作为主入口,主入口的路由为 `/`,其他入口的路由为 `/{entryName}`。
|
@@ -76,7 +76,8 @@ import EntryMode from '@site-docs/components/entry-mode.mdx';
|
|
76
76
|
默认情况下,Modern.js 启动项目前会对 `src/` 下的文件进行扫描,识别入口,并生成对应的服务端路由。
|
77
77
|
|
78
78
|
:::tip
|
79
|
-
|
79
|
+
- 你可以通过 [source.entriesDir](/configure/app/source/entries-dir) 修改页面入口的识别目录。
|
80
|
+
- 如果你需要自定义入口,请参考 [自定义入口](#自定义入口)。
|
80
81
|
|
81
82
|
:::
|
82
83
|
|
@@ -98,7 +99,7 @@ import EntryMode from '@site-docs/components/entry-mode.mdx';
|
|
98
99
|
|
99
100
|
### 框架模式入口
|
100
101
|
|
101
|
-
|
102
|
+
框架模式指的是需要使用 Modern.js 的框架能力,例如嵌套路由、SSR、一体化调用等。这类入口约定下,开发者定义的入口并不是真正的编译入口。Modern.js 在启动时会生成一个封装过的入口,可以在 `node_modules/.modern/[entryName]/index.js` 找到真正的入口。
|
102
103
|
|
103
104
|
#### 约定式路由
|
104
105
|
|
@@ -146,7 +147,7 @@ export default (App: React.ComponentType, bootstrap: () => void) => {
|
|
146
147
|
// do something before bootstrap...
|
147
148
|
initSomething().then(() => {
|
148
149
|
bootstrap();
|
149
|
-
})
|
150
|
+
});
|
150
151
|
};
|
151
152
|
```
|
152
153
|
|
@@ -170,7 +171,9 @@ function render() {
|
|
170
171
|
// runtime 的插件参数...
|
171
172
|
})(App);
|
172
173
|
if (IS_BROWSER) {
|
173
|
-
customBootstrap(AppWrapper, () =>
|
174
|
+
customBootstrap(AppWrapper, () =>
|
175
|
+
bootstrap(AppWrapper, MOUNT_ID, root, ReactDOM),
|
176
|
+
);
|
174
177
|
}
|
175
178
|
return AppWrapper;
|
176
179
|
}
|
@@ -182,9 +185,11 @@ export default AppWrapper;
|
|
182
185
|
|
183
186
|
### 构建模式入口
|
184
187
|
|
185
|
-
|
188
|
+
构建模式指的是不使用 Modern.js 自动生成的入口,而是完全由开发者自行定义页面的入口。
|
186
189
|
|
187
|
-
|
190
|
+
当入口目录中存在 `index.[jt]sx`,并且没有通过 `export default` 导出函数时,这类文件就会被识别为 webpack 或 Rspack 的 entry 模块。
|
191
|
+
|
192
|
+
此时 Modern.js 不会自动生成入口代码,因此需要你自行将组件挂载到 DOM 节点上,例如:
|
188
193
|
|
189
194
|
```js title=src/index.jsx
|
190
195
|
import React from 'react';
|
@@ -194,42 +199,39 @@ import App from './App';
|
|
194
199
|
ReactDOM.render(<App />, document.getElementById('root'));
|
195
200
|
```
|
196
201
|
|
197
|
-
Modern.js
|
202
|
+
这种方式等价于开启 Modern.js 的 [source.entries.disableMount](/configure/app/source/entries) 选项。当你使用这种方式时,**将无法使用 Modern.js 框架的运行时能力**,比如 modern.config.js 文件中的 `runtime` 配置将不会再生效。
|
198
203
|
|
199
|
-
##
|
204
|
+
## 自定义入口
|
200
205
|
|
201
|
-
|
206
|
+
在某些情况下,你可能需要自定义入口配置,而不是使用 Modern.js 提供的入口约定。
|
202
207
|
|
203
|
-
|
208
|
+
比如你需要将一个非 Modern.js 项目迁移到 Modern.js,它并不是按照 Modern.js 的目录结构来搭建的。如果你要将它改成 Modern.js 约定的目录结构,可能会存在一定的迁移成本。这种情况下,你就可以使用自定义入口。
|
204
209
|
|
205
|
-
|
206
|
-
export default defineConfig({
|
207
|
-
source: {
|
208
|
-
entries: {
|
209
|
-
// 指定一个名称为 entry_customize 的新入口
|
210
|
-
entry_customize: './src/home/test/index.ts',
|
211
|
-
},
|
212
|
-
// 禁用默认入口扫描
|
213
|
-
disableDefaultEntries: true,
|
214
|
-
},
|
215
|
-
});
|
216
|
-
```
|
210
|
+
Modern.js 提供了以下配置项,你可以在 [modern.config.ts](/configure/app/usage) 中配置它们:
|
217
211
|
|
218
|
-
|
212
|
+
- [source.entries](/configure/app/source/entries):用于设置自定义的入口对象。
|
213
|
+
- [source.disableDefaultEntries](/configure/app/source/disable-default-entries):用于关闭 Modern.js 默认的入口扫描行为。当你使用自定义入口时,项目的部分结构可能会恰巧命中 Modern.js 约定的目录结构,但你可能不希望 Modern.js 为你自动生成入口配置,开启该选项可以避免这个问题。
|
219
214
|
|
220
|
-
|
215
|
+
### 示例
|
221
216
|
|
222
|
-
|
217
|
+
下面是一个自定义入口的例子,你也可以查看相关配置项的文档来了解更多用法。
|
223
218
|
|
224
219
|
```ts title="modern.config.ts"
|
225
220
|
export default defineConfig({
|
226
221
|
source: {
|
222
|
+
entries: {
|
223
|
+
// 指定一个名为 'my-entry' 的入口
|
224
|
+
'my-entry': {
|
225
|
+
// 入口模块的路径
|
226
|
+
entry: './src/my-page/index.tsx',
|
227
|
+
// 关闭 Modern.js 自动生成入口代码的行为
|
228
|
+
disableMount: true,
|
229
|
+
},
|
230
|
+
},
|
231
|
+
// 禁用入口扫描行为
|
227
232
|
disableDefaultEntries: true,
|
228
233
|
},
|
229
234
|
});
|
230
235
|
```
|
231
236
|
|
232
|
-
|
233
|
-
详细用法请查看 [source.entries](/configure/app/source/entries) 和 [source.disableDefaultEntries](/configure/app/source/disable-default-entries)。
|
234
|
-
|
235
|
-
:::
|
237
|
+
注意,当你开启 `disableMount` 时,**将无法使用 Modern.js 框架的运行时能力**,比如 modern.config.ts 文件中的 `runtime` 配置将不会再生效。
|
@@ -91,23 +91,15 @@ $ pnpm run build
|
|
91
91
|
|
92
92
|
> modern build
|
93
93
|
|
94
|
-
info
|
95
|
-
|
96
|
-
info
|
94
|
+
info Staring production build...
|
95
|
+
ready Client compiled in 50 ms
|
96
|
+
info Production file sizes:
|
97
97
|
|
98
98
|
File Size Gzipped
|
99
|
-
dist/static/js/lib-
|
100
|
-
dist/
|
101
|
-
dist/static/js/
|
102
|
-
dist/static/
|
103
|
-
dist/html/main/index.html 5.84 kB 2.57 kB
|
104
|
-
dist/static/js/main.3568a38e.js 3.57 kB 1.44 kB
|
105
|
-
dist/static/css/async/304.c3c481a5.css 2.62 kB 874 B
|
106
|
-
dist/asset-manifest.json 1.48 kB 349 B
|
107
|
-
dist/static/js/async/304.c45706bc.js 1.4 kB 575 B
|
108
|
-
dist/static/js/async/509.fcb06e14.js 283 B 230 B
|
109
|
-
|
110
|
-
Client ✔ done in 3.57s
|
99
|
+
dist/static/js/lib-react.09721b5c.js 152.6 kB 49.0 kB
|
100
|
+
dist/html/main/index.html 5.8 kB 2.5 kB
|
101
|
+
dist/static/js/main.3568a38e.js 3.5 kB 1.4 kB
|
102
|
+
dist/static/css/main.03221f72.css 1.4 kB 741 B
|
111
103
|
```
|
112
104
|
|
113
105
|
构建产物默认生成到 `dist/`,目录结构如下:
|
@@ -135,8 +127,7 @@ $ pnpm run serve
|
|
135
127
|
|
136
128
|
> modern serve
|
137
129
|
|
138
|
-
Starting
|
139
|
-
info App running at:
|
130
|
+
Starting production server...
|
140
131
|
|
141
132
|
> Local: http://localhost:8080/
|
142
133
|
> Network: http://192.168.0.1:8080/
|
@@ -8,11 +8,13 @@ sidebar_position: 3
|
|
8
8
|
|
9
9
|
Modern.js 提供了 `upgrade` 命令支持项目升级到最新的 Modern.js 版本。
|
10
10
|
|
11
|
-
在项目中执行 `
|
11
|
+
在项目中执行 `upgrade` 命令:
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
import { PackageManagerTabs } from '@theme';
|
14
|
+
|
15
|
+
<PackageManagerTabs command="run upgrade" />
|
15
16
|
|
17
|
+
```bash
|
16
18
|
> modern upgrade
|
17
19
|
|
18
20
|
[INFO] [项目类型]: Web 应用
|
@@ -22,11 +24,15 @@ $ pnpm run upgrade
|
|
22
24
|
|
23
25
|
可以看到项目 `package.json` 中的依赖已经更改到最新。
|
24
26
|
|
27
|
+
:::tip
|
28
|
+
如果项目的 package.json 中没有声明 upgrade 命令,你可以执行 `npx modern upgrade`,效果是等价的。
|
29
|
+
:::
|
30
|
+
|
25
31
|
## 指定版本升级
|
26
32
|
|
27
33
|
Modern.js 所有的官方包目前都使用**统一版本号**进行发布。
|
28
34
|
|
29
|
-
import ReleaseNote from
|
35
|
+
import ReleaseNote from '@site-docs/components/release-note';
|
30
36
|
|
31
37
|
<ReleaseNote />
|
32
38
|
|
@@ -177,6 +177,8 @@ export const myPlugin = (): CliPlugin => ({
|
|
177
177
|
});
|
178
178
|
```
|
179
179
|
|
180
|
+
注意,只有当前插件的 setup 异步函数执行完毕,才会继续执行下一个插件的 setup 函数。因此,你需要避免在 setup 函数中进行耗时过长的异步操作,防止影响 CLI 启动性能。
|
181
|
+
|
180
182
|
## 添加插件
|
181
183
|
|
182
184
|
自定义插件的使用方式可以查看:[plugins (框架插件)](/configure/app/plugins)。下面会介绍 Modern.js 中推荐的插件实现方法。
|
@@ -1,6 +1,7 @@
|
|
1
1
|
---
|
2
2
|
title: 编写 UI 组件
|
3
3
|
---
|
4
|
+
|
4
5
|
# 编写 UI 组件
|
5
6
|
|
6
7
|
上一章节中,我们学习了如何初始化项目,并使用配置修改 Modern.js 的默认行为。
|
@@ -9,9 +10,9 @@ title: 编写 UI 组件
|
|
9
10
|
|
10
11
|
为了做更好的 UI 展示和交互,我们引入组件库 [Antd](https://ant.design/index-cn) 来开发,使用 `<List>` 组件来代替原始的列表。先添加依赖:
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
import { PackageManagerTabs } from '@theme';
|
14
|
+
|
15
|
+
<PackageManagerTabs command="add antd" />
|
15
16
|
|
16
17
|
修改 `src/routes/page.tsx`,在顶部导入组件:
|
17
18
|
|
package/package.json
CHANGED
@@ -15,17 +15,17 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "0.0.0-nightly-
|
18
|
+
"version": "0.0.0-nightly-20230922160540",
|
19
19
|
"publishConfig": {
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
21
21
|
"access": "public",
|
22
22
|
"provenance": true
|
23
23
|
},
|
24
24
|
"dependencies": {
|
25
|
-
"@modern-js/sandpack-react": "0.0.0-nightly-
|
25
|
+
"@modern-js/sandpack-react": "0.0.0-nightly-20230922160540"
|
26
26
|
},
|
27
27
|
"peerDependencies": {
|
28
|
-
"@modern-js/builder-doc": "0.0.0-nightly-
|
28
|
+
"@modern-js/builder-doc": "0.0.0-nightly-20230922160540"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
31
|
"classnames": "^2",
|
@@ -35,12 +35,12 @@
|
|
35
35
|
"ts-node": "^10.9.1",
|
36
36
|
"typescript": "^5",
|
37
37
|
"fs-extra": "^10",
|
38
|
-
"rspress": "0.
|
39
|
-
"@rspress/shared": "0.
|
38
|
+
"rspress": "0.1.1",
|
39
|
+
"@rspress/shared": "0.1.1",
|
40
40
|
"@types/node": "^16",
|
41
41
|
"@types/fs-extra": "^9",
|
42
|
-
"@modern-js/builder-doc": "0.0.0-nightly-
|
43
|
-
"@modern-js/doc-plugin-auto-sidebar": "0.0.0-nightly-
|
42
|
+
"@modern-js/builder-doc": "0.0.0-nightly-20230922160540",
|
43
|
+
"@modern-js/doc-plugin-auto-sidebar": "0.0.0-nightly-20230922160540"
|
44
44
|
},
|
45
45
|
"scripts": {
|
46
46
|
"dev": "rspress dev",
|