@modern-js/main-doc 0.0.0-next-20221205140617 → 0.0.0-next-20221206090411
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 +17 -17
- package/en/docusaurus-plugin-content-docs/current/components/reduck-migration.md +1 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/swc.md +42 -0
- package/package.json +3 -3
- package/zh/components/reduck-migration.md +1 -0
- package/zh/configure/app/experiments/_category_.json +4 -0
- package/zh/configure/app/html/_category_.json +4 -0
- package/zh/configure/app/performance/_category_.json +4 -0
- package/zh/configure/app/runtime/state.md +13 -0
- package/zh/configure/app/security/_category_.json +4 -0
- package/zh/configure/app/tools/swc.md +42 -0
- package/zh/guides/advanced-features/ssr.md +6 -8
- package/zh/guides/basic-features/env-vars.md +34 -0
- package/zh/guides/topic-detail/model/quick-start.md +4 -0
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-20221206090411
|
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-20221206090411
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
---
|
2
|
+
title: tools.swc
|
3
|
+
sidebar_label: swc
|
4
|
+
---
|
5
|
+
|
6
|
+
- Type: `Object`
|
7
|
+
- Default: `undefined`
|
8
|
+
|
9
|
+
## Introduction
|
10
|
+
|
11
|
+
[SWC](https://SWC.rs/) (Speedy Web Compiler) is a transformer and minimizer for JavaScript and TypeScript based on `Rust`. SWC can provide the same abilities with Babel, and it's more than 10x faster than Babel.
|
12
|
+
|
13
|
+
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.
|
14
|
+
|
15
|
+
## Install
|
16
|
+
|
17
|
+
The `@modern-js/plugin-swc` plugin needs to be installed before use. After the installation, the SWC compilation and compression will be automatically enabled.
|
18
|
+
|
19
|
+
```bash
|
20
|
+
pnpm add @modern-js/plugin-swc -D
|
21
|
+
```
|
22
|
+
|
23
|
+
## Config
|
24
|
+
|
25
|
+
You can set the SWC compilation behavior through the `tools.swc` config.
|
26
|
+
|
27
|
+
```js title="modern.config.ts"
|
28
|
+
import { defineConfig } from '@modern-js/app-tools';
|
29
|
+
|
30
|
+
export default defineConfig({
|
31
|
+
tools: {
|
32
|
+
swc: {
|
33
|
+
jsMinify: {
|
34
|
+
compress: {},
|
35
|
+
mangle: true,
|
36
|
+
},
|
37
|
+
},
|
38
|
+
},
|
39
|
+
});
|
40
|
+
```
|
41
|
+
|
42
|
+
For config details, please refer to [Modern.js Builder - SWC Plugin Configuration](https://modernjs.dev/builder/en/plugins/plugin-swc.html#config).
|
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-20221206090411",
|
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-20221206090411"
|
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-20221206090411"
|
28
28
|
},
|
29
29
|
"scripts": {
|
30
30
|
"build": "npx ts-node ./scripts/sync.ts"
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -12,6 +12,19 @@ sidebar_label: state
|
|
12
12
|
|
13
13
|
具体配置项如下:
|
14
14
|
|
15
|
+
## `models`
|
16
|
+
|
17
|
+
* 类型:`Array<Model>`
|
18
|
+
* 默认值:`[]`
|
19
|
+
|
20
|
+
注册提前挂载的 model 对象,这些 model 会在 Reduck store 创建完成后立即执行挂载。一般使用无需提前挂载。
|
21
|
+
|
22
|
+
## `initialState`
|
23
|
+
* 类型: `Object`
|
24
|
+
* * 默认值:`{}`
|
25
|
+
|
26
|
+
用于设置全局 store 的初始状态。一般用于 SSR 在 hydration 阶段初始化数据。
|
27
|
+
|
15
28
|
## `immer`
|
16
29
|
|
17
30
|
* 类型:`boolean`
|
@@ -0,0 +1,42 @@
|
|
1
|
+
---
|
2
|
+
title: tools.swc
|
3
|
+
sidebar_label: swc
|
4
|
+
---
|
5
|
+
|
6
|
+
- 类型: `Object`
|
7
|
+
- 默认值: `undefined`
|
8
|
+
|
9
|
+
## 介绍
|
10
|
+
|
11
|
+
[SWC](https://swc.rs/) (Speedy Web Compiler) 是基于 `Rust` 语言编写的高性能 JavaScript 和 TypeScript 转译和压缩工具。在 Polyfill 和语法降级方面可以和 Babel 提供一致的能力,并且比 Babel 性能高出 10 倍不止。
|
12
|
+
|
13
|
+
Modern.js 提供了开箱即用的 SWC 插件,可以为你的 Web 应用提供语法降级、Polyfill 以及压缩,并且移植了一些额外常见的 Babel 插件。
|
14
|
+
|
15
|
+
## 安装
|
16
|
+
|
17
|
+
使用前需要安装 `@modern-js/plugin-swc` 插件,安装完成后,会自动启用 SWC 编译和压缩能力。
|
18
|
+
|
19
|
+
```bash
|
20
|
+
pnpm add @modern-js/plugin-swc -D
|
21
|
+
```
|
22
|
+
|
23
|
+
## 配置项
|
24
|
+
|
25
|
+
你可以通过 `tools.swc` 配置项来设置 SWC 编译行为。
|
26
|
+
|
27
|
+
```js title="modern.config.ts"
|
28
|
+
import { defineConfig } from '@modern-js/app-tools';
|
29
|
+
|
30
|
+
export default defineConfig({
|
31
|
+
tools: {
|
32
|
+
swc: {
|
33
|
+
jsMinify: {
|
34
|
+
compress: {},
|
35
|
+
mangle: true,
|
36
|
+
},
|
37
|
+
},
|
38
|
+
},
|
39
|
+
});
|
40
|
+
```
|
41
|
+
|
42
|
+
完整配置项请参考 [Modern.js Builder - SWC 插件配置](https://modernjs.dev/builder/zh/plugins/plugin-swc.html#%E9%85%8D%E7%BD%AE)。
|
@@ -189,20 +189,18 @@ import { PreRender } from '@modern-js/runtime/ssr';
|
|
189
189
|
|
190
190
|
### 环境变量区分
|
191
191
|
|
192
|
-
在组件中可以直接使用
|
192
|
+
在组件中可以直接使用 Modern.js 内置的环境变量 `MODERN_TARGET` 进行判断,方便在构建时删除无用代码:
|
193
193
|
|
194
194
|
```ts
|
195
195
|
export default () => {
|
196
|
-
if (process.env.
|
197
|
-
console.log('server render')
|
196
|
+
if (process.env.MODERN_TARGET === 'node') {
|
197
|
+
console.log('server render');
|
198
198
|
} else {
|
199
|
-
console.log('client render')
|
199
|
+
console.log('client render');
|
200
200
|
}
|
201
201
|
}
|
202
202
|
```
|
203
203
|
|
204
|
-
在 SSR 中直接调用 RPC 接口时,该环境变量会非常有用,因为 RPC 调用时往往会执行到浏览器中无法运行的代码。
|
205
|
-
|
206
204
|
:::note
|
207
205
|
更多内容可以查看[环境变量](/docs/guides/basic-features/env-vars)。
|
208
206
|
:::
|
@@ -240,11 +238,11 @@ export default () => {
|
|
240
238
|
|
241
239
|
### 独立文件
|
242
240
|
|
243
|
-
上述两种方式,都会为开发者带来一些心智负担。Modern.js 正在基于[嵌套路由](/docs/guides/basic-features/routes)
|
241
|
+
上述两种方式,都会为开发者带来一些心智负担。Modern.js 正在基于[嵌套路由](/docs/guides/basic-features/routes)开发设计[更简单的方案](/docs/guides/basic-features/data-fetch)来分离 CSR 和 SSR 的代码。
|
244
242
|
|
245
243
|
## 接口请求
|
246
244
|
|
247
|
-
在 SSR 中发起接口请求时,开发者有时自己封装了同构的请求工具。部分接口需要传递用户 Cookie,开发者可以通过 [`useRuntimeContext`](/docs/
|
245
|
+
在 SSR 中发起接口请求时,开发者有时自己封装了同构的请求工具。部分接口需要传递用户 Cookie,开发者可以通过 [`useRuntimeContext`](/docs/guides/basic-features/data-fetch#route-loader) API 获取到请求头来实现。
|
248
246
|
|
249
247
|
需要注意的是,此时获取到的是 HTML 请求的请求头,不一定适用于接口请求,因此**千万不能**透传所有请求头。并且,一些后端接口,或是通用网关,会根据请求头中的信息做校验,全量透传容易出现各种难以排查的问题,推荐**按需透传**。
|
250
248
|
|
@@ -23,6 +23,40 @@ Modern.js 提供了对环境变量的支持,包含内置的环境变量和自
|
|
23
23
|
MODERN_ENV 的优先级高于 NODE_ENV。
|
24
24
|
:::
|
25
25
|
|
26
|
+
### MODERN_TARGET
|
27
|
+
|
28
|
+
使用 `@modern-js/runtime` 时会自动注入,用于区分 SSR 与 CSR 环境。开发者可以自行在代码中判断,构建时会默认移除 dead code。
|
29
|
+
|
30
|
+
```ts title="App.tsx"
|
31
|
+
function App() {
|
32
|
+
if (process.env.MODERN_TARGET === 'browser') {
|
33
|
+
console.log(window.innerHeight);
|
34
|
+
};
|
35
|
+
};
|
36
|
+
```
|
37
|
+
|
38
|
+
开发环境打包后,可以看到 SSR 产物和 CSR 产物如下:
|
39
|
+
|
40
|
+
```js title="dist/bundles/main.js"
|
41
|
+
function App() {
|
42
|
+
if (false) {}
|
43
|
+
}
|
44
|
+
```
|
45
|
+
|
46
|
+
```js title="dist/static/main.js"
|
47
|
+
function App() {
|
48
|
+
if (true) {
|
49
|
+
console.log(window.innerHeight);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
```
|
53
|
+
|
54
|
+
:::note
|
55
|
+
在生产环境,会将 dead code 移除,例如上述 `if` 语句。
|
56
|
+
:::
|
57
|
+
|
58
|
+
这种方式可以针对不同客户端提供不同的产物,保证代码体积最小化。也能方便处理不同环境下,代码中的一些副作用,
|
59
|
+
|
26
60
|
## 自定义环境变量
|
27
61
|
|
28
62
|
环境变量支持通过 `shell` 和 `.env` 文件两种方式指定。
|
@@ -3,6 +3,10 @@ sidebar_position: 1
|
|
3
3
|
title: 快速上手
|
4
4
|
---
|
5
5
|
|
6
|
+
import ReduckMigration from '@site-docs/components/reduck-migration.md'
|
7
|
+
|
8
|
+
<ReduckMigration />
|
9
|
+
|
6
10
|
[Reduck](https://github.com/modern-js-dev/reduck) 是 Modern.js 团队开发的遵循 MVC 模式的状态管理库,底层状态存储基于 [Redux](https://redux.js.org/) 实现,同时提供更高层级的抽象,并完全兼容 Redux 生态。
|
7
11
|
|
8
12
|
Reduck 的目标是以 MVC 模式组织 React 应用开发结构,将业务逻辑维护在 Model 层,业务逻辑与 UI 解耦,让开发业务逻辑更集中、更简单,同时通过更高层级的抽象,减少重复工作(样板代码)。
|