@modern-js/main-doc 0.0.0-nightly-20240820170642 → 0.0.0-nightly-20240822170648
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/components/deploy.mdx +1 -1
- package/docs/en/guides/concept/entries.mdx +70 -46
- package/docs/en/guides/get-started/introduction.mdx +1 -1
- package/docs/en/guides/get-started/quick-start.mdx +0 -1
- package/docs/en/guides/get-started/tech-stack.mdx +0 -2
- package/docs/en/guides/get-started/upgrade.mdx +16 -2
- package/docs/zh/components/deploy.mdx +1 -1
- package/docs/zh/guides/concept/entries.mdx +76 -57
- package/docs/zh/guides/get-started/introduction.mdx +2 -2
- package/docs/zh/guides/get-started/quick-start.mdx +0 -1
- package/docs/zh/guides/get-started/tech-stack.mdx +0 -2
- package/docs/zh/guides/get-started/upgrade.mdx +15 -1
- package/package.json +4 -4
@@ -1 +1 @@
|
|
1
|
-
After local
|
1
|
+
After local develop, you can refer to the [Deployment](/guides/basic-features/deploy.html) section to deploy the project to the server.
|
@@ -16,7 +16,7 @@ Many configuration options provided by Modern.js are divided by entry, such as p
|
|
16
16
|
|
17
17
|
## Single Entry and Multiple Entries
|
18
18
|
|
19
|
-
The project initialized by Modern.js is a single entry
|
19
|
+
The project initialized by Modern.js is a single entry project, with the following structure:
|
20
20
|
|
21
21
|
```
|
22
22
|
.
|
@@ -56,13 +56,12 @@ 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
|
-
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
|
-
|
61
|
-
:::tip
|
62
59
|
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}`.
|
63
60
|
|
64
|
-
|
61
|
+
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.
|
65
62
|
|
63
|
+
:::note
|
64
|
+
The concepts of **single entry/multiple entry** and **SPA/MPA** are not equivalent. The former pertains to how to configure and package the application, while the latter is about the patterns for organizing front-end applications. Each entry point can be either an SPA or a non-SPA.
|
66
65
|
:::
|
67
66
|
|
68
67
|
## Entry Types
|
@@ -82,30 +81,26 @@ By default, Modern.js scans the files under `src/` before starting the project,
|
|
82
81
|
|
83
82
|
:::
|
84
83
|
|
85
|
-
|
84
|
+
The entry directory must meet one of the following three conditions:
|
86
85
|
|
87
86
|
1. Has a `routes/` directory.
|
88
87
|
2. Has an `App.[jt]sx?` file.
|
89
|
-
|
90
|
-
4. Has a `pages/` directory (compatible with Modern.js 1.0).
|
91
|
-
5. Has an `entry.[jt]sx?` file.
|
88
|
+
5. Has an `entry.[jt]sx?` file.(Requires [source.enableCustomEntry](/configure/app/source/enable-custom-entry) to be enabled)
|
92
89
|
|
93
|
-
When the `src/` directory meets the entry
|
90
|
+
When the `src/` directory meets the conditions of an entry, Modern.js will consider the current application to be a single-entry application. Otherwise, Modern.js will scan the first-level directories under `src/` and further determine if they are entries. In this case, the application is typically a multi-entry application.
|
94
91
|
|
95
92
|
:::tip
|
96
93
|
In a single entry application, the default entry name is `main`.
|
97
94
|
|
98
95
|
:::
|
99
96
|
|
100
|
-
When the project is not a single entry application, Modern.js will further look at the top-level directories under `src/`.
|
101
|
-
|
102
97
|
### Framework Mode Entry
|
103
98
|
|
104
|
-
|
99
|
+
Framework mode refers to using Modern.js's framework capabilities, such as convention routing, SSR (Server-Side Rendering), and integrated calls. Under this type of entry convention, the entries in the application are not the actual compilation entries. Modern.js will generate a wrapped entry during startup, which you can find in `node_modules/.modern/[entryName]/index.js`.
|
105
100
|
|
106
101
|
#### Conventional Routing
|
107
102
|
|
108
|
-
If there is a `routes/` directory
|
103
|
+
If there is a `routes/` directory within the entry, we refer to this entry as a convention-based route. Modern.js will scan the files under `routes/` during startup and automatically generate client-side routes (react-router) based on file conventions. For example:
|
109
104
|
|
110
105
|
```bash
|
111
106
|
.
|
@@ -121,7 +116,15 @@ For more information, please refer to [Conventional Routing](/guides/basic-featu
|
|
121
116
|
|
122
117
|
#### Self-controlled Routing
|
123
118
|
|
124
|
-
If there is an `App.[jt]sx?` file
|
119
|
+
If there is an `App.[jt]sx?` file within the entry, we refer to this entry as a self-controlled route. For example:
|
120
|
+
|
121
|
+
```bash
|
122
|
+
.
|
123
|
+
├── src
|
124
|
+
│ └── App.tsx
|
125
|
+
```
|
126
|
+
|
127
|
+
In this file, you can define client-side routes or not to set any client-side routes.
|
125
128
|
|
126
129
|
```tsx
|
127
130
|
import { BrowserRouter, Route, Routes } from '@modern-js/runtime/router';
|
@@ -142,12 +145,14 @@ For more information, please refer to [Self-controlled Routing](/guides/basic-fe
|
|
142
145
|
|
143
146
|
#### Custom Entry
|
144
147
|
|
145
|
-
If there is an `entry.[jt]sx` file in the entry, developers need to call the `createRoot` and `render` functions in the `entry.[jt]sx` file to complete the project's entry logic.
|
146
|
-
|
147
148
|
:::info
|
148
|
-
Using this
|
149
|
+
Using this features requires enabling [source.enableCustomEntry](/configure/app/source/enable-custom-entry).
|
149
150
|
:::
|
150
151
|
|
152
|
+
By default, whether you use convention routing or self-controlled routing, Modern.js will automatically handle rendering. If you wish to customize this behavior, you can creating a custom entry file.
|
153
|
+
|
154
|
+
If there is an `entry.[jt]sx` file within the entry, Modern.js will no longer control the application's rendering process. You can call the `createRoot` and `render` functions within the `entry.[jt]sx` file to complete the entry logic for your application.
|
155
|
+
|
151
156
|
```tsx
|
152
157
|
import { createRoot } from '@modern-js/runtime/react';
|
153
158
|
import { render } from '@modern-js/runtime/browser';
|
@@ -157,7 +162,9 @@ const ModernRoot = createRoot();
|
|
157
162
|
render(<ModernRoot />);
|
158
163
|
```
|
159
164
|
|
160
|
-
|
165
|
+
In the code above, the component returned by the `createRoot` function is either the component generated from the `routes/` directory or the component exported by `App.tsx`.
|
166
|
+
|
167
|
+
The `render` function is used to handle rendering and mounting of the component. For example, if you want to execute some asynchronous tasks before rendering, you can achieve it like this:
|
161
168
|
|
162
169
|
```tsx
|
163
170
|
import { createRoot } from '@modern-js/runtime/react';
|
@@ -166,7 +173,7 @@ import { render } from '@modern-js/runtime/browser';
|
|
166
173
|
const ModernRoot = createRoot();
|
167
174
|
|
168
175
|
async function beforeRender() {
|
169
|
-
|
176
|
+
// some async request
|
170
177
|
}
|
171
178
|
|
172
179
|
beforeRender().then(() => {
|
@@ -174,34 +181,15 @@ beforeRender().then(() => {
|
|
174
181
|
});
|
175
182
|
```
|
176
183
|
|
177
|
-
#### Custom Bootstrap
|
178
|
-
|
179
|
-
:::warning
|
180
|
-
Soon to be deprecated, it is recommended to use a custom entry.
|
181
|
-
:::
|
182
|
-
|
183
|
-
If there is an `index.[jt]sx` file in the entry, and the file exports a function by default, Modern.js will pass the default `bootstrap` function as a parameter and use the exported function to replace the default `bootstrap`. This way, developers can customize how components are mounted to DOM nodes or add custom behavior before mounting. For example:
|
184
|
-
|
185
|
-
```tsx
|
186
|
-
export default (App: React.ComponentType, bootstrap: () => void) => {
|
187
|
-
// do something before bootstrap...
|
188
|
-
initSomething().then(() => {
|
189
|
-
bootstrap();
|
190
|
-
});
|
191
|
-
};
|
192
|
-
```
|
193
|
-
|
194
184
|
### Build Mode Entry
|
195
185
|
|
196
|
-
Build mode refers to not using the Runtime capabilities provided by Modern.js, but rather having the developer define the entry of the page completely on their own.
|
197
|
-
|
198
|
-
When the entry directory contains `index.[jt]sx` (soon to be deprecated) and does not export a function via `export default`, or when there is an `entry.[jt]sx` file in the entry directory and the `@modern-js/runtime` dependency is not installed, the corresponding file will be identified as the entry module of webpack or Rspack.
|
199
|
-
|
200
186
|
:::info
|
201
|
-
Using
|
187
|
+
Using this features requires enabling [source.enableCustomEntry](/configure/app/source/enable-custom-entry).
|
202
188
|
:::
|
203
189
|
|
204
|
-
|
190
|
+
Build mode refers to the development mode that does not use Modern.js's runtime capabilities and only utilizes Modern.js's build capabilities. When the `@modern-js/runtime` dependency is not installed in the application, Modern.js will treat all entries as build mode entries.
|
191
|
+
|
192
|
+
In this case, if there is an `entry.[jt]sx` file within the entry, this file will be recognized as the build entry for webpack or Rspack. Modern.js will not automatically generate entry code at this time, and you need to mount the component to the DOM node yourself. For example:
|
205
193
|
|
206
194
|
```js title=src/entry.tsx
|
207
195
|
import React from 'react';
|
@@ -211,9 +199,13 @@ import App from './App';
|
|
211
199
|
ReactDOM.render(<App />, document.getElementById('root'));
|
212
200
|
```
|
213
201
|
|
214
|
-
|
202
|
+
In build mode, the application **will not be able to use Modern.js's runtime capabilities**, such as:
|
203
|
+
- Convention routing, the routing based on the files under `src/routes`
|
204
|
+
- Server-Side Rendering (SSR)
|
205
|
+
- The `runtime` configuration in the `modern.config.js` file will no longer take effect
|
215
206
|
|
216
|
-
|
207
|
+
|
208
|
+
## Specify entry in the configuration file
|
217
209
|
|
218
210
|
In some cases, you may need to customize the entry configuration instead of using the entry conventions provided by Modern.js.
|
219
211
|
|
@@ -246,4 +238,36 @@ export default defineConfig({
|
|
246
238
|
});
|
247
239
|
```
|
248
240
|
|
249
|
-
|
241
|
+
It is worth noting that, by default, Modern.js considers entries specified through the configuration as **framework mode entries** and will automatically generate the actual compilation entry.
|
242
|
+
|
243
|
+
If your application is migrating from build tools like Webpack or Vite to the Modern.js framework, you typically need to enable the `disableMount` option in the entry configuration. In this case, Modern.js will treat the entry as a **build mode entry**.
|
244
|
+
|
245
|
+
## Deprecated
|
246
|
+
|
247
|
+
Currently, if the entry directory meets the following conditions, it will also be considered an application entry:
|
248
|
+
|
249
|
+
1. Has an `index.[jt]sx?` file.
|
250
|
+
2. Has a `pages/` directory.
|
251
|
+
|
252
|
+
The `pages/` directory was the convention routing in older versions of Modern.js. Now, it is recommended to use the `routes/` directory.
|
253
|
+
|
254
|
+
The `index.[jt]sx?` file supported **Custom Bootstrap** and **Build Mode Entry** in older versions. Now, it is recommended to use `entry.[jt]sx?` instead.
|
255
|
+
|
256
|
+
### Custom Bootstrap
|
257
|
+
|
258
|
+
When there is an `index.[jt]sx` file in the entry, and the file's default export is a function, Modern.js will pass the default `bootstrap` function as an argument and use the exported function to replace the default `bootstrap`.
|
259
|
+
|
260
|
+
This allows developers to customize mounting components to DOM or add custom behaviors before mounting. For example:
|
261
|
+
|
262
|
+
```tsx
|
263
|
+
export default (App: React.ComponentType, bootstrap: () => void) => {
|
264
|
+
// do something before bootstrap...
|
265
|
+
initSomething().then(() => {
|
266
|
+
bootstrap();
|
267
|
+
});
|
268
|
+
};
|
269
|
+
```
|
270
|
+
|
271
|
+
### Build Mode Entry
|
272
|
+
|
273
|
+
When an `index.[jt]sx` file exists in the entry directory and does not export a function via export default, this entry will also be considered a build mode entry.
|
@@ -55,7 +55,7 @@ It mainly includes the following features:
|
|
55
55
|
- 🏠 **Integration**: Development and production environment web server are unique, CSR and SSR are isomorphic development, and API service calls are functions as interfaces.
|
56
56
|
- 📦 **Out Of The Box**: Default TS support, built-in build, ESLint, debugging tools, fully functional and testable.
|
57
57
|
- 🌏 **Ecology**: Self-developed state management, micro-frontend, module packaging, and other peripheral needs.
|
58
|
-
- 🕸 **Routing
|
58
|
+
- 🕸 **Convention Routing**: Using file-based routing helps developers quickly set up applications.
|
59
59
|
|
60
60
|
## Comparison with Others
|
61
61
|
|
@@ -72,7 +72,6 @@ In a newly created project, the `@modern-js/app-tools` npm package is installed
|
|
72
72
|
- It integrates Modern.js Core, providing capabilities for configuration parsing, plugin loading, and more.
|
73
73
|
- It integrates Modern.js Builder, providing build capabilities.
|
74
74
|
- It integrates Modern.js Server, providing capabilities for development and production servers.
|
75
|
-
- It integrates some commonly used plugins, such as `plugin-lint`, `plugin-data-loader`, and more.
|
76
75
|
|
77
76
|
`@modern-js/app-tools` is implemented based on the plugin system of Modern.js. Essentially, it is a plugin. Therefore, you need to register `appTools` in the `plugins` field of the configuration file:
|
78
77
|
|
@@ -30,8 +30,6 @@ We are also working with Zack Jackson, the author of [Module Federation](https:/
|
|
30
30
|
|
31
31
|
Modern.js can be used with any community state management library, such as [Redux](https://redux.js.org/), [Jotai](https://jotai.org/), [Zustand](https://docs.pmnd.rs/zustand), [Valtio](https://valtio.pmnd.rs/), and more.
|
32
32
|
|
33
|
-
Modern.js also provides a wrapper around Redux called Reduck for state management. You can refer to ["Reduck State Management"](/en/guides/topic-detail/model/quick-start) for usage.
|
34
|
-
|
35
33
|
## Package Manager
|
36
34
|
|
37
35
|
Modern.js can be used with any community package manager, such as [npm](https://www.npmjs.com/package/npm), [yarn](https://classic.yarnpkg.com/lang/en/), [pnpm](https://pnpm.io/), or [Bun](https://bun.sh/).
|
@@ -37,10 +37,24 @@ import ReleaseNote from '@site-docs-en/components/release-note';
|
|
37
37
|
<ReleaseNote />
|
38
38
|
|
39
39
|
:::tip
|
40
|
-
When upgrading, you need to upgrade all packages provided by Modern.js,
|
41
|
-
|
40
|
+
When upgrading, you need to upgrade all packages provided by Modern.js, rather than upgrading some dependencies.
|
42
41
|
:::
|
43
42
|
|
43
|
+
## Version Management Strategy
|
44
|
+
|
45
|
+
In Modern.js projects, we recommend that all officially provided dependencies use fixed version, and avoid using `^` or `~` for range declarations. For example:
|
46
|
+
|
47
|
+
```json
|
48
|
+
{
|
49
|
+
"dependencies": {
|
50
|
+
"@modern-js/app-tools": "x.y.z"
|
51
|
+
}
|
52
|
+
}
|
53
|
+
```
|
54
|
+
|
55
|
+
This ensures that the versions of dependencies are fully determined, thereby guaranteeing build consistency and predictability.
|
56
|
+
|
57
|
+
|
44
58
|
## Lock nested dependency
|
45
59
|
|
46
60
|
When a nested dependency of the project has a problem and Modern.js cannot be updated immediately, you can use the package manager to lock the version of the nested dependency.
|
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
本地验证完成后,可以参考 [部署](/guides/basic-features/deploy.html) 一节,将项目部署到服务器上。
|
@@ -10,13 +10,13 @@ sidebar_position: 1
|
|
10
10
|
|
11
11
|
**入口(Entry)指的是一个页面的起始模块。**
|
12
12
|
|
13
|
-
在 Modern.js
|
13
|
+
在 Modern.js 应用中,每一个入口对应一个独立的页面,也对应一条服务端路由。默认情况下,Modern.js 会基于目录约定来自动确定页面的入口,同时也支持通过配置项来自定义入口。
|
14
14
|
|
15
15
|
Modern.js 提供的很多配置项都是以入口为维度进行划分的,比如页面标题、HTML 模板、页面 Meta 信息、是否开启 SSR/SSG、服务端路由规则等。
|
16
16
|
|
17
17
|
## 单入口与多入口
|
18
18
|
|
19
|
-
Modern.js
|
19
|
+
Modern.js 初始化的应用是单入口的,应用结构如下:
|
20
20
|
|
21
21
|
```
|
22
22
|
.
|
@@ -30,7 +30,7 @@ Modern.js 初始化的项目是单入口的(SPA),项目结构如下:
|
|
30
30
|
└── tsconfig.json
|
31
31
|
```
|
32
32
|
|
33
|
-
在 Modern.js
|
33
|
+
在 Modern.js 应用中,你可以很方便的将单入口切换成多入口。在应用目录下执行 `pnpm run new`,根据提示即可创建入口:
|
34
34
|
|
35
35
|
```bash
|
36
36
|
? 请选择你想要的操作 创建工程元素
|
@@ -56,13 +56,12 @@ Modern.js 初始化的项目是单入口的(SPA),项目结构如下:
|
|
56
56
|
|
57
57
|
原本的入口代码被移动到了和 `package.json` 中 `name` 同名的目录下,并创建了 `new-entry` 入口目录。
|
58
58
|
|
59
|
-
|
60
|
-
|
61
|
-
:::tip
|
62
|
-
Modern.js 会将与 `package.json` 文件中 `name` 字段同名的入口作为主入口,主入口的路由为 `/`,其他入口的路由为 `/{entryName}`。
|
59
|
+
Modern.js 会将与 `package.json` 文件中 `name` 字段同名的入口作为主入口,主入口的路由为 `/`,其他入口的路由为 `/{entryName}`。比如,`package.json` 中的 `name` 为 `myapp` 时,`src/myapp` 会作为应用的主入口。
|
63
60
|
|
64
|
-
|
61
|
+
你可以执行 `pnpm run dev` 启动开发服务,此时可以看到新增了一条名为 `/new-entry` 的路由,并且原有页面的路由并未发生变化。
|
65
62
|
|
63
|
+
:::note
|
64
|
+
**单入口/多入口** 和 **SPA/MPA** 的概念并不等价。前者是关于如何配置和打包应用,而后者是组织前端应用的模式,每个入口都可以是 SPA 或非 SPA 的。
|
66
65
|
:::
|
67
66
|
|
68
67
|
## 入口类型
|
@@ -73,39 +72,32 @@ import EntryMode from '@site-docs/components/entry-mode.mdx';
|
|
73
72
|
|
74
73
|
<EntryMode />
|
75
74
|
|
76
|
-
默认情况下,Modern.js
|
75
|
+
默认情况下,Modern.js 启动应用前会对 `src/` 下的文件进行扫描,识别入口,并生成对应的服务端路由。
|
77
76
|
|
78
77
|
:::tip
|
79
|
-
|
80
|
-
- 如果你需要自定义入口,请参考 [自定义入口](#自定义入口)。
|
81
|
-
|
78
|
+
你可以通过 [source.entriesDir](/configure/app/source/entries-dir) 修改识别入口的目录。
|
82
79
|
:::
|
83
80
|
|
84
|
-
|
81
|
+
入口所在目录必须满足以下三个条件之一:
|
85
82
|
|
86
83
|
1. 具有 `routes/` 目录。
|
87
84
|
2. 具有 `App.[jt]sx?` 文件。
|
88
|
-
3. 具有 `entry.[jt]sx?` 文件 (需要开启
|
89
|
-
4. 具有 `index.[jt]sx?` 文件(即将废弃)。
|
90
|
-
5. 具有 `pages/` 目录(兼容 Modern.js 1.0)(即将废弃)。
|
85
|
+
3. 具有 `entry.[jt]sx?` 文件 (需要开启 [source.enableCustomEntry](/configure/app/source/enable-custom-entry) 使用)。
|
91
86
|
|
92
87
|
|
93
|
-
当 `src/` 目录满足入口特征时,Modern.js
|
88
|
+
当 `src/` 目录满足入口特征时,Modern.js 会认为当前应用为单入口应用。否则,Modern.js 会扫描 `src/` 下的一级目录,并进一步判断是否为入口,此时应用通常为多入口应用。
|
94
89
|
|
95
90
|
:::tip
|
96
91
|
在单入口应用中,默认的入口名为 `main`。
|
97
|
-
|
98
92
|
:::
|
99
93
|
|
100
|
-
当项目不是单入口应用时,Modern.js 会进一步查看 `src/` 下的一级目录。
|
101
|
-
|
102
94
|
### 框架模式入口
|
103
95
|
|
104
|
-
框架模式指的是需要使用 Modern.js
|
96
|
+
框架模式指的是需要使用 Modern.js 的框架能力,例如约定式路由、SSR、一体化调用等。这类入口约定下,应用中的入口并不是真正的编译入口。Modern.js 在启动时会生成一个封装过的入口,可以在 `node_modules/.modern/[entryName]/index.js` 找到真正的入口。
|
105
97
|
|
106
98
|
#### 约定式路由
|
107
99
|
|
108
|
-
如果入口中存在 `routes/`
|
100
|
+
如果入口中存在 `routes/` 目录,我们称该入口为约定式路由。Modern.js 会在启动时扫描 `routes/` 下的文件,基于文件约定,自动生成客户端路由(react-router)。例如:
|
109
101
|
|
110
102
|
```bash
|
111
103
|
.
|
@@ -121,9 +113,17 @@ import EntryMode from '@site-docs/components/entry-mode.mdx';
|
|
121
113
|
|
122
114
|
#### 自控式路由
|
123
115
|
|
124
|
-
如果入口中存在 `App.[jt]sx?`
|
116
|
+
如果入口中存在 `App.[jt]sx?` 文件,我们称为该入口为自控式路由。例如:
|
125
117
|
|
126
|
-
```
|
118
|
+
```bash
|
119
|
+
.
|
120
|
+
├── src
|
121
|
+
│ └── App.tsx
|
122
|
+
```
|
123
|
+
|
124
|
+
你可以在这个文件中通过代码的方式,设置客户端路由,或者不设置客户端路由。
|
125
|
+
|
126
|
+
```tsx title="src/App.tsx"
|
127
127
|
import { BrowserRouter, Route, Routes } from '@modern-js/runtime/router';
|
128
128
|
|
129
129
|
export default () => {
|
@@ -142,12 +142,14 @@ export default () => {
|
|
142
142
|
|
143
143
|
#### 自定义入口
|
144
144
|
|
145
|
-
如果入口中存在 `entry.[jt]sx` 文件,需要开发者在 `entry.[jt]sx` 文件中调用 `createRoot` 和 `render` 函数,完成项目入口逻辑。
|
146
|
-
|
147
145
|
:::info
|
148
|
-
|
146
|
+
使用该功能需要开启 [source.enableCustomEntry](/configure/app/source/enable-custom-entry)。
|
149
147
|
:::
|
150
148
|
|
149
|
+
默认情况下,使用约定式路由或自控式路由时,Modern.js 会自动完成渲染。如果你希望自定义这个行为,可以通过自定义入口文件的方式来实现。
|
150
|
+
|
151
|
+
如果入口中存在 `entry.[jt]sx` 文件,则 Modern.js 不再控制应用的渲染流程,你可以在 `entry.[jt]sx` 文件中调用 `createRoot` 和 `render` 函数,完成应用入口逻辑。
|
152
|
+
|
151
153
|
```tsx
|
152
154
|
import { createRoot } from '@modern-js/runtime/react';
|
153
155
|
import { render } from '@modern-js/runtime/browser';
|
@@ -158,7 +160,7 @@ render(<ModernRoot />);
|
|
158
160
|
|
159
161
|
```
|
160
162
|
|
161
|
-
|
163
|
+
上述代码中,`createRoot` 函数返回的组件为 `routes/` 目录生成或 `App.tsx` 导出的组件,`render` 函数用于处理渲染与挂载组件。例如,你希望在渲染前执行某些异步任务,可以这样实现:
|
162
164
|
|
163
165
|
```tsx
|
164
166
|
import { createRoot } from '@modern-js/runtime/react';
|
@@ -167,7 +169,7 @@ import { render } from '@modern-js/runtime/browser';
|
|
167
169
|
const ModernRoot = createRoot();
|
168
170
|
|
169
171
|
async function beforeRender() {
|
170
|
-
//
|
172
|
+
// some async request
|
171
173
|
}
|
172
174
|
|
173
175
|
beforeRender().then(() => {
|
@@ -175,34 +177,15 @@ beforeRender().then(() => {
|
|
175
177
|
});
|
176
178
|
```
|
177
179
|
|
178
|
-
#### 自定义 Bootstrap
|
179
|
-
|
180
|
-
:::warning
|
181
|
-
即将废弃,推荐使用自定义入口
|
182
|
-
:::
|
183
|
-
|
184
|
-
如果入口中存在 `index.[jt]sx` 文件,并且当文件默认导出函数时,Modern.js 会将默认的 `bootstrap` 函数作为入参传入,并用导出的函数替代默认的 `bootstrap`,这样开发者可以自定义将组件挂载到 DOM 节点上,或在挂载前添加自定义行为。例如:
|
185
|
-
|
186
|
-
```tsx
|
187
|
-
export default (App: React.ComponentType, bootstrap: () => void) => {
|
188
|
-
// do something before bootstrap...
|
189
|
-
initSomething().then(() => {
|
190
|
-
bootstrap();
|
191
|
-
});
|
192
|
-
};
|
193
|
-
```
|
194
|
-
|
195
180
|
### 构建模式入口
|
196
181
|
|
197
|
-
构建模式指的是不使用 Modern.js 提供的 Runtime 能力,而是完全由开发者自行定义页面的入口。
|
198
|
-
|
199
|
-
当入口目录中存在 `index.[jt]sx`(即将废弃) 并且没有通过 `export default` 导出函数或者入口目录存在 `entry.[jt]sx` 并且未安装 `@modern-js/runtime` 依赖时,对应文件就会被识别为 webpack 或 Rspack 的 entry 模块。
|
200
|
-
|
201
182
|
:::info
|
202
|
-
|
183
|
+
使用该功能需要开启需要开启 [source.enableCustomEntry](/configure/app/source/enable-custom-entry)。
|
203
184
|
:::
|
204
185
|
|
205
|
-
|
186
|
+
构建模式指的是不使用 Modern.js 提供的 Runtime 能力,只使用 Modern.js 构建能力的开发模式。当应用中未安装 `@modern-js/runtime` 依赖时,Modern.js 会认为当前应用所有的入口都是构建模式入口。
|
187
|
+
|
188
|
+
此时,如果入口中存在 `entry.[jt]sx`,则该文件会被识别为 webpack 或 Rspack 的构建入口。此时,Modern.js 不会自动生成入口代码,你需要自行将组件挂载到 DOM 节点上,例如:
|
206
189
|
|
207
190
|
```js title=src/entry.tsx
|
208
191
|
import React from 'react';
|
@@ -212,18 +195,21 @@ import App from './App';
|
|
212
195
|
ReactDOM.render(<App />, document.getElementById('root'));
|
213
196
|
```
|
214
197
|
|
215
|
-
|
198
|
+
在构建模式入口中,**将无法使用 Modern.js 框架的运行时能力**,比如:
|
199
|
+
- 约定式路由,即基于 `src/routes` 下文件的路由
|
200
|
+
- 服务端渲染(SSR)
|
201
|
+
- `modern.config.js` 文件中的 `runtime` 配置将不会再生效
|
216
202
|
|
217
|
-
##
|
203
|
+
## 在配置文件中指定入口
|
218
204
|
|
219
205
|
在某些情况下,你可能需要自定义入口配置,而不是使用 Modern.js 提供的入口约定。
|
220
206
|
|
221
|
-
比如你需要将一个非 Modern.js
|
207
|
+
比如你需要将一个非 Modern.js 应用迁移到 Modern.js,它并不是按照 Modern.js 的目录结构来搭建的。如果你要将它改成 Modern.js 约定的目录结构,会存在一定的迁移成本。这种情况下,你就可以使用自定义入口。
|
222
208
|
|
223
209
|
Modern.js 提供了以下配置项,你可以在 [modern.config.ts](/configure/app/usage) 中配置它们:
|
224
210
|
|
225
211
|
- [source.entries](/configure/app/source/entries):用于设置自定义的入口对象。
|
226
|
-
- [source.disableDefaultEntries](/configure/app/source/disable-default-entries):用于关闭 Modern.js
|
212
|
+
- [source.disableDefaultEntries](/configure/app/source/disable-default-entries):用于关闭 Modern.js 默认的入口扫描行为。当你使用自定义入口时,应用的部分结构可能会恰巧命中 Modern.js 约定的目录结构,但你可能不希望 Modern.js 为你自动生成入口配置,开启该选项可以避免这个问题。
|
227
213
|
|
228
214
|
### 示例
|
229
215
|
|
@@ -247,4 +233,37 @@ export default defineConfig({
|
|
247
233
|
});
|
248
234
|
```
|
249
235
|
|
250
|
-
|
236
|
+
值得注意的是,默认情况下,Modern.js 认为通过配置指定的入口是**框架模式入口**,将自动生成真正的编译入口。如果你的应用是从 Webpack 或 Vite 等构建工具迁移到 Modern.js 框架时,你通常需要在入口配置中开启 `disableMount` 选项,此时 Modern.js 认为该入口是**构建模式入口**。
|
237
|
+
|
238
|
+
|
239
|
+
## 弃用功能
|
240
|
+
|
241
|
+
目前,如果入口所在的目录满足以下条件,也会成为应用入口。
|
242
|
+
|
243
|
+
1. 具有 `index.[jt]sx?` 文件。
|
244
|
+
2. 具有 `pages/` 目录。
|
245
|
+
|
246
|
+
|
247
|
+
`pages/` 目录为 Modern.js 旧版本中的约定式路由,新版本中推荐使用 `routes/` 目录。
|
248
|
+
|
249
|
+
`index.[jt]sx?` 在旧版本中支持应用自定义 Bootstrap 逻辑和构建模式入口,新版本中推荐使用 `entry.[jt]sx?` 代替。
|
250
|
+
|
251
|
+
### 自定义 Bootstrap
|
252
|
+
|
253
|
+
当入口中存在 `index.[jt]sx` 文件,并且当文件默认导出函数时,Modern.js 会将默认的 `bootstrap` 函数作为入参传入,并用导出的函数替代默认的 `bootstrap`,这样开发者可以自定义将组件挂载到 DOM 节点上,或在挂载前添加自定义行为。例如:
|
254
|
+
|
255
|
+
```tsx
|
256
|
+
export default (App: React.ComponentType, bootstrap: () => void) => {
|
257
|
+
// do something before bootstrap...
|
258
|
+
initSomething().then(() => {
|
259
|
+
bootstrap();
|
260
|
+
});
|
261
|
+
};
|
262
|
+
```
|
263
|
+
|
264
|
+
### 构建模式入口
|
265
|
+
|
266
|
+
当入口目录中存在 `index.[jt]sx`(即将废弃) 并且没有通过 `export default` 导出函数时,该入口也将被认为是构建模式入口。
|
267
|
+
|
268
|
+
|
269
|
+
|
@@ -50,10 +50,10 @@ Modern.js 能为开发者提供极致的**开发体验(Development Experience
|
|
50
50
|
|
51
51
|
- 🚀 **Rust 构建**:提供双构建工具支持,轻松切换到 Rspack 构建工具,编译飞快。
|
52
52
|
- 🪜 **渐进式**:使用最精简的模板创建项目,通过生成器逐步开启插件功能,定制解决方案。
|
53
|
-
- 🏠 **一体化**:开发与生产环境 Web Server
|
53
|
+
- 🏠 **一体化**:开发与生产环境 Web Server 逻辑一致,CSR 和 SSR 同构开发,函数即接口的 API 服务调用。
|
54
54
|
- 📦 **开箱即用**:默认 TS 支持,内置构建、ESLint、调试工具,全功能可测试。
|
55
55
|
- 🌏 **周边生态**:自研状态管理、微前端、模块打包等周边需求。
|
56
|
-
- 🕸
|
56
|
+
- 🕸 **约定式路由**:使用基于文件约定的路由,帮助开发者快速搭建应用。
|
57
57
|
|
58
58
|
## 和其他框架的对比
|
59
59
|
|
@@ -74,7 +74,6 @@ export default defineConfig({
|
|
74
74
|
- 集成 Modern.js Core,提供配置解析、插件加载等能力。
|
75
75
|
- 集成 Modern.js Builder,提供构建能力。
|
76
76
|
- 集成 Modern.js Server,提供开发和生产服务器相关能力。
|
77
|
-
- 集成一些最为常用的插件,比如 `plugin-lint`、`plugin-data-loader` 等。
|
78
77
|
|
79
78
|
`@modern-js/app-tools` 是基于 Modern.js 的插件体系实现的,本质上是一个插件,因此你需要在配置文件的 `plugins` 字段中注册 `appTools`:
|
80
79
|
|
@@ -30,8 +30,6 @@ Modern.js 提供对 [Garfish](https://www.garfishjs.org/) 微前端框架开箱
|
|
30
30
|
|
31
31
|
Modern.js 可以与社区中任意的状态管理库搭配使用,比如 [Redux](https://redux.js.org/)、[Jotai](https://jotai.org/)、[Zustand](https://docs.pmnd.rs/zustand)、[Valtio](https://valtio.pmnd.rs/) 等。
|
32
32
|
|
33
|
-
Modern.js 也基于 Redux 封装了 Reduck 状态管理库,你可以参考 [「Reduck 状态管理」](/guides/topic-detail/model/quick-start)来使用。
|
34
|
-
|
35
33
|
## 包管理器
|
36
34
|
|
37
35
|
Modern.js 可以与社区中任意的包管理器搭配使用,比如 [npm](https://www.npmjs.com/package/npm)、[yarn](https://classic.yarnpkg.com/lang/en/)、[pnpm](https://pnpm.io/) 或 [Bun](https://bun.sh/)。
|
@@ -38,9 +38,23 @@ import ReleaseNote from '@site-docs/components/release-note';
|
|
38
38
|
|
39
39
|
:::tip
|
40
40
|
当升级时,需要对 Modern.js 官方提供的所有包做统一升级,而不是升级单个依赖。
|
41
|
-
|
42
41
|
:::
|
43
42
|
|
43
|
+
## 版本管理策略
|
44
|
+
|
45
|
+
在 Modern.js 项目中,我们推荐所有官方提供的依赖都使用固定版本号,不使用 `^` 或 `~` 进行范围声明。例如:
|
46
|
+
|
47
|
+
```json
|
48
|
+
{
|
49
|
+
"dependencies": {
|
50
|
+
"@modern-js/app-tools": "x.y.z"
|
51
|
+
}
|
52
|
+
}
|
53
|
+
```
|
54
|
+
|
55
|
+
这样可以确保依赖的版本是完全确定的,从而保证构建的一致性和可预测性。
|
56
|
+
|
57
|
+
|
44
58
|
## 锁定子依赖
|
45
59
|
|
46
60
|
当项目某个子依赖出现问题,而 Modern.js 无法立即更新时,可以使用包管理器锁定子依赖版本。
|
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-20240822170648",
|
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-20240822170648"
|
26
26
|
},
|
27
27
|
"peerDependencies": {
|
28
|
-
"@modern-js/builder-doc": "0.0.0-nightly-
|
28
|
+
"@modern-js/builder-doc": "0.0.0-nightly-20240822170648"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
31
|
"@rspress/shared": "1.27.0",
|
@@ -39,7 +39,7 @@
|
|
39
39
|
"rspress": "1.27.0",
|
40
40
|
"ts-node": "^10.9.1",
|
41
41
|
"typescript": "^5",
|
42
|
-
"@modern-js/builder-doc": "0.0.0-nightly-
|
42
|
+
"@modern-js/builder-doc": "0.0.0-nightly-20240822170648"
|
43
43
|
},
|
44
44
|
"scripts": {
|
45
45
|
"dev": "rspress dev",
|