@modern-js/main-doc 2.10.0 → 2.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/docs/en/apis/app/hooks/src/server.mdx +1 -28
  3. package/docs/en/configure/app/source/config-dir.mdx +2 -2
  4. package/docs/en/configure/app/source/design-system.mdx +2 -2
  5. package/docs/en/configure/app/source/disable-default-entries.mdx +12 -12
  6. package/docs/en/configure/app/source/disable-entry-dirs.mdx +3 -4
  7. package/docs/en/configure/app/source/enable-async-entry.mdx +2 -2
  8. package/docs/en/configure/app/source/entries-dir.mdx +10 -10
  9. package/docs/en/configure/app/source/entries.mdx +83 -26
  10. package/docs/en/configure/app/source/plugin-import.mdx +13 -0
  11. package/docs/en/configure/app/source/transform-import.mdx +13 -0
  12. package/docs/en/configure/app/tools/esbuild.mdx +9 -4
  13. package/docs/en/configure/app/tools/jest.mdx +2 -3
  14. package/docs/en/configure/app/tools/swc.mdx +6 -2
  15. package/docs/en/guides/advanced-features/ssr.mdx +4 -3
  16. package/docs/en/guides/advanced-features/web-server.mdx +1 -1
  17. package/docs/en/guides/concept/entries.mdx +130 -39
  18. package/docs/en/guides/topic-detail/framework-plugin/hook-list.mdx +7 -3
  19. package/docs/en/guides/topic-detail/model/typescript-best-practice.mdx +4 -4
  20. package/docs/en/tutorials/first-app/c06-model.mdx +15 -12
  21. package/docs/en/tutorials/first-app/c07-container.mdx +22 -19
  22. package/docs/zh/apis/app/hooks/src/server.mdx +1 -28
  23. package/docs/zh/configure/app/source/config-dir.mdx +2 -2
  24. package/docs/zh/configure/app/source/design-system.mdx +3 -3
  25. package/docs/zh/configure/app/source/disable-default-entries.mdx +11 -10
  26. package/docs/zh/configure/app/source/disable-entry-dirs.mdx +2 -3
  27. package/docs/zh/configure/app/source/enable-async-entry.mdx +3 -3
  28. package/docs/zh/configure/app/source/entries-dir.mdx +10 -11
  29. package/docs/zh/configure/app/source/entries.mdx +84 -23
  30. package/docs/zh/configure/app/source/plugin-import.mdx +13 -0
  31. package/docs/zh/configure/app/source/transform-import.mdx +13 -0
  32. package/docs/zh/configure/app/tools/esbuild.mdx +9 -5
  33. package/docs/zh/configure/app/tools/jest.mdx +2 -3
  34. package/docs/zh/configure/app/tools/swc.mdx +6 -2
  35. package/docs/zh/guides/advanced-features/ssr.mdx +6 -3
  36. package/docs/zh/guides/advanced-features/web-server.mdx +10 -5
  37. package/docs/zh/guides/concept/entries.mdx +36 -35
  38. package/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx +6 -2
  39. package/docs/zh/tutorials/first-app/c06-model.mdx +14 -12
  40. package/docs/zh/tutorials/first-app/c07-container.mdx +22 -19
  41. package/modern.config.ts +1 -1
  42. package/package.json +5 -5
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @modern-js/main-doc
2
2
 
3
+ ## 2.11.0
4
+
5
+ ### Patch Changes
6
+
7
+ - a8c08c3: feat: 添加 `source.transformImoprt`
8
+
9
+ feat: add `source.transformImoprt`
10
+
11
+ - 304c950: fix: translate some chinese to english in en docs
12
+ fix: 将部分英文文档中的中文翻译为英文
13
+ - Updated dependencies [a8c08c3]
14
+ - Updated dependencies [b71cef1]
15
+ - @modern-js/builder-doc@2.11.0
16
+
3
17
  ## 2.10.0
4
18
 
5
19
  ### Patch Changes
@@ -4,31 +4,4 @@ sidebar_position: 8
4
4
  ---
5
5
  # *.[server|node].[tj]sx
6
6
 
7
- Used in application projects to place server side code, it generally has the following two functions:
8
-
9
- 1. When `*.tsx` and `*. [server|node].tsx` coexist, rendering on the server side will give preference to the `*. [server|node].tsx` file instead of the `*.tsx` file.
10
-
11
- 2. When using [data loader](/guides/basic-features/data-fetch), the server-side dependencies need to be re-exported from this file
12
-
13
- ```tsx
14
- // routes/user/avatar.tsx
15
- import { useLoaderData } from '@modern-js/runtime/router';
16
- import { readFile } from './utils.server';
17
-
18
- type ProfileData = {
19
- /* some type declarations */
20
- };
21
-
22
- export const loader = async (): ProfileData => {
23
- const profile = await readFile('profile.json');
24
- return profile;
25
- };
26
-
27
- export default function UserPage() {
28
- const profileData = useLoaderData() as ProfileData;
29
- return <div>{profileData}</div>;
30
- }
31
-
32
- // routes/user/utils.server.ts
33
- export * from 'fs-extra';
34
- ```
7
+ Used in application projects to place server side code, When `*.tsx` and `*. [server|node].tsx` coexist, rendering on the server side will give preference to the `*. [server|node].tsx` file instead of the `*.tsx` file.
@@ -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
  - **Type:** `string`
8
8
  - **Default:** `./config`
@@ -1,8 +1,8 @@
1
1
  ---
2
- title: source.designSystem
3
2
  sidebar_label: designSystem
4
3
  ---
5
- # designSystem
4
+
5
+ # source.designSystem
6
6
 
7
7
  - **Type:** `Object`
8
8
  - **Default:** See configuration details below.
@@ -1,26 +1,19 @@
1
1
  ---
2
- title: source.disableDefaultEntries
3
2
  sidebar_label: disableDefaultEntries
4
3
  ---
5
- # disableDefaultEntries
4
+
5
+ # source.disableDefaultEntries
6
6
 
7
7
  - **Type:** `boolean`
8
8
  - **Default:** `false`
9
9
 
10
- Turn off the function of automatically identifying the application build entry according to the project directory structure. By default, the Modern.js will get the corresponding build entry according to the project directory structure.
10
+ Used to disable the function of automatically identifying the page entry according to the project directory structure.
11
11
 
12
12
  :::info
13
- By default, Modern.js will get the corresponding entry information according to the project directory structure. For details, please refer to [Entry](/guides/concept/entries).
14
- After configuring the shutdown mechanism, you need to configure the custom entry with [`source.entries`](/configure/app/source/entries).
15
-
16
- :::
17
-
18
- :::warning Warning
19
- Organizing your code according to the catalog specification provided by the Modern.js is recommended to make better use of the framework's capabilities and avoid some redundant configuration.
20
-
13
+ By default, Modern.js will get the page entries according to the project directory structure. For details, please refer to [Entry](/guides/concept/entries).
21
14
  :::
22
15
 
23
- Set the following to turn off the default behavior:
16
+ Set the following to disable the default behavior:
24
17
 
25
18
  ```ts title="modern.config.ts"
26
19
  export default defineConfig({
@@ -29,3 +22,10 @@ export default defineConfig({
29
22
  },
30
23
  });
31
24
  ```
25
+
26
+ After turning off the default behavior, you need to use [`source.entries`](/configure/app/source/entries) to configure custom entries.
27
+
28
+ :::warning
29
+ We recommend using the directory convention provided by Modern.js to organize the code, so as to better use the functions of the framework and avoid some redundant configurations.
30
+
31
+ :::
@@ -1,14 +1,13 @@
1
1
  ---
2
- title: source.disableEntryDirs
3
-
4
2
  sidebar_label: disableEntryDirs
5
3
  ---
6
- # disableEntryDirs
4
+
5
+ # source.disableEntryDirs
7
6
 
8
7
  - **Type:** `string[]`
9
8
  - **Default:** `[]`
10
9
 
11
- By default, application entries are identified based on the'src 'directory, you can disable some directories from being identified as application entries with this option.
10
+ By default, application entries are identified based on the `src` directory, you can disable some directories from being identified as application entries with this option.
12
11
 
13
12
  For example, when the configuration and directory structure is as follows:
14
13
 
@@ -1,8 +1,8 @@
1
1
  ---
2
- title: source.enableAsyncEntry
3
2
  sidebar_label: enableAsyncEntry
4
3
  ---
5
- # enableAsyncEntry
4
+
5
+ # source.enableAsyncEntry
6
6
 
7
7
  - **Type:** `boolean`
8
8
  - **Default:** `false`
@@ -1,13 +1,13 @@
1
1
  ---
2
- title: source.entriesDir
3
2
  sidebar_label: entriesDir
4
3
  ---
5
- # entriesDir
4
+
5
+ # source.entriesDir
6
6
 
7
7
  - **Type:** `string`
8
8
  - **Default:** `./src`
9
9
 
10
- By default, the application entry will be identified according to the `src` directory. You can customize the identification directory of the application entry through this option.
10
+ Modern.js will scan the `src` directory by default to identify the page entries, you can customize the identification directory of the page entries through this option.
11
11
 
12
12
  For example, when the configuration and directory structure are as follows:
13
13
 
@@ -24,16 +24,16 @@ export default defineConfig({
24
24
  └── src
25
25
  └── pages
26
26
  ├── a
27
- │ └── App.jsx
27
+ │ └── App.tsx
28
28
  └── b
29
- └── App.jsx
29
+ └── App.tsx
30
30
  ```
31
31
 
32
- Modern.js will generate the build entry `a` and entry `b` according to the `./src/pages` directory structure, the result is as follows:
32
+ Modern.js will generate the build entry `a` and `b` according to the `./src/pages` directory structure, the result is as follows:
33
33
 
34
34
  ```js
35
- {
36
- a: './src/pages/a/App.jsx',
37
- b: './src/pages/b/App.jsx'
38
- }
35
+ const entry = {
36
+ a: './src/pages/a/App.tsx',
37
+ b: './src/pages/b/App.tsx',
38
+ };
39
39
  ```
@@ -1,19 +1,36 @@
1
1
  ---
2
- title: source.entries
3
2
  sidebar_label: entries
4
3
  ---
5
- # entries
6
4
 
7
- - **Type:** `Object = { [ entryName: string ]: string | Object }`
8
- - **Default:** Default entry object dynamically settled according to the current project directory structure.
5
+ # source.entries
9
6
 
10
- For most scenarios, Modern.js automatically generated entries according to the directory structure can meet most business requirements. For details, please refer to [Entries](/guides/concept/entries).
7
+ - **Type:**
11
8
 
12
- If you need to customize the build entry, you can specify it with this option.
9
+ ```ts
10
+ type Entries = Record<
11
+ string,
12
+ | string
13
+ | {
14
+ entry: string;
15
+ disableMount?: boolean;
16
+ }
17
+ >;
18
+ ```
19
+
20
+ - **Default:** An entry object calculated from the directory structure of the current project.
21
+
22
+ Used to configure custom page entries.
23
+
24
+ :::tip When to use
25
+ For most scenarios, the entries automatically generated by Modern.js according to the directory structure can already meet the requirements. For details, please refer to [entries](/guides/concept/entries).
26
+
27
+ If you need to customize the page entry, you can set it through this option.
28
+
29
+ :::
13
30
 
14
31
  ## String
15
32
 
16
- When the value is of type `string`, the file path of the entry:
33
+ When the value of `entries` object is `string` type, it means the file path of the entry module:
17
34
 
18
35
  ```ts title="modern.config.ts"
19
36
  import { defineConfig } from '@modern-js/app-tools';
@@ -22,13 +39,14 @@ export default defineConfig({
22
39
  source: {
23
40
  entries: {
24
41
  // Specify a new entry named entry_customize
25
- entry_customize: './src/home/test/index.js',
42
+ entry_customize: './src/home/test/index.ts',
26
43
  },
44
+ disableDefaultEntries: true,
27
45
  },
28
46
  });
29
47
  ```
30
48
 
31
- By default, the configured entry is equivalent to `App.[jt]sx`, that is, the specified entry file only needs to export the root component of the application.
49
+ By default, the configured entry is equivalent to `App.[jt]sx`, that is, the specified entry file **only needs to export the root component of the application**.
32
50
 
33
51
  For example the following directory structure:
34
52
 
@@ -41,17 +59,18 @@ For example the following directory structure:
41
59
  └── package.json
42
60
  ```
43
61
 
44
- With the content of the above default entry mechanism, Modern.js when analyzing the above directory, will not get any default entry.
62
+ The above directory does not conform to the directory structure convention of Modern.js, therefore, Modern.js will not get any default entry when analyzing the directory structure.
45
63
 
46
64
  In cases where you do not want to change the directory structure (such as project migration), you can customize the entry through `source.entries`:
47
65
 
48
- ```ts title="modern.config.js"
66
+ ```ts title="modern.config.ts"
49
67
  export default defineConfig({
50
68
  source: {
51
69
  entries: {
52
70
  home: './src/entry/home.tsx',
53
71
  chat: './src/entry/chat.tsx',
54
72
  },
73
+ disableDefaultEntries: true,
55
74
  },
56
75
  });
57
76
  ```
@@ -60,8 +79,8 @@ export default defineConfig({
60
79
 
61
80
  When the value is `Object`, the following properties can be configured:
62
81
 
63
- - `entry`: `string`,entry file path.
64
- - `disableMount`: `boolean = false`,turn off Modern.js generate entry code.
82
+ - `entry`: `string`, entry file path.
83
+ - `disableMount`: `boolean = false`, turn off the entry-scanning behavior of Modern.js.
65
84
 
66
85
  ```ts title="modern.config.ts"
67
86
  import { defineConfig } from '@modern-js/app-tools';
@@ -71,25 +90,63 @@ export default defineConfig({
71
90
  entries: {
72
91
  entry_customize: {
73
92
  // entry file path
74
- entry: './src/home/test/App.jsx',
93
+ entry: './src/home/test/App.tsx',
94
+ },
95
+ },
96
+ disableDefaultEntries: true,
97
+ },
98
+ });
99
+ ```
100
+
101
+ ### Disable entry file generation
102
+
103
+ By default, the configured entry is equivalent to `App.[jt]sx`, and Modern.js will automatically generate an entry file to reference your configured entry.
104
+
105
+ If you want to disable the logic of Modern.js automatically generating the entry file, you can set the `disableMount` property to `true`.
106
+
107
+ ```ts title="modern.config.ts"
108
+ export default defineConfig({
109
+ source: {
110
+ entries: {
111
+ entry_customize: {
112
+ entry: './src/home/test/index.tsx',
113
+ disableMount: true,
75
114
  },
76
- // Use Conventional Routing
115
+ },
116
+ // Disable default ingress scanning
117
+ disableDefaultEntries: true,
118
+ },
119
+ });
120
+ ```
121
+
122
+ ### Conventional Routing
123
+
124
+ If you need to enable conventional routing for a custom entry, you can set `entry` to a directory path:
125
+
126
+ ```ts title="modern.config.ts"
127
+ import { defineConfig } from '@modern-js/app-tools';
128
+
129
+ export default defineConfig({
130
+ source: {
131
+ entries: {
132
+ // enable conventional routing
77
133
  entry_spa: {
78
- // The entry path of a conventional route must be set to a directory
134
+ // The entry path of conventional routing must be set to a directory
79
135
  entry: './src/about',
80
136
  },
81
137
  },
138
+ disableDefaultEntries: true,
82
139
  },
83
140
  });
84
141
  ```
85
142
 
86
- By default, the configured entry is equivalent to `App.[jt]sx`. If you want the entry to be equivalent to the entry in build mode, you can set the value to'Object' and the property `disableMount` to `true`.
143
+ ## Entry Merge Rules
87
144
 
88
- ## Combine Entry
145
+ After setting `source.entries`, if `disableDefaultEntries: true` is not set, Modern.js will merge the custom entry with the entry obtained by analyzing the directory structure.
89
146
 
90
- When `source.entries` is specified, the Modern.js merges the user-defined entry with the default entry obtained by analyzing the directory structure. The merging rule is:
147
+ The merge rule is:
91
148
 
92
- Compare the entry path set by the custom entry with the default entry path. When the entry paths are the same, the custom entry will override the default entry.
149
+ - Compare the entry path set by the custom entry with the default entry path. When the entry path is consistent, the custom entry will override the default entry.
93
150
 
94
151
  For example the following directory structure:
95
152
 
@@ -97,13 +154,13 @@ For example the following directory structure:
97
154
  .
98
155
  ├── src
99
156
  │ ├── chat
100
- │ │ └── App.jsx
157
+ │ │ └── App.tsx
101
158
  │ └── home
102
- │ └── index.js
159
+ │ └── index.ts
103
160
  └── package.json
104
161
  ```
105
162
 
106
- Modern.js analyze the `src/` directory to get the default entries `chat` and `home`. When the user configures the following in the `modern.config.js` file:
163
+ Modern.js will analyze the `src/` directory to get the default entries `chat` and `home`. When the user configures the following in the `modern.config.ts` file:
107
164
 
108
165
  ```ts title="modern.config.ts"
109
166
  import { defineConfig } from '@modern-js/app-tools';
@@ -111,7 +168,7 @@ import { defineConfig } from '@modern-js/app-tools';
111
168
  export default defineConfig({
112
169
  source: {
113
170
  entries: {
114
- index: './src/home/index.js',
171
+ index: './src/home/index.ts',
115
172
  },
116
173
  },
117
174
  };
@@ -119,5 +176,5 @@ export default defineConfig({
119
176
 
120
177
  You can see that the path of the custom entry `index` is the same as the path of the default entry `home`. During the merging process, `index` will override `home`, and the final entry is as follows:
121
178
 
122
- - `chat -> ./src/chat/App.jsx`
123
- - `index -> ./src/home/index.js`
179
+ - `chat -> ./src/chat/App.tsx`
180
+ - `index -> ./src/home/index.ts`
@@ -0,0 +1,13 @@
1
+ ---
2
+ sidebar_label: pluginImport
3
+ ---
4
+
5
+ # source.transformImport
6
+
7
+ :::tip
8
+ This config is provided by Modern.js Builder, more detail can see [source.transformImport](https://modernjs.dev/builder/en/api/config-source.html#sourcepluginimport).
9
+ :::
10
+
11
+ import Main from '@modern-js/builder-doc/docs/en/config/source/transformImport.md';
12
+
13
+ <Main />
@@ -0,0 +1,13 @@
1
+ ---
2
+ sidebar_label: transformImport
3
+ ---
4
+
5
+ # source.transformImport
6
+
7
+ :::tip
8
+ This config is provided by Modern.js Builder, more detail can see [source.transformImport](https://modernjs.dev/builder/en/api/config-source.html#sourcetransformimport).
9
+ :::
10
+
11
+ import Main from '@modern-js/builder-doc/docs/en/config/source/transformImport.md';
12
+
13
+ <Main />
@@ -1,20 +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
  - **Type:** `Object`
8
8
  - **Default:** `undefined`
9
9
 
10
10
  ## Introduction
11
11
 
12
- :::tip About esbuild
13
12
  [esbuild](https://esbuild.github.io/) is a front-end build tool based on Golang. It has the functions of bundling, compiling and minimizing JavaScript code. Compared with traditional tools, the performance is significantly improved. When minimizing code, compared to webpack's built-in terser minimizer, esbuild has dozens of times better performance.
14
13
 
14
+ Modern.js provides esbuild plugin that allow you to use esbuild instead of babel-loader, ts-loader and terser for transformation and minification process.
15
+
16
+ :::tip Recommend using SWC
17
+ We recommend using SWC to transform and minify code rather than esbuild, because SWC supports more syntaxes, provides better code compression, and the compiled code has better compatibility.
18
+
19
+ Therefore, we recommend that you use SWC instead of esbuild, please refer to [tools.swc](/configure/app/tools/swc) for usage.
20
+
15
21
  :::
16
22
 
17
- Modern.js provides esbuild plugin that allow you to use esbuild instead of babel-loader, ts-loader and terser for transformation and minification process.
18
23
 
19
24
  ## Configuration
20
25
 
@@ -1,9 +1,8 @@
1
1
  ---
2
- title: tools.jest
3
-
4
2
  sidebar_label: jest
5
3
  ---
6
- # jest
4
+
5
+ # tools.jest
7
6
 
8
7
  - **Type:** `Object | Function`
9
8
  - **Default:** `{}`
@@ -1,8 +1,8 @@
1
1
  ---
2
- title: tools.swc
3
2
  sidebar_label: swc
4
3
  ---
5
- # swc
4
+
5
+ # tools.swc
6
6
 
7
7
  - **Type:** `Object`
8
8
  - **Default:** `undefined`
@@ -13,6 +13,10 @@ sidebar_label: swc
13
13
 
14
14
  Modern.js Builder has a out-of-box plugin for SWC, power your Web application with Polyfill and minification, we also port some common used Babel plugins to SWC.
15
15
 
16
+ :::tip
17
+ When using Rspack as the bundler, SWC will be used for transpiling and compression by default, so you don't need to install or configure the SWC plugin.
18
+ :::
19
+
16
20
  ## Install
17
21
 
18
22
  The `@modern-js/plugin-swc` plugin needs to be installed before use.
@@ -22,8 +22,8 @@ export default defineConfig({
22
22
 
23
23
  Modern.js provides Data Loader, which is convenient for developers to fetch data under SSR and CSR. Each routing module, such as `layout.tsx` and `page.tsx`, can define its own Data Loader:
24
24
 
25
- ```ts title="src/routes/page.tsx"
26
- export const loader = () => {
25
+ ```ts title="src/routes/page.loader.ts"
26
+ export default () => {
27
27
  return {
28
28
  message: 'Hello World',
29
29
  };
@@ -49,6 +49,7 @@ However, developers still need to pay attention to the fallback of data, such as
49
49
  1. When you request the page on client-side page transitions, Modern.js sends an API request to the server, which runs Data Loader function.
50
50
 
51
51
  2. When using Data Loader, data fetching happens before rendering, Modern.js still supports fetching data when the component is rendered. See [Data Fetch](/guides/basic-features/data-fetch).
52
+
52
53
  :::
53
54
 
54
55
  ## Keep Rendering Consistent
@@ -225,7 +226,7 @@ The Node API is introduced in the component file, usually because of the use of
225
226
 
226
227
  ```ts
227
228
  import fse from 'fs-extra';
228
- export const loader = () => {
229
+ export default () => {
229
230
  const file = fse.readFileSync('./myfile');
230
231
  return {
231
232
  ...
@@ -80,7 +80,7 @@ Modern.js provides a set of APIs by default for projects to use:
80
80
  ```ts
81
81
  import { Middlewre } from '@modern-js/runtime/server';
82
82
 
83
- export const middleware = (context, next) => {
83
+ export const middleware: Middlewre = (context, next) => {
84
84
  const { source: { req, res } } = context;
85
85
  console.log(req.url);
86
86
  next();