@modern-js/main-doc 0.0.0-next-20221209071922 → 0.0.0-next-20221209080421
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
CHANGED
package/CHANGELOG.md
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
# @modern-js/main-doc
|
2
2
|
|
3
|
-
## 0.0.0-next-
|
3
|
+
## 0.0.0-next-20221209080421
|
4
4
|
|
5
5
|
### Patch Changes
|
6
6
|
|
7
|
-
-
|
7
|
+
- 5927355ee1: feat: develop documentation directly with main-doc
|
8
8
|
feat: 直接使用 main-doc 包开发文档
|
9
|
-
-
|
9
|
+
- 1ef69374be: feat: support update main-doc when package build or website build
|
10
10
|
feat: 支持在包发布和官网发布时,更新 main-doc 包
|
11
|
-
- Updated dependencies [
|
12
|
-
- Updated dependencies [
|
13
|
-
- Updated dependencies [
|
14
|
-
- Updated dependencies [
|
15
|
-
- Updated dependencies [
|
16
|
-
- Updated dependencies [
|
17
|
-
- Updated dependencies [
|
18
|
-
- Updated dependencies [
|
19
|
-
- Updated dependencies [
|
20
|
-
- Updated dependencies [
|
21
|
-
- Updated dependencies [
|
22
|
-
- Updated dependencies [
|
23
|
-
- Updated dependencies [
|
24
|
-
- @modern-js/builder-doc@0.0.0-next-
|
11
|
+
- Updated dependencies [2bc090c089]
|
12
|
+
- Updated dependencies [f96a725211]
|
13
|
+
- Updated dependencies [57077b2c64]
|
14
|
+
- Updated dependencies [2ff6167be0]
|
15
|
+
- Updated dependencies [5402fdb0ca]
|
16
|
+
- Updated dependencies [10d08a480d]
|
17
|
+
- Updated dependencies [5d67c26cdb]
|
18
|
+
- Updated dependencies [af4422d67f]
|
19
|
+
- Updated dependencies [dda38c9c3e]
|
20
|
+
- Updated dependencies [812913ccdd]
|
21
|
+
- Updated dependencies [3fae2d03b3]
|
22
|
+
- Updated dependencies [df41d71ade]
|
23
|
+
- Updated dependencies [14b712da84]
|
24
|
+
- @modern-js/builder-doc@0.0.0-next-20221209080421
|
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-20221209080421",
|
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-20221209080421"
|
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-20221209080421"
|
28
28
|
},
|
29
29
|
"scripts": {
|
30
30
|
"build": "npx ts-node ./scripts/sync.ts"
|
@@ -0,0 +1,184 @@
|
|
1
|
+
---
|
2
|
+
title: Document 模板
|
3
|
+
sidebar_position: 9
|
4
|
+
---
|
5
|
+
|
6
|
+
Docuemnt 模板会生成 html 文件,替代原来的 `html 模板`。
|
7
|
+
这种方式的好处在于:使用了 `jsx` 语法,让写模板跟写组件一样丝滑。
|
8
|
+
|
9
|
+
为了同时能够满足后端同学也能使用 `ejs` 语法编写模板,Modern.js 使用 `html 模板` 作为兼容。当项目中,没有编写 `Document.j|tsx` 文件时,将自动回退至 html 模板。
|
10
|
+
|
11
|
+
## 使用说明
|
12
|
+
### 引入
|
13
|
+
```tsx
|
14
|
+
import {
|
15
|
+
Html,
|
16
|
+
Root,
|
17
|
+
Head,
|
18
|
+
DocumentContext,
|
19
|
+
Body,
|
20
|
+
} from '@modern-js/runtime/document';
|
21
|
+
```
|
22
|
+
|
23
|
+
### 导出
|
24
|
+
```tsx
|
25
|
+
export default Document() {}
|
26
|
+
|
27
|
+
```
|
28
|
+
|
29
|
+
### 文件位置
|
30
|
+
|
31
|
+
Document 文件,默认在应用根目录下:
|
32
|
+
|
33
|
+
```bash
|
34
|
+
.
|
35
|
+
├── node_modules
|
36
|
+
├── src
|
37
|
+
│ ├── modern-app-env.d.ts
|
38
|
+
│ ├── myapp
|
39
|
+
│ │ └── routes
|
40
|
+
│ │ ├── index.css
|
41
|
+
│ │ ├── layout.tsx
|
42
|
+
│ │ ├── Document.tsx
|
43
|
+
│ │ └── page.tsx
|
44
|
+
│ ├── new-entry
|
45
|
+
│ │ └── routes
|
46
|
+
│ │ ├── index.css
|
47
|
+
│ │ ├── layout.tsx
|
48
|
+
│ │ ├── Document.tsx
|
49
|
+
│ │ └── page.tsx
|
50
|
+
│ └── Document.tsx
|
51
|
+
├── modern.config.ts
|
52
|
+
├── package.json
|
53
|
+
├── pnpm-lock.yaml
|
54
|
+
├── README.md
|
55
|
+
└── tsconfig.json
|
56
|
+
```
|
57
|
+
|
58
|
+
多 entry 场景构建时,优先 entry 的根目录下的 Docoument.tsx 文件。如果当前 entry 没有 Document.tsx 文件,则会查找根目录下的 Document.tsx 文件。
|
59
|
+
如果还没有,则会 fallback 到 `html 模板` 的逻辑。
|
60
|
+
|
61
|
+
### 子组件
|
62
|
+
Document 模板共提供了 `Html`、`Root` `Head` `Body` 渲染页面的组件,以及 `DocumentContext` 等提供
|
63
|
+
分别渲染:
|
64
|
+
- `Html`: 提供 html 原生 dom。并计算出 `DocumentStructrueContext` 的值,将 `Html` 的结构传递给子组件,判断其它子组件是否默认渲染。
|
65
|
+
|
66
|
+
- `Body`: 渲染生成 `body` 节点。其子元素包含 `Root` 组件。支持其它元素同时作为子元素,例如页脚。
|
67
|
+
|
68
|
+
- `Root`: 渲染的根节点 `<div id='root'></div>`。默认根节点的 `id = 'root'`。可以设置 props.rootId 来更改 id 属性。子元素,也会被渲染进 DOM 里,随着 react 渲染完成,会替换掉,一般用来实现全局 loading。
|
69
|
+
|
70
|
+
- `Head`: 渲染生成 `head` 节点。会自动填充 meta 元素,以及 `Scripts` 组件。
|
71
|
+
|
72
|
+
- `Scripts`: 将构建产生的 script 标签渲染到该位置。用于调整构建产物的位置,默认放在 `Head` 组件里,用于
|
73
|
+
|
74
|
+
`Html` 组件中,`Head` 和 `Body` 是必须要存在的,其它组件可以按需选择合适的组件进行组装。
|
75
|
+
|
76
|
+
### 模板参数
|
77
|
+
|
78
|
+
因为是 JSX 形式,Document.tsx 里,可以比较自由的在组件内使用各种变量去赋值给各种自定义组件。
|
79
|
+
但同时 Document 自身也提供了 `DocumentContext` context 来提供一些配置、环境参数,方便直接获取。主要以下参数:
|
80
|
+
|
81
|
+
- processEnv:提供构建时的 `process.env`
|
82
|
+
- config: Modern.js 项目的配置。目前只暴露出 output 相关的配置
|
83
|
+
- entryName: 当前的 entry 名。
|
84
|
+
- templateParams: html 模板的参数,由 builder 提供。对应 [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) 的 `templateParameters` 配置项最终获取到的结果。不建议使用!
|
85
|
+
|
86
|
+
|
87
|
+
## 示例
|
88
|
+
|
89
|
+
```tsx
|
90
|
+
import React, { useContext } from 'react';
|
91
|
+
import {
|
92
|
+
Html,
|
93
|
+
Root,
|
94
|
+
Head,
|
95
|
+
DocumentContext,
|
96
|
+
Body,
|
97
|
+
} from '@modern-js/runtime/document';
|
98
|
+
import Script from '@/components/Script';
|
99
|
+
|
100
|
+
// 默认导出
|
101
|
+
export default function Document(): React.ReactElement {
|
102
|
+
// DocumentContext 提供一些构建时的参数
|
103
|
+
const {
|
104
|
+
config: { output: htmlConfig },
|
105
|
+
entryName,
|
106
|
+
templateParams,
|
107
|
+
} = useContext(DocumentContext);
|
108
|
+
|
109
|
+
return (
|
110
|
+
<Html>
|
111
|
+
<Head>
|
112
|
+
// Head 组件支持自定义子元素。包括 link, script
|
113
|
+
<link href="https://www.baidu.com">百度</link>
|
114
|
+
<script
|
115
|
+
// inline script 的脚本需要如下处理
|
116
|
+
dangerouslySetInnerHTML={{
|
117
|
+
__html: `window.b = 22`,
|
118
|
+
}}
|
119
|
+
></script>
|
120
|
+
</Head>
|
121
|
+
<Body>
|
122
|
+
// rootId 可以更改根元素的 id
|
123
|
+
<Root rootId="root">
|
124
|
+
// Root 支持子元素
|
125
|
+
<h1 style={{ color: 'red' }}>以下为构建时传过来的参数:</h1>
|
126
|
+
<h2> entryName:{entryName}</h2>
|
127
|
+
<h2> title:{htmlConfig.title}</h2>
|
128
|
+
<h2> rootId: {templateParams.mountId}</h2>
|
129
|
+
</Root>
|
130
|
+
// Body 组件支持 Root 以外增加不同的组件,共同组成页面
|
131
|
+
<h1>bottom</h1>
|
132
|
+
</Body>
|
133
|
+
</Html>
|
134
|
+
);
|
135
|
+
}
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
以上文件,将会生成以下 html 文件:
|
140
|
+
|
141
|
+
```html
|
142
|
+
<!DOCTYPE html>
|
143
|
+
<html>
|
144
|
+
|
145
|
+
<head>
|
146
|
+
<meta charset="utf-8">
|
147
|
+
<meta name="viewport"
|
148
|
+
content="width=device-width, initial-scale=1.0, shrink-to-fit=no, viewport-fit=cover, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
149
|
+
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
150
|
+
<meta name="renderer" content="webkit">
|
151
|
+
<meta name="layoutmode" content="standard">
|
152
|
+
<meta name="imagemode" content="force">
|
153
|
+
<meta name="wap-font-scale" content="no">
|
154
|
+
<meta name="format-detection" content="telephone=no">
|
155
|
+
<script>...</script>
|
156
|
+
<script defer src="/static/js/lib-react.js"></script>
|
157
|
+
<script defer src="/static/js/lib-polyfill.js"></script>
|
158
|
+
<script defer src="/static/js/lib-router.js"></script>
|
159
|
+
<script defer
|
160
|
+
src="/static/js/vendors-node_modules_pnpm_loadable_component_5_15_2_react_18_2_0_node_modules_loadable_compon-3fb0cf.js"></script>
|
161
|
+
<script defer
|
162
|
+
src="/static/js/packages_runtime_plugin-router-legacy_dist_js_treeshaking_runtime_index_js-packages_runtime_p-28f4c9.js"></script>
|
163
|
+
<script defer src="/static/js/sub.js"></script>
|
164
|
+
<link href="https://www.baidu.com" />
|
165
|
+
<script>window.b = 22</script>
|
166
|
+
</head>
|
167
|
+
|
168
|
+
<body>
|
169
|
+
<div id="root">
|
170
|
+
<!--<?- html ?>-->
|
171
|
+
<h1 style="color:red">以下为构建时传过来的参数:</h1>
|
172
|
+
<h2> entryName:sub</h2>
|
173
|
+
<h2> title:</h2>
|
174
|
+
<h2> rootId: root</h2>
|
175
|
+
</div>
|
176
|
+
<h1>bottom</h1>
|
177
|
+
<!--<?- chunksMap.js ?>-->
|
178
|
+
<!--<?- SSRDataScript ?>-->
|
179
|
+
</body>
|
180
|
+
|
181
|
+
</html>
|
182
|
+
```
|
183
|
+
|
184
|
+
|
@@ -219,7 +219,7 @@ export default const ErrorBoundary = () => {
|
|
219
219
|
|
220
220
|
## 自控式路由
|
221
221
|
|
222
|
-
以 `
|
222
|
+
以 `src/App.tsx` 为约定的入口,Modern.js 不会多路由做额外的操作,开发者可以自行使用 React Router 6 的 API 进行开发,例如:
|
223
223
|
|
224
224
|
```tsx
|
225
225
|
import { Route, Routes, BrowserRouter } from '@modern-js/runtime/router';
|