@modern-js/main-doc 2.17.1 → 2.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/docs/en/apis/app/hooks/src/index_.mdx +1 -1
  3. package/docs/en/apis/app/runtime/core/bootstrap.mdx +1 -1
  4. package/docs/en/configure/app/output/ssg.mdx +3 -1
  5. package/docs/en/configure/app/source/enable-async-entry.mdx +2 -2
  6. package/docs/en/configure/app/source/entries.mdx +2 -0
  7. package/docs/en/configure/app/tools/tailwindcss.mdx +30 -2
  8. package/docs/en/configure/app/usage.mdx +35 -0
  9. package/docs/en/guides/advanced-features/rspack-start.mdx +1 -3
  10. package/docs/en/guides/advanced-features/ssg.mdx +1 -1
  11. package/docs/en/guides/advanced-features/testing.mdx +1 -1
  12. package/docs/en/guides/basic-features/routes.mdx +4 -1
  13. package/docs/en/guides/concept/entries.mdx +35 -13
  14. package/docs/en/guides/topic-detail/framework-plugin/hook-list.mdx +4 -4
  15. package/docs/zh/apis/app/hooks/src/index_.mdx +1 -1
  16. package/docs/zh/apis/app/runtime/core/bootstrap.mdx +1 -1
  17. package/docs/zh/configure/app/output/ssg.mdx +3 -1
  18. package/docs/zh/configure/app/source/enable-async-entry.mdx +2 -2
  19. package/docs/zh/configure/app/source/entries.mdx +2 -0
  20. package/docs/zh/configure/app/tools/tailwindcss.mdx +31 -3
  21. package/docs/zh/configure/app/usage.mdx +35 -0
  22. package/docs/zh/guides/advanced-features/rspack-start.mdx +1 -3
  23. package/docs/zh/guides/advanced-features/ssg.mdx +1 -1
  24. package/docs/zh/guides/advanced-features/testing.mdx +1 -1
  25. package/docs/zh/guides/basic-features/routes.mdx +2 -15
  26. package/docs/zh/guides/concept/entries.mdx +41 -21
  27. package/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx +4 -4
  28. package/package.json +5 -5
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @modern-js/main-doc
2
2
 
3
+ ## 2.18.0
4
+
5
+ ### Patch Changes
6
+
7
+ - d0a9bf8: chore: improve entry docs, fix ssg docs
8
+ chore: 优化入口文档,修复 ssg 文档
9
+ - @modern-js/builder-doc@2.18.0
10
+
3
11
  ## 2.17.1
4
12
 
5
13
  ### Patch Changes
@@ -4,7 +4,7 @@ sidebar_position: 4
4
4
  ---
5
5
  # index.[tj]s
6
6
 
7
- Entry identifier if App want use custom entry. In most case, [`App.[tj]sx`](/apis/app/hooks/src/app) hook file can already meet our needs.
7
+ Entry identifier if App want use custom bootstrap. In most case, [`App.[tj]sx`](/apis/app/hooks/src/app) hook file can already meet our needs.
8
8
 
9
9
  When we need to add custom behavior before `bootstrap` or completely take over the webpack entry, we can place `index.[tj]s` in `src/` or entry directory. The following are discussed in two cases:
10
10
 
@@ -3,7 +3,7 @@ title: bootstrap
3
3
  ---
4
4
  # bootstrap
5
5
 
6
- Used to start and mount App, usually without manual calls. This API is only required when using [Custom App](/guides/concept/entries#自定义-app).
6
+ Used to start and mount App, usually without manual calls. This API is only required when using [Custom Bootstrap](/guides/concept/entries#custom-bootstrap).
7
7
 
8
8
  ## Usage
9
9
 
@@ -163,7 +163,9 @@ You can set this to disable the default behavior of a client-side route:
163
163
  ```js
164
164
  export default defineConfig({
165
165
  output: {
166
- preventDefault: ['/user'],
166
+ ssg: {
167
+ preventDefault: ['/user'],
168
+ },
167
169
  },
168
170
  });
169
171
  ```
@@ -33,7 +33,7 @@ Then execute the `dev` or `build` command, and you can see Modern.js automatical
33
33
  node_modules
34
34
  └─ .modern-js
35
35
  └─ main
36
- ├─ bootstrap.js # real entry code
36
+ ├─ bootstrap.jsx # real entry code
37
37
  ├─ index.js # asynchronous entry file
38
38
  └─ index.html
39
39
  ```
@@ -41,7 +41,7 @@ node_modules
41
41
  The `index.js` reads as follows:
42
42
 
43
43
  ```js
44
- import('./bootstrap.js');
44
+ import('./bootstrap.jsx');
45
45
  ```
46
46
 
47
47
  At this point, you can consume any remote modules in the current page.
@@ -13,6 +13,7 @@ type Entries = Record<
13
13
  | {
14
14
  entry: string;
15
15
  disableMount?: boolean;
16
+ customBootstrap?: string;
16
17
  }
17
18
  >;
18
19
  ```
@@ -81,6 +82,7 @@ When the value is `Object`, the following properties can be configured:
81
82
 
82
83
  - `entry`: `string`, entry file path.
83
84
  - `disableMount`: `boolean = false`, turn off the entry-scanning behavior of Modern.js.
85
+ - `customBootstrap`: `string = ''`, [Custom Bootstrap](/guides/concept/entries#custom-bootstrap) file path。
84
86
 
85
87
  ```ts title="modern.config.ts"
86
88
  import { defineConfig } from '@modern-js/app-tools';
@@ -24,9 +24,37 @@ const tailwind = {
24
24
  };
25
25
  ```
26
26
 
27
- When the value is of type `Object`, rhe configuration corresponding to [TailwindCSS](https://tailwindcss.com/docs/configuration) is merged with the default configuration through `Object.assign`.
27
+ ### Function Type
28
28
 
29
- When the value is of type `Function`, the object returned by the function is merged with the default configuration by `Object.assign`.
29
+ When `tools.tailwindcss`'s type is Function, the default tailwindcss config will be passed in as the first parameter, the config object can be modified directly, or a value can be returned as the final result.
30
+
31
+ ```ts title="modern.config.ts"
32
+ export default {
33
+ tools: {
34
+ tailwindcss(config) {
35
+ config.content.push('./some-folder/**/*.{js,ts}');
36
+ },
37
+ },
38
+ };
39
+ ```
40
+
41
+ ### Object Type
42
+
43
+ When `tools.tailwindcss`'s type is `Object`, the config will be shallow merged with default config by `Object.assign`.
44
+
45
+ ```ts title="modern.config.ts"
46
+ export default {
47
+ tools: {
48
+ tailwindcss: {
49
+ plugins: [
50
+ require('@tailwindcss/forms'),
51
+ require('@tailwindcss/aspect-ratio'),
52
+ require('@tailwindcss/typography'),
53
+ ],
54
+ },
55
+ },
56
+ };
57
+ ```
30
58
 
31
59
  ### Limitations
32
60
 
@@ -43,6 +43,15 @@ export default defineConfig({
43
43
  });
44
44
  ```
45
45
 
46
+ When using Rspack as the bundler, due to some differences in configuration types between webpack and Rspack, you need to specify `<'rspack'>` generic type for `defineConfig`:
47
+
48
+ ```diff title=modern.config.ts
49
+ - export default defineConfig({
50
+ + export default defineConfig<'rspack'>({
51
+ //...
52
+ });
53
+ ```
54
+
46
55
  ### modern.config.js
47
56
 
48
57
  If you are developing a non-TypeScript project, you can use the configuration file in .js format:
@@ -242,3 +251,29 @@ const mergedConfig = {
242
251
  },
243
252
  };
244
253
  ```
254
+
255
+ ## Configuration Type
256
+
257
+ Modern.js exports `AppUserConfig` type, which corresponds to the type of Modern.js configuration object:
258
+
259
+ ```ts title="modern.config.ts"
260
+ import type { AppUserConfig } from '@modern-js/app-tools';
261
+
262
+ const config: AppUserConfig = {
263
+ tools: {
264
+ webpack: {},
265
+ },
266
+ };
267
+ ```
268
+
269
+ When using Rspack as the bundler, due to some differences in configuration types between webpack and Rspack, you need to specify `<'rspack'>` generic type for `defineConfig`:
270
+
271
+ ```ts title="modern.config.ts"
272
+ import type { AppUserConfig } from '@modern-js/app-tools';
273
+
274
+ const config: AppUserConfig<'rspack'> = {
275
+ tools: {
276
+ rspack: {},
277
+ },
278
+ };
279
+ ```
@@ -25,10 +25,8 @@ After the project is created, you can experience the project by running `pnpm ru
25
25
  :::tip
26
26
  When using Rspack as the bundler, the following Features are temporarily unavailable as some of the capabilities are still under development and we will provide support in the future.
27
27
 
28
- - Micro Frontend
29
28
  - Storybook Devtool
30
- - Server Side Rendering + Conventional Routing
31
- - Static Site Generation + Conventional Routing
29
+ - The usage of [useLoader](/guides/basic-features/data-fetch.html) in Client Side Rendering
32
30
 
33
31
  :::
34
32
 
@@ -86,7 +86,7 @@ After executing `pnpm run serve` to start the project, visit the page in the Net
86
86
 
87
87
  ### Self-controlled Routing
88
88
 
89
- **Self-controlled routing** is a custom routing through component code, which requires the application to run to obtain accurate routing information. Therefore, the SSG function cannot be used out of the box. At this time, the user needs to inform the Modern.js framework in advance which routes need to enable the SSG.
89
+ **Self-controlled routing** is a routing through component code, which requires the application to run to obtain accurate routing information. Therefore, the SSG function cannot be used out of the box. At this time, the user needs to inform the Modern.js framework in advance which routes need to enable the SSG.
90
90
 
91
91
  For example, there is the following code, which contains multiple routes. When setting `output.ssg` to `true`, only the entry route '/' will be rendered by default:
92
92
 
@@ -4,7 +4,7 @@ title: Testing
4
4
  ---
5
5
  # Testing
6
6
 
7
- Modern.js inherits the testing capabilities of [Jest](https://jestjs.io/) by default.
7
+ Modern.js integrates the testing capabilities of [Jest](https://jestjs.io/) by default.
8
8
 
9
9
  First need to execute `pnpm run new` enable [unit test/integration test] features:
10
10
 
@@ -388,9 +388,12 @@ export default () => {
388
388
 
389
389
  :::note
390
390
  Modern.js has a series of resource loading and rendering optimizations to the default convention-based routing, and provides out-of-the-box SSR capabilities, when using self-directed routing, need to be packaged by the developer, and it is recommended that developers use convention-based routing.
391
-
392
391
  :::
393
392
 
393
+ use self-controller routing, if the developer turns off the [`runtime.router`](/configure/app/runtime/router) configuration and uses `react-router-dom` directly, then you need to wrap the `Provider` according to the React Router documentation.
394
+
395
+ ```tsx title="src/App.tsx"
396
+
394
397
  ## Other
395
398
 
396
399
  By default, Modern.js turn on the built-in routing scheme, React Router.
@@ -98,27 +98,49 @@ Framework mode refers to the need to use the capabilities of the Modern.js frame
98
98
 
99
99
  #### Conventional Routing
100
100
 
101
- If there is a `routes/` directory in the entry, Modern.js will scan the files under `routes/` at startup, and automatically generate client-side routes (react-router) based on file conventions.
101
+ If there is a `routes/` directory in the entry, Modern.js will scan the files under `routes/` at startup, and automatically generate client-side routes (react-router) based on file conventions. For example:
102
102
 
103
- For details, please refer to [routing](/guides/basic-features/routes).
103
+ ```bash
104
+ .
105
+ ├── src
106
+ │ └── routes
107
+ │ ├── layout.tsx
108
+ │ └── page.tsx
109
+ ```
104
110
 
105
- #### Custom Routing
111
+ For details, please refer to [routing](/guides/basic-features/routes#conventional-routing).
112
+
113
+ #### Self-controlled Routing
106
114
 
107
115
  If there is an `App.[jt]sx?` file in the entry, the developer can freely set the client route in this file, or not set the client route.
108
116
 
109
- For details, please refer to [routing](/guides/basic-features/routes).
117
+ ```tsx
118
+ import { BrowserRouter, Route, Routes } from '@modern-js/runtime/router';
119
+
120
+ export default () => {
121
+ return (
122
+ <BrowserRouter>
123
+ <Routes>
124
+ <Route index element={<div>index</div>} />
125
+ <Route path="about" element={<div>about</div>} />
126
+ </Routes>
127
+ </BrowserRouter>
128
+ );
129
+ };
130
+ ```
110
131
 
111
- #### Custom App
132
+ For details, please refer to [routing](/guides/basic-features/routes#self-controlled-routing).
112
133
 
113
- If there is an `index.[jt]sx` file in the entry, and when the file exports functions by default, Modern.js will still generate the code wrapped by createApp according to the runtime settings. In the rendering process, the component wrapped by createApp is passed as a parameter to the function exported by the index file, so that developers can customize the component to be mounted on the DOM node, or add custom behavior before mounting. For example:
134
+ #### Custom Bootstrap
114
135
 
115
- ```tsx
116
- import ReactDOM from 'react-dom/client';
117
- import { bootstrap } from '@modern-js/runtime';
136
+ If there is an `index.[jt]sx` file in the entry, and when the file defaults to exporting functions, Modern.js will pass the default bootstrap function as an imported parameter, and replace the default bootstrap with the exported function, so that developers can customize Mounting components to DOM nodes, or adding custom behavior before mounting. E.g:
118
137
 
119
- export default (App: React.ComponentType) => {
138
+ ```tsx
139
+ export default (App: React.ComponentType, bootstrap: () => void) => {
120
140
  // do something before bootstrap...
121
- bootstrap(App, 'root', undefined, ReactDOM);
141
+ initSomething().then(() => {
142
+ bootstrap();
143
+ })
122
144
  };
123
145
  ```
124
146
 
@@ -170,9 +192,9 @@ import App from './App';
170
192
  ReactDOM.render(<App />, document.getElementById('root'));
171
193
  ```
172
194
 
173
- Modern.js **not recommended** to use this method, this method loses some capabilities of the framework, such as the `runtime` configuration in the **`modern.config.js` file will no longer take effect**. But this method will be very useful when the project is migrated from other frameworks to Modern.js, such as CRA, or webpack that is manually built by yourself.
195
+ Modern.js **not recommended** new project to use this method, this method loses some capabilities of the framework, such as the `runtime` configuration in the **`modern.config.js` file will no longer take effect**. But this method will be very useful when the project is migrated from other frameworks to Modern.js, such as CRA, or webpack that is manually built by yourself.
174
196
 
175
- ## Custom Entry
197
+ ## Specify Entry Using Configuration
176
198
 
177
199
  Most existing projects are not built according to the directory convention of Modern.js. If you want to change to the directory structure agreed by Modern.js, there will be a certain migration cost.
178
200
 
@@ -611,7 +611,7 @@ export default (): CliPlugin => ({
611
611
 
612
612
  This adds a new Script tag to the HTML template.
613
613
 
614
- <!-- ## Server
614
+ {/* ## Server
615
615
 
616
616
  :::note
617
617
  The Server plugin is currently not fully opened, and the API is not guaranteed to be stable. Use with caution.
@@ -691,7 +691,7 @@ export default (): ServerPlugin => ({
691
691
  };
692
692
  },
693
693
  });
694
- ``` -->
694
+ ```*/}
695
695
 
696
696
  ## Runtime
697
697
 
@@ -762,7 +762,7 @@ export default (): Plugin => ({
762
762
  });
763
763
  ```
764
764
 
765
- <!-- ### `provide`
765
+ {/* ### `provide`
766
766
 
767
767
  - Function: Modifies the Elements that need to be rendered.
768
768
  - Execution Stage: Rendering (SSR/CSR).
@@ -834,4 +834,4 @@ export default (): Plugin => ({
834
834
  };
835
835
  },
836
836
  });
837
- ``` -->
837
+ ``` */}
@@ -4,7 +4,7 @@ sidebar_position: 4
4
4
  ---
5
5
  # index.[tj]s
6
6
 
7
- 应用项目自定义路由入口标识。
7
+ 应用项目入口标识。
8
8
 
9
9
  通常情况下 [`src/App.[tj]sx, src/[entry]/App.[tj]sx`](/apis/app/hooks/src/app) 钩子文件已经能满足我们的需求,当我们需要在 `bootstrap` 之前添加自定义行为或者完全接管 webpack 打包入口时,可以在 `src` 或者入口目录下放置 `index.[tj]s`。 下面有分两种情况进行讨论:
10
10
 
@@ -3,7 +3,7 @@ title: bootstrap
3
3
  ---
4
4
  # bootstrap
5
5
 
6
- 用于启动和挂载应用,通常情况下不做手动调用。只有在使用[自定义 App](/guides/concept/entries#自定义-app) 时,才需要使用该 API。
6
+ 用于启动和挂载应用,通常情况下不做手动调用。只有在使用[自定义 Bootstrap](/guides/concept/entries#自定义-bootstrap) 时,才需要使用该 API。
7
7
 
8
8
  ## 使用姿势
9
9
 
@@ -163,7 +163,9 @@ export default defineConfig({
163
163
  ```js
164
164
  export default defineConfig({
165
165
  output: {
166
- preventDefault: ['/user'],
166
+ ssg: {
167
+ preventDefault: ['/user'],
168
+ },
167
169
  },
168
170
  });
169
171
  ```
@@ -36,7 +36,7 @@ export default defineConfig({
36
36
  node_modules
37
37
  └─ .modern-js
38
38
  └─ main
39
- ├─ bootstrap.js # 真正的入口代码
39
+ ├─ bootstrap.jsx # 真正的入口代码
40
40
  ├─ index.js # 异步入口文件(asynchronous boundary)
41
41
  └─ index.html
42
42
  ```
@@ -44,7 +44,7 @@ node_modules
44
44
  其中 `index.js` 的内容如下:
45
45
 
46
46
  ```js
47
- import('./bootstrap.js');
47
+ import('./bootstrap.jsx');
48
48
  ```
49
49
 
50
50
  此时,就可以在当前页面中消费任意的远程模块了。
@@ -13,6 +13,7 @@ type Entries = Record<
13
13
  | {
14
14
  entry: string;
15
15
  disableMount?: boolean;
16
+ customBootstrap?: string;
16
17
  }
17
18
  >;
18
19
  ```
@@ -83,6 +84,7 @@ export default defineConfig({
83
84
 
84
85
  - `entry`:`string`,入口文件路径。
85
86
  - `disableMount`:`boolean = false`,关闭 Modern.js 自动生成入口代码的行为。
87
+ - `customBootstrap`: `string = ''`,指定[自定义 Bootstrap](/guides/concept/entries#自定义-bootstrap) 的文件路径。
86
88
 
87
89
  ```ts title="modern.config.ts"
88
90
  import { defineConfig } from '@modern-js/app-tools';
@@ -26,12 +26,40 @@ const tailwind = {
26
26
 
27
27
  对应 [TailwindCSS](https://tailwindcss.com/docs/configuration) 的配置。
28
28
 
29
- 当值为 `Object` 类型时,与默认配置通过 `Object.assign` 合并。
29
+ ### Function 类型
30
30
 
31
- 当值为 `Function` 类型时,函数返回的对象与默认配置通过 `Object.assign` 合并。
31
+ `tools.tailwindcss` Function 类型时,默认配置会作为第一个参数传入,你可以直接修改配置对象,也可以返回一个值作为最终结果:
32
+
33
+ ```ts title="modern.config.ts"
34
+ export default {
35
+ tools: {
36
+ tailwindcss(config) {
37
+ config.content.push('./some-folder/**/*.{js,ts}');
38
+ },
39
+ },
40
+ };
41
+ ```
42
+
43
+ ### Object 类型
44
+
45
+ 当 `tools.tailwindcss` 的值为 `Object` 类型时,会与默认配置通过 `Object.assign` 浅合并。
46
+
47
+ ```ts title="modern.config.ts"
48
+ export default {
49
+ tools: {
50
+ tailwindcss: {
51
+ plugins: [
52
+ require('@tailwindcss/forms'),
53
+ require('@tailwindcss/aspect-ratio'),
54
+ require('@tailwindcss/typography'),
55
+ ],
56
+ },
57
+ },
58
+ };
59
+ ```
32
60
 
33
61
  ### 限制
34
62
 
35
63
  注意,该配置中不允许使用 `theme` 配置项,否则会构建失败。在 Modern.js 中,请使用 [source.designSystem](/configure/app/source/design-system) 作为 `Tailwind CSS Theme` 配置。
36
64
 
37
- 其他的使用方式和 Tailwind CSS 一致: [快速传送门](https://tailwindcss.com/docs/configuration)。
65
+ 其他配置的使用方式和 Tailwind CSS 一致,请参考 [tailwindcss - Configuration](https://tailwindcss.com/docs/configuration)。
@@ -43,6 +43,15 @@ export default defineConfig({
43
43
  });
44
44
  ```
45
45
 
46
+ 当你使用 Rspack 作为打包工具时,由于 webpack 和 Rspack 的配置类型存在一些差异,需要为 `defineConfig` 指定 `<'rspack'>` 泛型:
47
+
48
+ ```diff title=modern.config.ts
49
+ - export default defineConfig({
50
+ + export default defineConfig<'rspack'>({
51
+ // ...
52
+ });
53
+ ```
54
+
46
55
  ### modern.config.js
47
56
 
48
57
  如果你在开发一个非 TypeScript 项目,可以使用 .js 格式的配置文件:
@@ -242,3 +251,29 @@ const mergedConfig = {
242
251
  },
243
252
  };
244
253
  ```
254
+
255
+ ## 配置类型定义
256
+
257
+ Modern.js 导出了 `AppUserConfig` 类型,对应 Modern.js 配置对象的类型:
258
+
259
+ ```ts title="modern.config.ts"
260
+ import type { AppUserConfig } from '@modern-js/app-tools';
261
+
262
+ const config: AppUserConfig = {
263
+ tools: {
264
+ webpack: {},
265
+ },
266
+ };
267
+ ```
268
+
269
+ 当你使用 Rspack 作为打包工具时,由于 webpack 和 Rspack 的配置类型存在一些差异,需要为 `AppUserConfig` 指定 `<'rspack'>` 泛型:
270
+
271
+ ```ts title="modern.config.ts"
272
+ import type { AppUserConfig } from '@modern-js/app-tools';
273
+
274
+ const config: AppUserConfig<'rspack'> = {
275
+ tools: {
276
+ rspack: {},
277
+ },
278
+ };
279
+ ```
@@ -26,10 +26,8 @@ import InitRspackApp from '@site-docs/components/init-rspack-app';
26
26
  :::tip
27
27
  在使用 Rspack 作为打包工具时,由于部分能力尚在开发中,以下 features 暂时无法使用,我们将在未来提供支持:
28
28
 
29
- - 微前端(Micro Frontend)
30
29
  - Storybook 调试
31
- - 同时使用服务端渲染(SSR)和约定式路由的场景
32
- - 同时使用静态站点生成(SSG)和约定式路由的场景
30
+ - 客户端渲染(CSR)使用 [useLoader](/guides/basic-features/data-fetch.html)
33
31
 
34
32
  :::
35
33
 
@@ -86,7 +86,7 @@ export default defineConfig({
86
86
 
87
87
  ### 在自控式路由中使用
88
88
 
89
- **自控式路由**是通过组件代码自定义路由,需要应用运行起来才能获取准确的路由信息。因此,无法开箱即用的使用 SSG 功能。此时需要用户提前告知 Modern.js 框架,哪些路由需要开启 SSG 功能。
89
+ **自控式路由**是通过组件代码定义路由,需要应用运行起来才能获取准确的路由信息。因此,无法开箱即用的使用 SSG 功能。此时需要用户提前告知 Modern.js 框架,哪些路由需要开启 SSG 功能。
90
90
 
91
91
  例如有以下代码,包含多条路由,设置 `output.ssg` 为 `true` 时,默认只会渲染入口路由即 `/`:
92
92
 
@@ -4,7 +4,7 @@ sidebar_position: 10
4
4
 
5
5
  # 测试
6
6
 
7
- Modern.js 默认继承了 [Jest](https://jestjs.io/) 的测试能力。
7
+ Modern.js 默认集成了 [Jest](https://jestjs.io/) 的测试能力。
8
8
 
9
9
  我们首先需要执行 `pnpm run new` 启用「单元测试 / 集成测试」功能:
10
10
 
@@ -488,9 +488,10 @@ export default () => {
488
488
 
489
489
  :::note
490
490
  Modern.js 默认对约定式路由做了一系列资源加载及渲染上的优化,并且提供了开箱即用的 SSR 能力,而这些能力,在使用自控路由时,都需要开发者自行封装,推荐开发者使用约定式路由。
491
-
492
491
  :::
493
492
 
493
+ 使用自控式路由时,如果开发者关闭了 [`runtime.router`](/configure/app/runtime/router) 配置,并直接使用 `react-router-dom`,那还需要根据 React Router 文档自行包裹 `Provider`。
494
+
494
495
  ## 其他路由方案
495
496
 
496
497
  默认情况下,Modern.js 会开启内置的路由方案,即 React Router。
@@ -513,19 +514,5 @@ export default defineConfig({
513
514
  });
514
515
  ```
515
516
 
516
- ### 常见问题
517
-
518
- 1. 产物中的代码是 es2015+ 的,期望产物中是 es5 的代码
519
-
520
- react-router^6 的目前默认产物是 es2020 的,如果需要产物中是 es5 的代码,可以配置 `source.include`,让 react-router 相关包经过 bundler 编译 :
521
-
522
- ```
523
- source: {
524
- source: {
525
- include: [/@remix-run\/router/, /react-router-dom/, /react-router/],
526
- }
527
- }
528
- ```
529
-
530
517
 
531
518
 
@@ -98,36 +98,55 @@ Modern.js 会将与 package.json 文件中 `name` 字段同名的入口作为主
98
98
 
99
99
  #### 约定式路由
100
100
 
101
- 如果入口中存在 `routes/` 目录,Modern.js 会在启动时扫描 `routes/` 下的文件,基于文件约定,自动生成客户端路由(react-router)。
101
+ 如果入口中存在 `routes/` 目录,Modern.js 会在启动时扫描 `routes/` 下的文件,基于文件约定,自动生成客户端路由(react-router)。例如:
102
102
 
103
- 详细内容可以参考[路由](/guides/basic-features/routes)。
104
-
105
- #### 自定义路由
103
+ ```bash
104
+ .
105
+ ├── src
106
+ │ └── routes
107
+ │ ├── layout.tsx
108
+ │ └── page.tsx
109
+ ```
106
110
 
107
- 如果入口中存在 `App.[jt]sx?` 文件,开发者可以在这个文件中自由的设置客户端路由,或者不设置客户端路由。
111
+ 上述目录中,`layout.tsx` 中导出的组件会作为最外层的组件,`page.tsx` 中导出的组件会作为 `/` 路由的组件。
108
112
 
109
- 详细内容可以参考[路由](/guides/basic-features/routes)。
113
+ 详细内容可以参考[路由](/guides/basic-features/routes#约定式路由)。
110
114
 
111
- #### 自定义 App
115
+ #### 自控式路由
112
116
 
113
- 如果入口中存在 `index.[jt]sx` 文件,并且当文件默认导出函数时,Modern.js 还是会根据 runtime 的设置情况生成 createApp 包裹后的代码。在渲染过程中,将 createApp 包裹后的组件作为参数传递给 index 文件导出的函数,这样开发者可以自定义将组件挂载到 DOM 节点上,或在挂载前添加自定义行为。例如:
117
+ 如果入口中存在 `App.[jt]sx?` 文件,开发者可以在这个文件中通过代码的方式,设置客户端路由,或者不设置客户端路由。
114
118
 
115
119
  ```tsx
116
- import ReactDOM from 'react-dom/client';
117
- import { bootstrap } from '@modern-js/runtime';
118
-
119
- export default (App: React.ComponentType) => {
120
- // do something before bootstrap...
121
- bootstrap(App, 'root', undefined, ReactDOM);
120
+ import { BrowserRouter, Route, Routes } from '@modern-js/runtime/router';
121
+
122
+ export default () => {
123
+ return (
124
+ <BrowserRouter>
125
+ <Routes>
126
+ <Route index element={<div>index</div>} />
127
+ <Route path="about" element={<div>about</div>} />
128
+ </Routes>
129
+ </BrowserRouter>
130
+ );
122
131
  };
123
132
  ```
124
133
 
125
- :::warning
126
- 由于 bootstrap 函数需要兼容 React17 和 React18 的用法,调用 bootstrap 函数时需要手动传入 ReactDOM 参数。
134
+ 详细内容可以参考[路由](/guides/basic-features/routes#自控式路由)。
127
135
 
128
- :::
136
+ #### 自定义 Bootstrap
137
+
138
+ 如果入口中存在 `index.[jt]sx` 文件,并且当文件默认导出函数时,Modern.js 会将默认的 bootstrap 函数作为入参传入,并用导出的函数替代默认的 bootstrap,这样开发者可以自定义将组件挂载到 DOM 节点上,或在挂载前添加自定义行为。例如:
139
+
140
+ ```tsx
141
+ export default (App: React.ComponentType, bootstrap: () => void) => {
142
+ // do something before bootstrap...
143
+ initSomething().then(() => {
144
+ bootstrap();
145
+ })
146
+ };
147
+ ```
129
148
 
130
- Modern.js 生成的文件内容如下:
149
+ 此时,Modern.js 生成的文件内容如下:
131
150
 
132
151
  ```js
133
152
  import React from 'react';
@@ -140,13 +159,14 @@ const IS_BROWSER = typeof window !== 'undefined' && window.name !== 'nodejs';
140
159
  const MOUNT_ID = 'root';
141
160
 
142
161
  let AppWrapper = null;
162
+ let root = null;
143
163
 
144
164
  function render() {
145
165
  AppWrapper = createApp({
146
166
  // runtime 的插件参数...
147
167
  })(App);
148
168
  if (IS_BROWSER) {
149
- customBootstrap(AppWrapper);
169
+ customBootstrap(AppWrapper, () => bootstrap(AppWrapper, MOUNT_ID, root, ReactDOM));
150
170
  }
151
171
  return AppWrapper;
152
172
  }
@@ -170,9 +190,9 @@ import App from './App';
170
190
  ReactDOM.render(<App />, document.getElementById('root'));
171
191
  ```
172
192
 
173
- Modern.js **不推荐**使用这种方式,这种方式丧失了框架的一些能力,如 **`modern.config.js` 文件中的 `runtime` 配置将不会再生效**。但是在项目从其他框架迁移到 Modern.js,例如 CRA,或是自己手动搭建的 webpack 时,这种方式会非常有用。
193
+ Modern.js **不推荐**新项目使用这种方式,这种方式丧失了框架的一些能力,如 **`modern.config.js` 文件中的 `runtime` 配置将不会再生效**。但是在项目从其他框架迁移到 Modern.js,例如 CRA,或是自己手动搭建的 webpack 时,这种方式会非常有用。
174
194
 
175
- ## 自定义入口
195
+ ## 使用配置指定入口
176
196
 
177
197
  大部分存量项目并不是按照 Modern.js 的目录结构来搭建的。如果要改成 Modern.js 约定的目录结构,会存在一定的迁移成本。
178
198
 
@@ -611,7 +611,7 @@ export default (): CliPlugin => ({
611
611
 
612
612
  这样就为 HTML 模版中新增了一个 Script 标签。
613
613
 
614
- <!-- ## Server
614
+ {/* ## Server
615
615
 
616
616
  :::note
617
617
  目前 Server 插件还未完全开放,API 不保证稳定,使用需谨慎。
@@ -692,7 +692,7 @@ export default (): ServerPlugin => ({
692
692
  };
693
693
  },
694
694
  });
695
- ``` -->
695
+ ```*/}
696
696
 
697
697
  ## Runtime
698
698
 
@@ -764,7 +764,7 @@ export default (): Plugin => ({
764
764
  });
765
765
  ```
766
766
 
767
- <!-- ### `provide`
767
+ {/* ### `provide`
768
768
 
769
769
  - 功能:修改需要渲染的 Element
770
770
  - 执行阶段:渲染(SSR/CSR)
@@ -836,4 +836,4 @@ export default (): Plugin => ({
836
836
  };
837
837
  },
838
838
  });
839
- ``` -->
839
+ ```*/}
package/package.json CHANGED
@@ -15,13 +15,13 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.17.1",
18
+ "version": "2.18.0",
19
19
  "publishConfig": {
20
20
  "registry": "https://registry.npmjs.org/",
21
21
  "access": "public"
22
22
  },
23
23
  "peerDependencies": {
24
- "@modern-js/builder-doc": "^2.17.1"
24
+ "@modern-js/builder-doc": "^2.18.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "classnames": "^2",
@@ -33,9 +33,9 @@
33
33
  "fs-extra": "^10",
34
34
  "@types/node": "^16",
35
35
  "@types/fs-extra": "^9",
36
- "@modern-js/builder-doc": "2.17.1",
37
- "@modern-js/doc-tools": "2.17.1",
38
- "@modern-js/doc-plugin-auto-sidebar": "2.17.1"
36
+ "@modern-js/builder-doc": "2.18.0",
37
+ "@modern-js/doc-tools": "2.18.0",
38
+ "@modern-js/doc-plugin-auto-sidebar": "2.18.0"
39
39
  },
40
40
  "scripts": {
41
41
  "dev": "modern dev",