@modern-js/main-doc 0.0.0-next-1679558946601 → 0.0.0-next-1679666804179

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.
Files changed (27) hide show
  1. package/CHANGELOG.md +13 -5
  2. package/docs/en/configure/app/source/config-dir.mdx +2 -2
  3. package/docs/en/configure/app/source/design-system.mdx +2 -2
  4. package/docs/en/configure/app/source/disable-default-entries.mdx +12 -12
  5. package/docs/en/configure/app/source/disable-entry-dirs.mdx +3 -4
  6. package/docs/en/configure/app/source/enable-async-entry.mdx +2 -2
  7. package/docs/en/configure/app/source/entries-dir.mdx +10 -10
  8. package/docs/en/configure/app/source/entries.mdx +83 -26
  9. package/docs/en/configure/app/tools/esbuild.mdx +9 -4
  10. package/docs/en/configure/app/tools/jest.mdx +2 -3
  11. package/docs/en/configure/app/tools/swc.mdx +6 -2
  12. package/docs/en/guides/concept/entries.mdx +130 -39
  13. package/docs/en/guides/topic-detail/framework-plugin/hook-list.mdx +7 -3
  14. package/docs/en/guides/topic-detail/model/typescript-best-practice.mdx +4 -4
  15. package/docs/zh/configure/app/source/config-dir.mdx +2 -2
  16. package/docs/zh/configure/app/source/design-system.mdx +3 -3
  17. package/docs/zh/configure/app/source/disable-default-entries.mdx +11 -10
  18. package/docs/zh/configure/app/source/disable-entry-dirs.mdx +2 -3
  19. package/docs/zh/configure/app/source/enable-async-entry.mdx +3 -3
  20. package/docs/zh/configure/app/source/entries-dir.mdx +10 -11
  21. package/docs/zh/configure/app/source/entries.mdx +84 -23
  22. package/docs/zh/configure/app/tools/esbuild.mdx +9 -5
  23. package/docs/zh/configure/app/tools/jest.mdx +2 -3
  24. package/docs/zh/configure/app/tools/swc.mdx +6 -2
  25. package/docs/zh/guides/concept/entries.mdx +36 -35
  26. package/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx +6 -2
  27. package/package.json +5 -5
@@ -4,26 +4,29 @@ sidebar_position: 1
4
4
 
5
5
  # Entries
6
6
 
7
- Entries are Modern.js default file convention, and each entry in the project is a separate page, corresponding to a server level route.
7
+ Through this chapter, you can learn about the entry convention in Modern.js and how to customize the entry.
8
8
 
9
- Many configurations, such as HTML templates, Meta information, whether SSR is enabled, SSG, server level routing rules are divided by the entry dimension.
9
+ ## What is an Entry
10
+
11
+ **Entry is the starting module of a page.**
12
+
13
+ In the Modern.js project, each entry corresponds to an independent page, and also corresponds to a server route. By default, Modern.js will automatically determine the entry of the page based on the directory convention, and also supports customizing the entry through configuration items.
14
+
15
+ Many configuration items provided by Modern.js are divided by entry, such as page title, HTML template, page Meta information, whether to enable SSR/SSG, server-side routing rules, etc.
10
16
 
11
17
  ## Single Entry and Multiple Entries
12
18
 
13
- Modern.js initialization project is a single entry, the project structure is as follows:
19
+ The project initialized by Modern.js is single-entry (SPA), and the project structure is as follows:
14
20
 
15
21
  ```
16
22
  .
17
23
  ├── src
18
- │ ├── modern-app-env.d.ts
19
24
  │ └── routes
20
25
  │ ├── index.css
21
26
  │ ├── layout.tsx
22
27
  │ └── page.tsx
23
28
  ├── package.json
24
29
  ├── modern.config.ts
25
- ├── pnpm-lock.yaml
26
- ├── README.md
27
30
  └── tsconfig.json
28
31
  ```
29
32
 
@@ -35,17 +38,16 @@ Modern.js can easily switch from single entry to multiple entry. You can execute
35
38
  ? Entry name: new-entry
36
39
  ```
37
40
 
38
- After execution, the `src/` directory will become the following structure:
41
+ After execution, Modern.js will automatically generate a new entry directory, and you can see that the `src/` directory has the following structure:
39
42
 
40
- ```
43
+ ```bash
41
44
  .
42
- ├── modern-app-env.d.ts
43
- ├── myapp
45
+ ├── myapp # Original entry
44
46
  │ └── routes
45
47
  │ ├── index.css
46
48
  │ ├── layout.tsx
47
49
  │ └── page.tsx
48
- └── new-entry
50
+ └── new-entry # New entry
49
51
  └── routes
50
52
  ├── index.css
51
53
  ├── layout.tsx
@@ -56,65 +58,154 @@ The original code was moved to the directory with the same name as the `name` in
56
58
 
57
59
  After executing `pnpm run dev`, you can see that a `/new-entry` route has been added, and the migrated code route has not changed.
58
60
 
59
- :::note
60
- Modern.js will use the directory with the same name as the `name` in the `package.json` as the main entry, the default route is `/`, and the default route for other entries is `/{entryName}`.
61
+ :::tip
62
+ Modern.js will use the entry with the same name as the `name` field in the package.json file as the main entry, the route of the main entry is `/`, and the route of other entries is `/{entryName}`.
63
+
64
+ For example, when `name` in package.json is `myapp`, `src/myapp` will be used as the main entry of the project.
61
65
 
62
66
  :::
63
67
 
64
- ## Entry conditions
68
+ ## Entry Type
65
69
 
66
- By default, the Modern.js entry currently scans the file under `src/`, identifies the entry, and generates the corresponding server level route.
70
+ Different entry types have different compile and run-time behaviors. When creating a project in Modern.js, developers can manually choose to create a project in **framework mode** or **build mode**. After the creation is complete, you can see that the project template files for different modes are different.
71
+
72
+ By default, Modern.js will scan the files under `src/` before starting the project, identify the entry, and generate the corresponding server-side route.
67
73
 
68
74
  :::tip
69
- You can change the entry directory to another directory by [source.entriesDir](/configure/app/source/entries-dir).
75
+ You can change the entry directory to another directory through [source.entriesDir](/configure/app/source/entries-dir).
70
76
 
71
77
  :::
72
78
 
73
- Not all first-level directories under `src/` will become project entrances. The directory where the entry is located must meet one of the following four conditions:
79
+ Not all first-level directories under `src/` will become project entries, and the directory where the entry is located must meet one of the following four conditions:
74
80
 
75
- 1. Directory with `routes/`
76
- 2. Has the `App.[jt]sx?` file
77
- 3. With `index.[jt]sx?` file
78
- 4. With `pages/` directory (compatible Modern.js 1.0)
81
+ 1. Have a `routes/` directory
82
+ 2. Has `App.[jt]sx?` file
83
+ 3. Has `index.[jt]sx?` file
84
+ 4. Has a `pages/` directory (Modern.js 1.0 compatible)
79
85
 
80
- When the `src/` directory satisfies the entry feature, the Modern.js considers the current project to be a single entry application.
86
+ When the `src/` directory meets the entry characteristics, Modern.js will consider the current project as a single-entry application.
81
87
 
82
88
  :::tip
83
- Single entry The default entry name is `main`.
89
+ In single-entry applications, the default entry is named `main`.
84
90
 
85
91
  :::
86
92
 
87
- When the project is not a single-entry application, Modern.js further look at the first-level directory under `src/`.
93
+ When the project is not a single-entry application, Modern.js will further check the first-level directory under `src/`.
94
+
95
+ ### Framework Mode Entry
88
96
 
89
- ## Difference between entries
97
+ Framework mode refers to the need to use the capabilities of the Modern.js framework, such as Router, SSR, integrated calls, etc. Under this kind of entry agreement, the entry defined by the developer is not the real Webpack compilation entry. Modern.js will generate an encapsulated entry when it starts, and the real entry can be found in `node_modules/.modern/{entryName}/index.js`.
90
98
 
91
- Entries to different conventions have different behaviors.
99
+ #### Conventional Routing
92
100
 
93
- ### routes
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.
94
102
 
95
- If the entry is the `routes/` convention, Modern.js will scan the files under `routes` at startup, and automatically generate the client route based on the file convention(react-router).
103
+ For details, please refer to [routing](/guides/basic-features/routes).
96
104
 
97
- For details, please refer to [Routing](/guides/basic-features/routes).
105
+ #### Custom Routing
98
106
 
99
- ### App
107
+ 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.
100
108
 
101
- If the entry is the `App.[jt]sx?` convention, the developer can freely set the client route in this file, or not set the client route.
109
+ For details, please refer to [routing](/guides/basic-features/routes).
102
110
 
103
- For details, please refer to [Routing](/guides/basic-features/routes).
111
+ #### Custom App
104
112
 
105
- ### Index
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:
106
114
 
107
- Typically, the above two modes are sufficient, but when developers need to take over the React mount logic themselves, or take over the Webpack entry entirely, the `index.[jt]sx?`convention can be used.
115
+ ```tsx
116
+ import ReactDOM from 'react-dom/client';
117
+ import { bootstrap } from '@modern-js/runtime';
108
118
 
109
- If the entry is the `index.[jt]sx?` convention, the Modern.js determines the build behavior based on whether the file has a default component export.
119
+ export default (App: React.ComponentType) => {
120
+ // do something before bootstrap...
121
+ bootstrap(App, 'root', undefined, ReactDOM);
122
+ };
123
+ ```
124
+
125
+ :::warning
126
+ Since the bootstrap function needs to be compatible with React17 and React18, you need to manually pass in ReactDOM parameters when calling the bootstrap function.
127
+
128
+ :::
129
+
130
+ The content of the file generated by Modern.js is as follows:
110
131
 
111
- For details, please refer to [customized App](/guides/concept/entries#自定义-app).
132
+ ```js
133
+ import React from 'react';
134
+ import ReactDOM from 'react-dom/client';
135
+ import customBootstrap from '@_edenx_src/index.tsx';
136
+ import App from '@_edenx_src/App';
137
+ import { router, state } from '@edenx/runtime/plugins';
112
138
 
113
- ## configuration
139
+ const IS_BROWSER = typeof window !== 'undefined' && window.name !== 'nodejs';
140
+ const MOUNT_ID = 'root';
114
141
 
115
- In Modern.js, you can manually configure the entry in `modern.config.[jt]s`, in addition to using the file convention to generate the entry.
142
+ let AppWrapper = null;
143
+
144
+ function render() {
145
+ AppWrapper = createApp({
146
+ // plugin parameters for runtime...
147
+ })(App);
148
+ if (IS_BROWSER) {
149
+ customBootstrap(AppWrapper);
150
+ }
151
+ return AppWrapper;
152
+ }
153
+
154
+ AppWrapper = render();
155
+
156
+ export default AppWrapper;
157
+ ```
158
+
159
+ ### Build Mode Entry
160
+
161
+ Build mode refers to the ability not to use any Modern.js runtime, and the developer defines the entry of the project Webpack completely by himself.
162
+
163
+ If `index.[jt]sx` exists in the entry and there is no default export function, then this file is the real Webpack entry file. This is similar to [Create React App](https://github.com/facebook/create-react-app), you need to mount components to DOM nodes, add hot update code, etc. For example:
164
+
165
+ ```js title=src/index.jsx
166
+ import React from 'react';
167
+ import ReactDOM from 'react-dom';
168
+ import App from './App';
169
+
170
+ ReactDOM.render(<App />, document.getElementById('root'));
171
+ ```
172
+
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.
174
+
175
+ ## Custom Entry
176
+
177
+ 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
+
179
+ In this case, instead of generating the entry using file conventions, you can manually configure the entry in `modern.config.[jt]s`.
180
+
181
+ ```ts title="modern.config.ts"
182
+ export default defineConfig({
183
+ source: {
184
+ entries: {
185
+ // Specify a new entry named entry_customize
186
+ entry_customize: './src/home/test/index.ts',
187
+ },
188
+ // Disable default ingress scanning
189
+ disableDefaultEntries: true,
190
+ },
191
+ });
192
+ ```
193
+
194
+ ### Disable Default Entries
195
+
196
+ When using a custom entry, part of the project structure may happen to hit the directory convention of Modern.js, but in fact this part of the directory is not the real entry.
197
+
198
+ Modern.js provides `disableDefaultEntries` config to disable default entry scanning rules. When you need to customize the entry, you generally need to use `disableDefaultEntries` with `entries`. In this way, some existing projects can be quickly migrated without modifying the directory structure.
199
+
200
+ ```ts title="modern.config.ts"
201
+ export default defineConfig({
202
+ source: {
203
+ disableDefaultEntries: true,
204
+ },
205
+ });
206
+ ```
116
207
 
117
208
  :::tip
118
- Details can be found in [source.entries](/configure/app/source/entries).
209
+ For detailed usage, please refer to [source.entries](/configure/app/source/entries) and [source.disableDefaultEntries](/configure/app/source/disable-default-entries).
119
210
 
120
211
  :::
@@ -191,8 +191,8 @@ foo
191
191
 
192
192
  - Functionality: Reset some file states before exiting the process.
193
193
  - Execution phase: Before the process exits.
194
- - Hook model: AsyncWorkflow
195
- - Type: `AsyncWorkflow<void, void>`
194
+ - Hook model: Workflow
195
+ - Type: `Workflow<void, void>`
196
196
  - Example usage:
197
197
 
198
198
  ```ts
@@ -209,6 +209,10 @@ export default (): CliPlugin => ({
209
209
  });
210
210
  ```
211
211
 
212
+ :::tip
213
+ Since the callback function when exiting the process in Node.js is synchronous, the type of `beforeExit` Hook is `Workflow` and cannot perform asynchronous operations.
214
+ :::
215
+
212
216
  ### `beforeDev`
213
217
 
214
218
  - Functionality: Tasks before running the main dev process.
@@ -442,7 +446,7 @@ export default (): CliPlugin => ({
442
446
  modifyEntryRuntimePlugins({ entrypoint, plugins }) {
443
447
  const name = 'customPlugin';
444
448
  const options = {
445
- /** 可序列化的内容 */
449
+ /** serializable content */
446
450
  };
447
451
 
448
452
  return {
@@ -47,7 +47,7 @@ export const bar = model<State>('bar').define({
47
47
  data: '',
48
48
  },
49
49
  computed: {
50
- // fooState 指定类型
50
+ // specify the type for fooState
51
51
  withFoo: [foo, (state, fooState: FooState) => state.data + fooState.data],
52
52
  },
53
53
  });
@@ -62,11 +62,11 @@ Reduck provides a set of utility types for getting Model type information:
62
62
 
63
63
  ```ts
64
64
  export const foo = model<State2>('foo').define({
65
- // 省略
65
+ // skip some codes
66
66
  });
67
67
 
68
- // 获取 foo State 类型
68
+ // get the State type of foo
69
69
  let fooActions: GetModelActions<typeof foo>;
70
- // 获取 foo Actions 类型
70
+ // get the Actions type of foo
71
71
  let fooState: GetModelState<typeof foo>;
72
72
  ```
@@ -1,8 +1,8 @@
1
1
  ---
2
- title: source.configDir
3
2
  sidebar_label: configDir
4
3
  ---
5
- # configDir
4
+
5
+ # source.configDir
6
6
 
7
7
  - **类型:** `string`
8
8
  - **默认值:** `./config`
@@ -1,11 +1,11 @@
1
1
  ---
2
- title: source.designSystem
3
2
  sidebar_label: designSystem
4
3
  ---
5
- # designSystem
4
+
5
+ # source.designSystem
6
6
 
7
7
  - **类型:** `Object`
8
- - **默认值:**见下方配置详情。
8
+ - **默认值:** 见下方配置详情。
9
9
 
10
10
  :::caution 注意
11
11
  需要先通过 `pnpm run new` 启用 Tailwind CSS 功能。
@@ -1,22 +1,16 @@
1
1
  ---
2
- title: source.disableDefaultEntries
3
2
  sidebar_label: disableDefaultEntries
4
3
  ---
5
- # disableDefaultEntries
4
+
5
+ # source.disableDefaultEntries
6
6
 
7
7
  - **类型:** `boolean`
8
8
  - **默认值:** `false`
9
9
 
10
- 关闭根据项目目录结构自动识别应用构建入口的功能,默认情况下,Modern.js 会根据项目目录结构得到对应构建入口。
10
+ 用于关闭基于目录结构来自动识别页面入口的功能。
11
11
 
12
12
  :::info
13
- 默认情况下,Modern.js 会根据项目目录结构得到对应入口信息。具体可参考[入口](/guides/concept/entries)。
14
- 配置关闭改机制后,需要使用 [`source.entries`](/configure/app/source/entries) 配置自定义入口。
15
-
16
- :::
17
-
18
- :::warning 警告
19
- 推荐按照 Modern.js 提供的目录规范组织代码可以更好使用框架的功能,避免一些冗余的配置。
13
+ 默认情况下,Modern.js 会基于目录约定来自动确定页面的入口,具体可参考[入口](/guides/concept/entries)。
20
14
 
21
15
  :::
22
16
 
@@ -29,3 +23,10 @@ export default defineConfig({
29
23
  },
30
24
  });
31
25
  ```
26
+
27
+ 关闭默认行为后,你需要使用 [`source.entries`](/configure/app/source/entries) 配置自定义的入口。
28
+
29
+ :::warning
30
+ 我们推荐使用 Modern.js 提供的目录规范来组织代码,从而更好地使用框架功能,避免一些冗余的配置。
31
+
32
+ :::
@@ -1,9 +1,8 @@
1
1
  ---
2
- title: source.disableEntryDirs
3
-
4
2
  sidebar_label: disableEntryDirs
5
3
  ---
6
- # disableEntryDirs
4
+
5
+ # source.disableEntryDirs
7
6
 
8
7
  - **类型:** `string[]`
9
8
  - **默认值:** `[]`
@@ -1,13 +1,13 @@
1
1
  ---
2
- title: source.enableAsyncEntry
3
2
  sidebar_label: enableAsyncEntry
4
3
  ---
5
- # enableAsyncEntry
4
+
5
+ # source.enableAsyncEntry
6
6
 
7
7
  - **类型:** `boolean`
8
8
  - **默认值:** `false`
9
9
 
10
- 该选项用于 webpack 模块联邦 (Module Federation)场景。
10
+ 该选项用于 webpack 模块联邦(Module Federation)场景。
11
11
 
12
12
  开启此选项后,Modern.js 会通过 Dynamic Import 来包裹自动生成的入口文件(asynchronous boundary),使页面代码可以消费模块联邦生成的远程模块。
13
13
 
@@ -1,14 +1,13 @@
1
1
  ---
2
- title: source.entriesDir
3
-
4
2
  sidebar_label: entriesDir
5
3
  ---
6
- # entriesDir
4
+
5
+ # source.entriesDir
7
6
 
8
7
  - **类型:** `string`
9
8
  - **默认值:** `./src`
10
9
 
11
- 默认会根据 `src` 目录识别应用入口,可通过该选项自定义应用入口的识别目录。
10
+ Modern.js 默认会扫描 `src` 目录来识别页面入口,你可以通过该选项自定义页面入口的识别目录。
12
11
 
13
12
  例如,当配置与目录结构如下时:
14
13
 
@@ -25,16 +24,16 @@ export default defineConfig({
25
24
  └── src
26
25
  └── pages
27
26
  ├── a
28
- │ └── App.jsx
27
+ │ └── App.tsx
29
28
  └── b
30
- └── App.jsx
29
+ └── App.tsx
31
30
  ```
32
31
 
33
- Modern.js 会根据 `./src/pages` 目录结构生成构建入口 `a` 和入口 `b`,结果如下:
32
+ Modern.js 会扫描 `./src/pages` 目录,自动生成构建入口 `a` 和入口 `b`,结果如下:
34
33
 
35
34
  ```js
36
- {
37
- a: './src/pages/a/App.jsx',
38
- b: './src/pages/b/App.jsx'
39
- }
35
+ const entry = {
36
+ a: './src/pages/a/App.tsx',
37
+ b: './src/pages/b/App.tsx',
38
+ };
40
39
  ```
@@ -1,19 +1,36 @@
1
1
  ---
2
- title: source.entries
3
2
  sidebar_label: entries
4
3
  ---
5
- # entries
6
4
 
7
- - **类型:** `Object = { [ entryName: string ]: string | Object }`
8
- - **默认值:** 根据当前项目目录结构动态结算出的默认入口对象。
5
+ # source.entries
9
6
 
10
- 对于大部分场景,Modern.js 根据目录结构自动生成的入口能满足大部分业务需求。具体可参考[入口](/guides/concept/entries)。
7
+ - **类型:**
11
8
 
12
- 如需自定义构建入口时,可以通过该选项指定。
9
+ ```ts
10
+ type Entries = Record<
11
+ string,
12
+ | string
13
+ | {
14
+ entry: string;
15
+ disableMount?: boolean;
16
+ }
17
+ >;
18
+ ```
19
+
20
+ - **默认值:** 根据当前项目的目录结构计算出的入口对象。
21
+
22
+ 用于配置自定义的页面入口。
23
+
24
+ :::tip 何时使用
25
+ 对于大部分场景,Modern.js 根据目录结构自动生成的入口已经可以满足需求,具体可参考[入口](/guides/concept/entries)。
26
+
27
+ 如果你需要自定义页面入口时,可以通过该选项进行设置。
28
+
29
+ :::
13
30
 
14
31
  ## String 类型
15
32
 
16
- 当值为 `string` 类型时,为入口的文件路径:
33
+ `entries` 对象的 value 为 `string` 类型时,表示入口模块的文件路径:
17
34
 
18
35
  ```ts title="modern.config.ts"
19
36
  import { defineConfig } from '@modern-js/app-tools';
@@ -22,13 +39,15 @@ export default defineConfig({
22
39
  source: {
23
40
  entries: {
24
41
  // 指定一个名称为 entry_customize 的新入口
25
- entry_customize: './src/home/test/index.js',
42
+ entry_customize: './src/home/test/index.ts',
26
43
  },
44
+ // 禁用默认入口扫描
45
+ disableDefaultEntries: true,
27
46
  },
28
47
  });
29
48
  ```
30
49
 
31
- 默认情况下,配置的入口等价于 `App.[jt]sx`,即指定的入口文件只需要导出应用的根组件。
50
+ 默认情况下,配置的入口等价于 `App.[jt]sx`,即指定的入口文件**只需要导出应用的根组件**。
32
51
 
33
52
  例如以下目录结构:
34
53
 
@@ -41,17 +60,19 @@ export default defineConfig({
41
60
  └── package.json
42
61
  ```
43
62
 
44
- 结合上面默认入口机制的内容,Modern.js 在分析上述目录结构时,不会得到任何默认入口。
63
+ 上述目录不符合 Modern.js 的目录结构约定,因此,Modern.js 在分析目录结构时,不会得到任何默认入口。
45
64
 
46
65
  在不想改变目录结构的情况下(如项目迁移),可以通过 `source.entries` 自定义入口:
47
66
 
48
- ```ts title="modern.config.js"
67
+ ```ts title="modern.config.ts"
49
68
  export default defineConfig({
50
69
  source: {
51
70
  entries: {
52
71
  home: './src/entry/home.tsx',
53
72
  chat: './src/entry/chat.tsx',
54
73
  },
74
+ // 禁用默认入口扫描
75
+ disableDefaultEntries: true,
55
76
  },
56
77
  });
57
78
  ```
@@ -61,7 +82,7 @@ export default defineConfig({
61
82
  当值为 `Object` 时,可配置如下属性:
62
83
 
63
84
  - `entry`:`string`,入口文件路径。
64
- - `disableMount`:`boolean = false`,关闭 Modern.js 生成入口代码的行为。
85
+ - `disableMount`:`boolean = false`,关闭 Modern.js 自动生成入口代码的行为。
65
86
 
66
87
  ```ts title="modern.config.ts"
67
88
  import { defineConfig } from '@modern-js/app-tools';
@@ -71,25 +92,65 @@ export default defineConfig({
71
92
  entries: {
72
93
  entry_customize: {
73
94
  // 入口文件路径
74
- entry: './src/home/test/App.jsx',
95
+ entry: './src/home/test/index.tsx',
96
+ },
97
+ },
98
+ // 禁用默认入口扫描
99
+ disableDefaultEntries: true,
100
+ },
101
+ });
102
+ ```
103
+
104
+ ### 禁用入口文件生成
105
+
106
+ 默认情况下,配置的入口等价于 `App.[jt]sx`,Modern.js 会自动生成一个入口文件来引用你配置的入口。
107
+
108
+ 如果你希望禁用 Modern.js 自动生成入口文件的逻辑,可以将 `disableMount` 属性设置为 `true`。
109
+
110
+ ```ts title="modern.config.ts"
111
+ export default defineConfig({
112
+ source: {
113
+ entries: {
114
+ entry_customize: {
115
+ entry: './src/home/test/index.tsx',
116
+ disableMount: true,
75
117
  },
118
+ },
119
+ // 禁用默认入口扫描
120
+ disableDefaultEntries: true,
121
+ },
122
+ });
123
+ ```
124
+
125
+ ### 约定式路由
126
+
127
+ 如果你需要为某个自定义入口启用约定式路由,可以将 `entry` 设置为目录路径:
128
+
129
+ ```ts title="modern.config.ts"
130
+ import { defineConfig } from '@modern-js/app-tools';
131
+
132
+ export default defineConfig({
133
+ source: {
134
+ entries: {
76
135
  // 启用约定式路由
77
136
  entry_spa: {
78
137
  // 约定式路由的入口路径必须设置为目录
79
138
  entry: './src/about',
80
139
  },
81
140
  },
141
+ // 禁用默认入口扫描
142
+ disableDefaultEntries: true,
82
143
  },
83
144
  });
84
145
  ```
85
146
 
86
- 默认情况下,配置的入口等价于 `App.[jt]sx`,如果希望该入口等价于构建模式下的入口,可以将属性 `disableMount` 设置为 `true`。
147
+ ## 入口合并规则
87
148
 
88
- ## 自定义入口和默认入口合并
149
+ 在设置 `source.entries` 后,如果没有设置 `disableDefaultEntries: true`,Modern.js 会将自定义入口与分析目录结构得到的入口合并。
89
150
 
90
- 在指定 `source.entries` 后,Modern.js 会将用户自定义的入口与分析目录结构得到的默认入口合并。合并规则为:
151
+ 合并规则为:
91
152
 
92
- 比较自定义入口设置的入口路径和默认入口路径,当入口路径一致时,自定义入口会覆盖默认入口。
153
+ - 比较自定义入口设置的入口路径和默认入口路径,当入口路径一致时,自定义入口会覆盖默认入口。
93
154
 
94
155
  例如以下目录结构:
95
156
 
@@ -97,13 +158,13 @@ export default defineConfig({
97
158
  .
98
159
  ├── src
99
160
  │ ├── chat
100
- │ │ └── App.jsx
161
+ │ │ └── App.tsx
101
162
  │ └── home
102
- │ └── index.js
163
+ │ └── index.ts
103
164
  └── package.json
104
165
  ```
105
166
 
106
- Modern.js 分析 `src/` 目录,得到默认入口 `chat` 和 `home`。当用户在 `modern.config.js` 文件中配置如下时:
167
+ Modern.js 会分析 `src/` 目录,得到默认入口 `chat` 和 `home`。当用户在 `modern.config.ts` 文件中配置如下时:
107
168
 
108
169
  ```ts title="modern.config.ts"
109
170
  import { defineConfig } from '@modern-js/app-tools';
@@ -111,7 +172,7 @@ import { defineConfig } from '@modern-js/app-tools';
111
172
  export default defineConfig({
112
173
  source: {
113
174
  entries: {
114
- index: './src/home/index.js',
175
+ index: './src/home/index.ts',
115
176
  },
116
177
  },
117
178
  };
@@ -119,5 +180,5 @@ export default defineConfig({
119
180
 
120
181
  可以看到自定义入口 `index` 的路径和默认入口 `home` 的路径一致,在合并的过程中,`index` 会覆盖掉 `home`,最终入口如下:
121
182
 
122
- - `chat -> ./src/chat/App.jsx`
123
- - `index -> ./src/home/index.js`
183
+ - `chat -> ./src/chat/App.tsx`
184
+ - `index -> ./src/home/index.ts`
@@ -1,21 +1,25 @@
1
1
  ---
2
- title: tools.esbuild
3
2
  sidebar_label: esbuild
4
3
  ---
5
- # esbuild
4
+
5
+ # tools.esbuild
6
6
 
7
7
  - **类型:** `Object`
8
8
  - **默认值:** `undefined`
9
9
 
10
10
  ## 介绍
11
11
 
12
- :::tip esbuild 介绍
13
12
  [esbuild](https://esbuild.github.io/) 是一款基于 Golang 开发的前端构建工具,具有打包、编译和压缩 JavaScript 代码的功能,相比传统的打包编译工具,esbuild 在性能上有显著提升。在代码压缩方面,相比 webpack 内置的 terser 压缩器,esbuild 在性能上有数十倍的提升。
14
13
 
15
- :::
16
-
17
14
  Modern.js 提供了 esbuild 插件,让你能使用 esbuild 代替 babel-loader、ts-loader 和 terser 等库进行代码编译和压缩。在大型工程中开启后,**可以大幅度减少代码编译和压缩所需的时间,同时有效避免 OOM (heap out of memory) 问题**。
18
15
 
16
+ :::tip 推荐使用 SWC
17
+ 相较于 esbuild,我们更推荐使用 SWC 来编译和压缩代码,因为 SWC 支持更多的语法特性、拥有更好的代码压缩率,并且产物具备更好的兼容性。
18
+
19
+ 因此,我们建议你使用 SWC 而不是 esbuild,用法请参考 [tools.swc](/configure/app/tools/swc)。
20
+
21
+ :::
22
+
19
23
  ## 配置项
20
24
 
21
25
  你可以通过 `tools.esbuild` 配置项来设置 esbuild 编译行为。