@modern-js/main-doc 2.34.0 → 2.35.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 +12 -11
- package/docs/en/apis/app/hooks/config/public.mdx +2 -2
- package/docs/en/apis/app/hooks/config/upload.mdx +3 -3
- 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/components/tech-stack-node-framework.mdx +1 -0
- package/docs/en/configure/app/builder-plugins.mdx +2 -2
- package/docs/en/configure/app/plugins.mdx +3 -4
- package/docs/en/configure/app/server/base-url.mdx +0 -2
- package/docs/en/configure/app/source/config-dir.mdx +2 -2
- package/docs/en/configure/app/source/enable-async-entry.mdx +1 -1
- package/docs/en/guides/advanced-features/eslint.mdx +1 -1
- package/docs/en/guides/basic-features/alias.mdx +12 -51
- package/docs/en/guides/basic-features/css.mdx +35 -15
- package/docs/en/guides/basic-features/data-fetch.mdx +12 -12
- package/docs/en/guides/basic-features/env-vars.mdx +1 -1
- package/docs/en/guides/basic-features/html.mdx +2 -2
- package/docs/en/guides/basic-features/routes.mdx +31 -19
- package/docs/en/guides/concept/builder.mdx +1 -1
- package/docs/en/guides/get-started/glossary.mdx +1 -1
- package/docs/en/guides/get-started/introduction.mdx +21 -1
- package/docs/en/guides/get-started/quick-start.mdx +8 -17
- package/docs/en/guides/get-started/tech-stack.mdx +138 -0
- package/docs/en/guides/get-started/upgrade.mdx +10 -4
- package/docs/en/guides/topic-detail/changesets/add.mdx +1 -1
- package/docs/en/guides/topic-detail/changesets/changelog.mdx +3 -3
- package/docs/en/guides/topic-detail/changesets/commit.mdx +3 -3
- package/docs/en/guides/topic-detail/changesets/release-note.mdx +3 -3
- package/docs/en/guides/topic-detail/changesets/release.mdx +1 -1
- package/docs/en/guides/topic-detail/framework-plugin/implement.mdx +6 -4
- package/docs/en/guides/topic-detail/generator/create/use.mdx +1 -9
- package/docs/en/guides/topic-detail/generator/plugin/context.md +1 -1
- package/docs/en/guides/topic-detail/micro-frontend/c01-introduction.mdx +1 -0
- package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +24 -17
- package/docs/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +51 -37
- package/docs/en/guides/topic-detail/micro-frontend/c04-communicate.mdx +1 -0
- package/docs/en/guides/topic-detail/micro-frontend/c05-mixed-stack.mdx +1 -1
- package/docs/en/tutorials/first-app/c02-component.mdx +4 -3
- package/docs/zh/apis/app/commands.mdx +10 -9
- package/docs/zh/apis/app/hooks/api/api.mdx +2 -1
- package/docs/zh/apis/app/hooks/server/index_.mdx +2 -1
- package/docs/zh/community/blog/2022-0708-updates.md +4 -4
- package/docs/zh/community/blog/overview.md +1 -1
- 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/components/tech-stack-node-framework.mdx +1 -0
- package/docs/zh/configure/app/builder-plugins.mdx +1 -1
- package/docs/zh/configure/app/plugins.mdx +1 -2
- package/docs/zh/configure/app/server/base-url.mdx +0 -2
- package/docs/zh/configure/app/source/enable-async-entry.mdx +1 -1
- package/docs/zh/guides/advanced-features/eslint.mdx +1 -1
- package/docs/zh/guides/basic-features/alias.mdx +12 -51
- package/docs/zh/guides/basic-features/css.mdx +35 -15
- package/docs/zh/guides/basic-features/data-fetch.mdx +11 -12
- package/docs/zh/guides/basic-features/html.mdx +4 -5
- package/docs/zh/guides/basic-features/routes.mdx +26 -19
- package/docs/zh/guides/get-started/introduction.mdx +20 -0
- package/docs/zh/guides/get-started/quick-start.mdx +8 -17
- package/docs/zh/guides/get-started/tech-stack.mdx +138 -0
- package/docs/zh/guides/get-started/upgrade.mdx +10 -4
- package/docs/zh/guides/topic-detail/changesets/add.mdx +1 -1
- package/docs/zh/guides/topic-detail/changesets/changelog.mdx +3 -3
- package/docs/zh/guides/topic-detail/changesets/commit.mdx +3 -3
- package/docs/zh/guides/topic-detail/changesets/release-note.mdx +3 -3
- package/docs/zh/guides/topic-detail/changesets/release.mdx +1 -1
- package/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx +1 -1
- package/docs/zh/guides/topic-detail/framework-plugin/implement.mdx +7 -5
- package/docs/zh/guides/topic-detail/generator/create/use.mdx +0 -8
- package/docs/zh/guides/topic-detail/generator/plugin/context.md +1 -1
- package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +45 -21
- package/docs/zh/guides/topic-detail/micro-frontend/c03-main-app.mdx +51 -33
- package/docs/zh/guides/topic-detail/micro-frontend/c04-communicate.mdx +1 -0
- package/docs/zh/guides/topic-detail/micro-frontend/c05-mixed-stack.mdx +1 -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/
|
|
@@ -42,7 +41,7 @@ In multi-page (MPA) projects, the `--entry` option can be added to specify one o
|
|
|
42
41
|
|
|
43
42
|
For example, execute `modern dev --entry`, the entry selector will be displayed in the command line interface:
|
|
44
43
|
|
|
45
|
-
```
|
|
44
|
+
```text
|
|
46
45
|
$ modern dev --entry
|
|
47
46
|
|
|
48
47
|
? Please select the entry that needs to be built
|
|
@@ -86,14 +85,16 @@ Options:
|
|
|
86
85
|
|
|
87
86
|
execute `npx modern build --analyze` command, can produce an HTML file that analyzes the volume of the bundle while packaging the production code:
|
|
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:
|
|
@@ -125,7 +126,7 @@ Options:
|
|
|
125
126
|
|
|
126
127
|
In the project, execute the `new` command to add entries as follows:
|
|
127
128
|
|
|
128
|
-
```
|
|
129
|
+
```text
|
|
129
130
|
$ npx modern new
|
|
130
131
|
? Please select the operation you want: Create Element
|
|
131
132
|
? Please select the type of element to create: New "entry"
|
|
@@ -136,7 +137,7 @@ $ npx modern new
|
|
|
136
137
|
|
|
137
138
|
In the project, execute the `new` command to enable features as follows:
|
|
138
139
|
|
|
139
|
-
```
|
|
140
|
+
```text
|
|
140
141
|
$ npx modern new
|
|
141
142
|
? Please select the operation you want: Enable Features
|
|
142
143
|
? Please select the feature name: (Use arrow keys)
|
|
@@ -156,7 +157,7 @@ pnpm does not support the use of JSON strings as parameter values currently. Use
|
|
|
156
157
|
|
|
157
158
|
## modern serve
|
|
158
159
|
|
|
159
|
-
|
|
160
|
+
The `modern serve` command is used to start a Modern.js project in the production environment. It can also be used to preview the artifacts built for the production environment locally. Please note that you need to execute the [`build`](/apis/app/commands#modern-build) command beforehand to generate the corresponding artifacts.
|
|
160
161
|
|
|
161
162
|
```bash
|
|
162
163
|
Usage: modern serve [options]
|
|
@@ -4,7 +4,7 @@ sidebar_position: 3
|
|
|
4
4
|
---
|
|
5
5
|
# public/
|
|
6
6
|
|
|
7
|
-
Any static
|
|
7
|
+
Any static assets can be placed in the `public/` directory, and the files will be deployed to the corresponding application domain by the server.
|
|
8
8
|
|
|
9
9
|
## Description
|
|
10
10
|
|
|
@@ -19,7 +19,7 @@ For example, authentication files required by third-party systems such as `robot
|
|
|
19
19
|
Or SDKs for other business parties (requiring unchanged routing), or HTML files without entry.
|
|
20
20
|
|
|
21
21
|
:::info
|
|
22
|
-
For static
|
|
22
|
+
For static assets (such as SVG images) that need to be imported through import in the source code, it is recommended to manage them in the `src/assets` directory.
|
|
23
23
|
|
|
24
24
|
:::
|
|
25
25
|
|
|
@@ -4,13 +4,13 @@ sidebar_position: 4
|
|
|
4
4
|
---
|
|
5
5
|
# upload/
|
|
6
6
|
|
|
7
|
-
Any static
|
|
7
|
+
Any static assets can be placed in the `upload/` directory.
|
|
8
8
|
|
|
9
9
|
## Description
|
|
10
10
|
|
|
11
|
-
In the development environment, the static
|
|
11
|
+
In the development environment, the static assets in this directory will be hosted under the `/upload` path. After building the application, the files in this directory will be copied to the dist directory.
|
|
12
12
|
|
|
13
|
-
This file convention is mainly used for developers to use plugins to proactively upload static
|
|
13
|
+
This file convention is mainly used for developers to use plugins to proactively upload static assets to the CDN.
|
|
14
14
|
|
|
15
15
|
## Scenarios
|
|
16
16
|
|
|
@@ -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 50ms
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Modern.js supports [Express.js](https://expressjs.com/) and [Koa.js](https://koajs.com/) as optional BFF runtime frameworks. Please refer to the [BFF - Frameworks](/guides/advanced-features/bff/frameworks.html) for more information.
|
|
@@ -15,8 +15,8 @@ Modern.js Builder is the build tool of Modern.js, please read [Builder](/guides/
|
|
|
15
15
|
|
|
16
16
|
This option **is used to configure the Modern.js Builder plugins**. If you need to configure other types of plugins, please select the corresponding configs:
|
|
17
17
|
|
|
18
|
-
- Use [plugins](/configure/app/
|
|
19
|
-
- Use [tools.
|
|
18
|
+
- Use [plugins](/configure/app/plugins) to configure Modern.js framework plugins.
|
|
19
|
+
- Use [tools.bundlerChain](/configure/app/tools/bundler-chain) to configure Rspack or webpack plugins.
|
|
20
20
|
- Use [tools.babel](/configure/app/tools/babel) to configure Babel plugins.
|
|
21
21
|
|
|
22
22
|
## When to use
|
|
@@ -15,10 +15,9 @@ Refer to [How to Develop Plugins](/guides/topic-detail/framework-plugin/implemen
|
|
|
15
15
|
|
|
16
16
|
This option is used to configure framework plugins. If you need to configure other types of plugins, please choose the corresponding configuration method:
|
|
17
17
|
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
- To configure Babel plugins, use the [tools.babel](/configure/app/tools/babel) option.
|
|
18
|
+
- Use [builderPlugins](/configure/app/builder-plugins) to configure Modern.js Builder plugins.
|
|
19
|
+
- Use [tools.bundlerChain](/configure/app/tools/bundler-chain) to configure Rspack or webpack plugins.
|
|
20
|
+
- Use [tools.babel](/configure/app/tools/babel) to configure Babel plugins.
|
|
22
21
|
|
|
23
22
|
## Plugin types
|
|
24
23
|
|
|
@@ -7,11 +7,11 @@ sidebar_label: configDir
|
|
|
7
7
|
- **Type:** `string`
|
|
8
8
|
- **Default:** `./config`
|
|
9
9
|
|
|
10
|
-
Modern.js supports placing some files in the `./config` folder to customize HTML templates, icons, static
|
|
10
|
+
Modern.js supports placing some files in the `./config` folder to customize HTML templates, icons, static assets, etc. For details, please refer to [File Convention](/apis/app/hooks/config/html).
|
|
11
11
|
|
|
12
12
|
This option allows you to customize the directory of the configuration files.
|
|
13
13
|
|
|
14
|
-
For example, adjust the
|
|
14
|
+
For example, adjust the assets directory to the `resources` directory:
|
|
15
15
|
|
|
16
16
|
```js title="modern.config.ts"
|
|
17
17
|
import { defineConfig } from '@modern-js/app-tools';
|
|
@@ -49,6 +49,6 @@ import('./bootstrap.jsx');
|
|
|
49
49
|
At this point, you can consume any remote module in the current page.
|
|
50
50
|
|
|
51
51
|
:::info
|
|
52
|
-
Modern.js does not have
|
|
52
|
+
Modern.js does not have ModuleFederationPlugin plugin built in. Please configure the ModuleFederationPlugin yourself via [tools.bundlerChain](/configure/app/tools/bundler-chain).
|
|
53
53
|
|
|
54
54
|
:::
|
|
@@ -71,7 +71,7 @@ If the project does have special requirements or inevitable compatibility issues
|
|
|
71
71
|
|
|
72
72
|
### `src/.eslintrc.js`
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
Modern.js Framework or Modern.js Module project will have this configuration file by default in the source code directory, which is designed for Universal JS code.
|
|
75
75
|
|
|
76
76
|
:::info
|
|
77
77
|
Universal JS code is code that can run on both the browser side and the server side.
|
|
@@ -1,67 +1,28 @@
|
|
|
1
1
|
---
|
|
2
|
-
sidebar_position:
|
|
2
|
+
sidebar_position: 4
|
|
3
3
|
---
|
|
4
4
|
|
|
5
5
|
# Path Alias
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
import Alias from '@modern-js/builder-doc/docs/en/shared/alias';
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
'@': '<appDirectory>/src',
|
|
12
|
-
'@shared': '<appDirectory>/shared',
|
|
13
|
-
}
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
:::info
|
|
17
|
-
When enabling optional features, the `new` command will also dynamically add built-in alias specific to the features. For example, when enabling BFF, the `@api` alias is added by default.
|
|
9
|
+
<Alias />
|
|
18
10
|
|
|
19
|
-
|
|
11
|
+
## Default Aliases
|
|
20
12
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
.
|
|
25
|
-
├── common
|
|
26
|
-
│ ├── styles
|
|
27
|
-
│ │ └── base.css
|
|
28
|
-
│ └── utils
|
|
29
|
-
│ └── index.ts
|
|
30
|
-
└── App.tsx
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
The code in `src/App.tsx` is as follows:
|
|
34
|
-
|
|
35
|
-
```ts
|
|
36
|
-
import utils from '@/src/common/utils';
|
|
37
|
-
import '@/src/common/styles/base.css';
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
Modern.js also provides a way to customize alias. For example, adding the `@common` alias is as follows:
|
|
41
|
-
|
|
42
|
-
For TypeScript projects, just set `compilerOptions.paths` in the project's `tsconfig.json`:
|
|
13
|
+
The Modern.js framework comes with the following aliases built-in:
|
|
43
14
|
|
|
44
15
|
```json
|
|
45
16
|
{
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
"@/*": ["./src/*"],
|
|
49
|
-
"@/common/*": ["./src/common/*"]
|
|
50
|
-
}
|
|
51
|
-
}
|
|
17
|
+
"@": "./src",
|
|
18
|
+
"@shared": "./shared"
|
|
52
19
|
}
|
|
53
20
|
```
|
|
54
21
|
|
|
55
|
-
|
|
22
|
+
Additionally, when the BFF plugin of the framework is enabled, the `@api` alias is automatically added.
|
|
56
23
|
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
'@common': './src/common',
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
});
|
|
24
|
+
```json
|
|
25
|
+
{
|
|
26
|
+
"@api": "./api"
|
|
27
|
+
}
|
|
65
28
|
```
|
|
66
|
-
|
|
67
|
-
For the specific usage of alias configuration, please refer to the [source.alias documentation](/configure/app/source/alias).
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
sidebar_position:
|
|
2
|
+
sidebar_position: 1
|
|
3
3
|
---
|
|
4
4
|
|
|
5
5
|
# Styling
|
|
@@ -61,16 +61,31 @@ If you need to use other CSS-in-JS libraries such as [styled-jsx](https://www.np
|
|
|
61
61
|
|
|
62
62
|
## Using Tailwind CSS
|
|
63
63
|
|
|
64
|
-
[Tailwind CSS](https://tailwindcss.com/) is a CSS framework and design system based on Utility Class, which can quickly add common styles to components, and support flexible extension of theme styles.
|
|
64
|
+
[Tailwind CSS](https://tailwindcss.com/) is a CSS framework and design system based on Utility Class, which can quickly add common styles to components, and support flexible extension of theme styles.
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
To use [Tailwind CSS](https://tailwindcss.com/) in Modern.js, you can follow the steps below:
|
|
67
|
+
|
|
68
|
+
1. Run `pnpm run new` in the root directory of your project and make the following selections:
|
|
67
69
|
|
|
68
70
|
```text
|
|
69
71
|
? Please select the operation you want: Enable features
|
|
70
72
|
? Please select the feature name: Enable Tailwind CSS
|
|
71
73
|
```
|
|
72
74
|
|
|
73
|
-
|
|
75
|
+
After successful initialization, you will see the following additions to the `package.json` file:
|
|
76
|
+
|
|
77
|
+
```json title="./package.json"
|
|
78
|
+
{
|
|
79
|
+
"dependencies": {
|
|
80
|
+
"tailwindcss": "^3.0.0"
|
|
81
|
+
},
|
|
82
|
+
"devDependencies": {
|
|
83
|
+
"@modern-js/plugin-tailwindcss": "^2.0.0"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
2. Register the Tailwind plugin in `modern.config.ts`:
|
|
74
89
|
|
|
75
90
|
```ts title="modern.config.ts"
|
|
76
91
|
import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
|
|
@@ -80,29 +95,34 @@ export default defineConfig({
|
|
|
80
95
|
});
|
|
81
96
|
```
|
|
82
97
|
|
|
83
|
-
|
|
98
|
+
3. Create a `index.css` file and add the following code:
|
|
99
|
+
|
|
100
|
+
```css title="index.css"
|
|
101
|
+
@tailwind base;
|
|
102
|
+
@tailwind components;
|
|
103
|
+
@tailwind utilities;
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
:::info
|
|
107
|
+
Depending on your needs, you can selectively import the CSS styles provided by Tailwind CSS. Please refer to the [`@tailwind` documentation](https://tailwindcss.com/docs/functions-and-directives#tailwind) for detailed usage of the `@tailwind` directive.
|
|
108
|
+
:::
|
|
109
|
+
|
|
110
|
+
4. Import the `index.css` file, for example, add the following code in the root component `src/App.jsx`:
|
|
84
111
|
|
|
85
112
|
```js
|
|
86
|
-
import '
|
|
87
|
-
import 'tailwindcss/components.css';
|
|
88
|
-
import 'tailwindcss/utilities.css';
|
|
113
|
+
import './index.css';
|
|
89
114
|
```
|
|
90
115
|
|
|
91
|
-
|
|
116
|
+
5. Now you can use the Utility Classes provided by Tailwind CSS in your components:
|
|
92
117
|
|
|
93
118
|
```tsx
|
|
94
|
-
const
|
|
119
|
+
const Hello = () => (
|
|
95
120
|
<div className="h-12 w-48">
|
|
96
121
|
<p className="text-xl font-medium text-black">hello world</p>
|
|
97
122
|
</div>
|
|
98
123
|
);
|
|
99
124
|
```
|
|
100
125
|
|
|
101
|
-
:::info Additional Information
|
|
102
|
-
Depending on your needs, you can selectively import the CSS files provided by Tailwind CSS. Since using `@tailwind` is equivalent to directly importing CSS files, you can refer to the comments in the [`@tailwind` usage](https://tailwindcss.com/docs/functions-and-directives#tailwind) documentation for the purpose of the CSS files provided by Tailwind CSS.
|
|
103
|
-
|
|
104
|
-
:::
|
|
105
|
-
|
|
106
126
|
### Configuring Tailwind CSS
|
|
107
127
|
|
|
108
128
|
In Modern.js, you have two ways to configure Tailwind CSS:
|
|
@@ -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
|
```
|
|
@@ -10,7 +10,7 @@ Modern.js provides support for environment variables, including built-in environ
|
|
|
10
10
|
|
|
11
11
|
### ASSET_PREFIX
|
|
12
12
|
|
|
13
|
-
The current path prefix of the
|
|
13
|
+
The current path prefix of the asset, which is a read-only environment variable.
|
|
14
14
|
|
|
15
15
|
### NODE_ENV
|
|
16
16
|
|
|
@@ -165,9 +165,9 @@ export default function Document(): React.ReactElement {
|
|
|
165
165
|
|
|
166
166
|
## HTML Syntax
|
|
167
167
|
|
|
168
|
-
Modern.js also supports HTML
|
|
168
|
+
Modern.js also supports generating HTML files using HTML (EJS) syntax.
|
|
169
169
|
|
|
170
|
-
|
|
170
|
+
By default, Modern.js projects come with a built-in HTML template for generating HTML code. If you need to customize the HTML template, you can use two methods: **Custom HTML Fragments** and **Fully Custom HTML Templates**.
|
|
171
171
|
|
|
172
172
|
### Custom HTML Fragments
|
|
173
173
|
|
|
@@ -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';
|
|
@@ -480,15 +491,16 @@ In conventional routing, Modern.js automatically splits routes into chunks based
|
|
|
480
491
|
|
|
481
492
|
However, this also brings a problem: if the chunk corresponding to the route has not finished loading when the user visits the route, a white screen will appear.
|
|
482
493
|
|
|
483
|
-
In this case, you can define a `Loading` component to display a custom `Loading` component before the static
|
|
494
|
+
In this case, you can define a `Loading` component to display a custom `Loading` component before the static assets are loaded.
|
|
484
495
|
|
|
485
|
-
To further improve the user experience and reduce loading time, Modern.js supports defining the `prefetch` attribute on the Link component to preload static
|
|
496
|
+
To further improve the user experience and reduce loading time, Modern.js supports defining the `prefetch` attribute on the Link component to preload static assets and data.
|
|
486
497
|
|
|
487
498
|
```tsx
|
|
488
499
|
<Link prefetch="intent" to="page">
|
|
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
|
|
|
@@ -502,19 +514,19 @@ The `prefetch` attribute has three optional values:
|
|
|
502
514
|
|
|
503
515
|
#### FAQ
|
|
504
516
|
|
|
505
|
-
1. What is the difference between using `render` and not splitting static
|
|
517
|
+
1. What is the difference between using `render` and not splitting static assets based on the route?
|
|
506
518
|
|
|
507
519
|
- By using `render`, you can specify which routes to load during the initial screen, and you can control the rendering so that only when the `Link` component enters the visible area, the `Link` component will be rendered.
|
|
508
|
-
- By using `render`, static
|
|
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
|
|
|
@@ -42,6 +42,6 @@ For detailed information about build configuration, please refer to [「Modern.j
|
|
|
42
42
|
|
|
43
43
|
## Builder Capabilities
|
|
44
44
|
|
|
45
|
-
Modern.js Builder provides rich build capabilities, including JavaScript compilation, CSS compilation, static
|
|
45
|
+
Modern.js Builder provides rich build capabilities, including JavaScript compilation, CSS compilation, static assets processing, code hot update, code compression, TS type checking, and dozens of other capabilities.
|
|
46
46
|
|
|
47
47
|
We recommend that you read [「Modern.js Builder - All Features」](https://modernjs.dev/builder/en/guide/features.html) to learn about all the features provided by Modern.js Builder.
|