@modern-js/main-doc 0.0.0-next-20230104054903 → 0.0.0-next-20230104140639
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +2 -3
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/_category_.json +5 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c01-start.md +99 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c02-component.md +56 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c03-css.md +324 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c04-routes.md +169 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c05-loader.md +82 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c06-model.md +260 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c07-container.md +283 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c08-entries.md +137 -0
- package/package.json +3 -3
@@ -0,0 +1,137 @@
|
|
1
|
+
---
|
2
|
+
title: 添加应用入口
|
3
|
+
---
|
4
|
+
|
5
|
+
上一个章节中,我们基本完成了联系人列表应用的开发,介绍了 Modern.js 中部分功能的用法,以及推荐的最佳实践。
|
6
|
+
|
7
|
+
这一章节中,我们将介绍如何为应用添加新的入口。
|
8
|
+
|
9
|
+
## 新建入口
|
10
|
+
|
11
|
+
一个完整的项目可能需要多个入口,Modern.js 支持自动创建新入口,前面的章节中提到过,`pnpm run new` 可以启用可选功能。
|
12
|
+
|
13
|
+
我们也可以通过它来创建新的工程元素,在项目根目录下执行 `pnpm run new`:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
? 请选择你想要的操作 创建工程元素
|
17
|
+
? 创建工程元素 新建「应用入口」
|
18
|
+
? 请填写入口名称 (entry) landing-page
|
19
|
+
```
|
20
|
+
|
21
|
+
创建完成,项目会变成这样:
|
22
|
+
|
23
|
+
```md
|
24
|
+
.
|
25
|
+
├── README.md
|
26
|
+
├── modern.config.ts
|
27
|
+
├── node_modules
|
28
|
+
├── package.json
|
29
|
+
├── pnpm-lock.yaml
|
30
|
+
├── src
|
31
|
+
│ ├── modern-app-env.d.ts
|
32
|
+
│ ├── landing-page
|
33
|
+
│ │ └── routes
|
34
|
+
│ │ ├── index.css
|
35
|
+
│ │ ├── layout.tsx
|
36
|
+
│ │ └── page.tsx
|
37
|
+
│ └── myapp
|
38
|
+
│ ├── components
|
39
|
+
│ │ ├── Avatar
|
40
|
+
│ │ │ └── index.tsx
|
41
|
+
│ │ └── Item
|
42
|
+
│ │ └── index.tsx
|
43
|
+
│ ├── containers
|
44
|
+
│ │ └── Contacts.tsx
|
45
|
+
│ ├── models
|
46
|
+
│ │ └── contacts.ts
|
47
|
+
│ ├── routes
|
48
|
+
│ │ ├── archives
|
49
|
+
│ │ │ └── page.tsx
|
50
|
+
│ │ ├── layout.tsx
|
51
|
+
│ │ └── page.tsx
|
52
|
+
│ └── styles
|
53
|
+
│ └── utils.css
|
54
|
+
└── tsconfig.json
|
55
|
+
|
56
|
+
```
|
57
|
+
|
58
|
+
可以看到联系人列表应用的文件,都被自动重构到 `src/myapp/` 里。
|
59
|
+
|
60
|
+
同时新建了一个 `src/landing-page/`,里面同样有 `routes/*`(`pnpm run new` 命令只做了这些事,所以你也可以很容易的手动创建新入口或修改入口)
|
61
|
+
|
62
|
+
执行 `pnpm run dev`,显示:
|
63
|
+
|
64
|
+

|
65
|
+
|
66
|
+
访问 `http://localhost:8080/`,可以像之前一样看到应用程序。
|
67
|
+
|
68
|
+
访问 `http://localhost:8080/landing-page`,可以看到刚创建的新入口 `landing-page` 的页面(Modern.js 自动生成的默认页面)。
|
69
|
+
|
70
|
+
Modern.js 框架的设计原则之一是【[约定优于配置(Convention over Configuration)](https://en.wikipedia.org/wiki/Convention_over_configuration)】,多数情况下可以按约定直接写代码,不需要做任何配置,这里 `src/` 中的目录结构就是一种约定:
|
71
|
+
|
72
|
+
`src/myapp/` 和 `src/landing-page/` 被自动识别为两个应用入口:myapp 和 landing-page。
|
73
|
+
|
74
|
+
其中 `src/myapp/` 的目录名跟项目名(`package.json` 里的 `name`)一致,会被认为是项目**主入口**,项目 URL 的根路径(开发环境里默认是 `http://localhost:8080/`)会自动指向主入口。
|
75
|
+
|
76
|
+
其他入口的 URL,是在根路径后追加入口名,比如 `http://localhost:8080/landing-page`。
|
77
|
+
|
78
|
+
接下来,我们把 `src/myapp/` 重命名为 `src/contacts/`:
|
79
|
+
|
80
|
+
```bash
|
81
|
+
mv src/myapp src/contacts
|
82
|
+
```
|
83
|
+
|
84
|
+
再次执行 `pnpm run dev`,结果变成:
|
85
|
+
|
86
|
+

|
87
|
+
|
88
|
+
现在不再有主入口,联系人列表现在是一个普通入口,需要用 `http://localhost:8080/contacts` 访问。
|
89
|
+
|
90
|
+
|
91
|
+
## 按入口修改配置
|
92
|
+
|
93
|
+
我们可以在 Modern.js 配置文件里,自己写代码来控制项目的配置。
|
94
|
+
|
95
|
+
现在,修改 `modern.config.ts` 里面添加内容:
|
96
|
+
|
97
|
+
```typescript
|
98
|
+
import AppToolsPlugin, { defineConfig } from '@modern-js/app-tools';
|
99
|
+
import TailwindCSSPlugin from '@modern-js/plugin-tailwindcss';
|
100
|
+
|
101
|
+
// https://modernjs.dev/docs/apis/app/config
|
102
|
+
export default defineConfig({
|
103
|
+
runtime: {
|
104
|
+
router: true,
|
105
|
+
state: true,
|
106
|
+
},
|
107
|
+
server: {
|
108
|
+
ssr: true,
|
109
|
+
ssrByEntries: {
|
110
|
+
'landing-page': false,
|
111
|
+
},
|
112
|
+
},
|
113
|
+
plugins: [AppToolsPlugin(), TailwindCSSPlugin()],
|
114
|
+
});
|
115
|
+
```
|
116
|
+
|
117
|
+
执行 `pnpm run dev`,再用浏览器打开 `view-source:http://localhost:8080/landing-page`,可以看到 `landing-page` 网页内容是通过 js 动态加载的,且此页面的 SSR 功能被关闭。
|
118
|
+
|
119
|
+
如果注释掉 `ssrByEntries` 和它的值,landing-page 的 SSR 功能就恢复开启了。
|
120
|
+
|
121
|
+
还有一些时候,需要一些更复杂的逻辑来做设置,比如需要 JS 变量、表达式、导入模块等,例如在只在开发环境里开启 SSR:
|
122
|
+
|
123
|
+
```js
|
124
|
+
export default defineConfig({
|
125
|
+
server: {
|
126
|
+
ssrByEntries: {
|
127
|
+
'landing-page': process.env.NODE_ENV !== 'production',
|
128
|
+
},
|
129
|
+
},
|
130
|
+
};
|
131
|
+
```
|
132
|
+
|
133
|
+
到底为止,我们的联系人列表应用的雏形就基本完成了 👏👏👏。
|
134
|
+
|
135
|
+
## 下一步
|
136
|
+
|
137
|
+
接下来你可以通过了解[指南](/docs/guides/get-started/quick-start)、[配置](/docs/configure/app/usage) 等更多教程,进一步完善你的应用。
|
package/package.json
CHANGED
@@ -11,20 +11,20 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "0.0.0-next-
|
14
|
+
"version": "0.0.0-next-20230104140639",
|
15
15
|
"publishConfig": {
|
16
16
|
"registry": "https://registry.npmjs.org/",
|
17
17
|
"access": "public"
|
18
18
|
},
|
19
19
|
"peerDependencies": {
|
20
|
-
"@modern-js/builder-doc": "0.0.0-next-
|
20
|
+
"@modern-js/builder-doc": "0.0.0-next-20230104140639"
|
21
21
|
},
|
22
22
|
"devDependencies": {
|
23
23
|
"ts-node": "^10",
|
24
24
|
"fs-extra": "^10",
|
25
25
|
"@types/node": "^16",
|
26
26
|
"@types/fs-extra": "^9",
|
27
|
-
"@modern-js/builder-doc": "0.0.0-next-
|
27
|
+
"@modern-js/builder-doc": "0.0.0-next-20230104140639"
|
28
28
|
},
|
29
29
|
"scripts": {
|
30
30
|
"build": "npx ts-node ./scripts/sync.ts"
|