@modern-js/main-doc 2.14.0 → 2.15.0
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/CHANGELOG.md
CHANGED
@@ -31,7 +31,9 @@ MODERN_ENV priority is higher than NODE_ENV.
|
|
31
31
|
|
32
32
|
### MODERN_TARGET
|
33
33
|
|
34
|
-
|
34
|
+
When using `@modern-js/runtime`, Modern.js will automatically inject `MODERN_TARGET` to distinguish between SSR and CSR environments.
|
35
|
+
|
36
|
+
You can use `process.env.MODERN_TARGET` to judge the environment and execute the appropriate code.
|
35
37
|
|
36
38
|
```ts title="App.tsx"
|
37
39
|
function App() {
|
@@ -41,9 +43,10 @@ function App() {
|
|
41
43
|
}
|
42
44
|
```
|
43
45
|
|
44
|
-
|
46
|
+
After the development build, you can see that the SSR and CSR bundles as follows:
|
45
47
|
|
46
48
|
```js title="dist/bundles/main.js"
|
49
|
+
// SSR bundles
|
47
50
|
function App() {
|
48
51
|
if (false) {
|
49
52
|
}
|
@@ -51,6 +54,7 @@ function App() {
|
|
51
54
|
```
|
52
55
|
|
53
56
|
```js title="dist/static/main.js"
|
57
|
+
// CSR bundles
|
54
58
|
function App() {
|
55
59
|
if (true) {
|
56
60
|
console.log(window.innerHeight);
|
@@ -58,12 +62,16 @@ function App() {
|
|
58
62
|
}
|
59
63
|
```
|
60
64
|
|
61
|
-
|
62
|
-
In a production environment, dead code is removed, such as the `if` statement above.
|
65
|
+
This can provide different outputs for different environments to ensure that the bundle size is minimized. It can also be convenient to deal with some side effects for different environments.
|
63
66
|
|
64
|
-
:::
|
67
|
+
:::note Dead Code Elimination
|
68
|
+
In the production environment, minimizers such as Terser and SWC will analyze the code and remove dead code to reduce the bundle size. This process is called "Dead Code Elimination" (DCE).
|
65
69
|
|
66
|
-
|
70
|
+
For example, the code inside the `if (false)` statement will be removed, while the code inside the `if (true)` will be preserved.
|
71
|
+
|
72
|
+
If you do not use `process.env.MODERN_TARGET` as described above, the code minimizer may not be able to analyze the dead code, resulting in an increased bundle size.
|
73
|
+
|
74
|
+
:::
|
67
75
|
|
68
76
|
## Custom Environment Variables
|
69
77
|
|
@@ -0,0 +1,110 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 1
|
3
|
+
---
|
4
|
+
|
5
|
+
# Dependencies FAQ
|
6
|
+
|
7
|
+
### How to check the actual installed version of a dependency in the project?
|
8
|
+
|
9
|
+
You can use the `ls` command that provided by the package manager to view the dependencies version.
|
10
|
+
|
11
|
+
The following are some basic examples, please refer to the documentation of each package manager for detailed usage.
|
12
|
+
|
13
|
+
**npm / yarn**
|
14
|
+
|
15
|
+
For projects using npm or yarn, the `npm ls` command can be used.
|
16
|
+
|
17
|
+
For example, execute `npm ls @modern-js/core`, you can see the following results:
|
18
|
+
|
19
|
+
```
|
20
|
+
project
|
21
|
+
└─┬ @modern-js/app-tools@2.0.0
|
22
|
+
└── @modern-js/core@2.0.0
|
23
|
+
```
|
24
|
+
|
25
|
+
**pnpm**
|
26
|
+
|
27
|
+
For projects using pnpm, you can use `pnpm ls` command.
|
28
|
+
|
29
|
+
For example, execute `pnpm ls @modern-js/core --depth Infinity`, you can see the following results:
|
30
|
+
|
31
|
+
```
|
32
|
+
devDependencies:
|
33
|
+
@modern-js/app-tools 2.0.0
|
34
|
+
└── @modern-js/core 2.0.0
|
35
|
+
```
|
36
|
+
|
37
|
+
---
|
38
|
+
|
39
|
+
### The engine "node" is incompatible when installing dependencies?
|
40
|
+
|
41
|
+
If the following error message appears when installing dependencies, it means that the version of Node.js used in the current environment is too low, and Node.js needs to be upgraded to a higher version.
|
42
|
+
|
43
|
+
```bash
|
44
|
+
The engine "node" is incompatible with this module.
|
45
|
+
|
46
|
+
Expected version ">=14.17.6". Got "12.20.1"
|
47
|
+
```
|
48
|
+
|
49
|
+
When using Modern.js, it is recommended to use [Node.js 14.x](https://nodejs.org/download/release/latest-v14.x/) or [Node.js 16.x](https://nodejs.org/download/release/latest-v16.x/).
|
50
|
+
|
51
|
+
If the Node.js version in the current environment is lower than the required version, you can use [nvm](https://github.com/nvm-sh/nvm) or [fnm](https://github.com/Schniz /fnm) and other tools for version switching.
|
52
|
+
|
53
|
+
Here's an example using nvm:
|
54
|
+
|
55
|
+
```
|
56
|
+
# Install Node.js v14
|
57
|
+
nvm install 14
|
58
|
+
|
59
|
+
# Switch to Node 14
|
60
|
+
nvm use 14
|
61
|
+
|
62
|
+
# Set Node 14 as the default version
|
63
|
+
nvm default 14
|
64
|
+
```
|
65
|
+
|
66
|
+
It is recommended to use [fnm](https://github.com/Schniz/fnm) in the local development environment. Its usage is similar to nvm, but it has better performance than nvm.
|
67
|
+
|
68
|
+
---
|
69
|
+
|
70
|
+
### ReactNode type error after upgrading dependencies?
|
71
|
+
|
72
|
+
If the following types of errors are reported after upgrading the project's dependencies, it means that the wrong `@types/react` version is installed in the project.
|
73
|
+
|
74
|
+
```bash
|
75
|
+
The types returned by 'render()' are incompatible between these types.
|
76
|
+
Type 'React.ReactNode' is not assignable to type 'import("/node_modules/@types/react/index").ReactNode'.
|
77
|
+
Type '{}' is not assignable to type 'ReactNode'.
|
78
|
+
```
|
79
|
+
|
80
|
+
The reason for this problem is that the ReactNode type definitions in React 18 and React 16/17 are different. If there are multiple different `@types/react` versions in the project, there will be a ReactNode type conflict, resulting in the above error.
|
81
|
+
|
82
|
+
The solution is to lock `@types/react` and `@types/react-dom` in the project to a unified version, such as `v17`.
|
83
|
+
|
84
|
+
```json
|
85
|
+
{
|
86
|
+
"@types/react": "^17",
|
87
|
+
"@types/react-dom": "^17"
|
88
|
+
}
|
89
|
+
```
|
90
|
+
|
91
|
+
For the method of locking the dependency version, please refer to [Lock nested dependency](/guides/get-started/upgrade.html#lock-nested-dependency).
|
92
|
+
|
93
|
+
---
|
94
|
+
|
95
|
+
### After pnpm install, there are some peer dependencies warnings in the console?
|
96
|
+
|
97
|
+
The reason for this warning is that the version range of peer dependencies declared by some third-party npm packages does not match the version range installed in Modern.js.
|
98
|
+
|
99
|
+
In most cases, you can ignore the peer dependency warnings because it will not affect the use of Modern.js.
|
100
|
+
|
101
|
+
---
|
102
|
+
|
103
|
+
### What is the minimum React version supported by the Modern.js framework?
|
104
|
+
|
105
|
+
The recommended React version for **Modern.js framework is >= 18.0.0**, and different features have different React version requirements.
|
106
|
+
|
107
|
+
- If you are using React 17, some framework features, such as Steaming SSR, will not be available as they rely on new features provided by React 18.
|
108
|
+
- If you're still using React 16, you won't be able to use Modern.js's runtime or server-side featurs. You can consider using Modern.js's build mode, that is, only use Modern.js as a builder. In this case, you can still use React 16.
|
109
|
+
|
110
|
+
In a future major release of Modern.js, we will remove support for React 16 and React 17. Please upgrade to React 18+ as soon as possible.
|
@@ -31,7 +31,9 @@ MODERN_ENV 的优先级高于 NODE_ENV。
|
|
31
31
|
|
32
32
|
### MODERN_TARGET
|
33
33
|
|
34
|
-
使用 `@modern-js/runtime`
|
34
|
+
使用 `@modern-js/runtime` 时,Modern.js 会自动注入 `MODERN_TARGET`,用于区分 SSR 与 CSR 环境。
|
35
|
+
|
36
|
+
你可以在代码中通过 `process.env.MODERN_TARGET` 来判断环境,并执行相应的逻辑。
|
35
37
|
|
36
38
|
```ts title="App.tsx"
|
37
39
|
function App() {
|
@@ -41,9 +43,10 @@ function App() {
|
|
41
43
|
}
|
42
44
|
```
|
43
45
|
|
44
|
-
|
46
|
+
在开发环境构建完成后,可以看到 SSR 产物和 CSR 产物如下:
|
45
47
|
|
46
48
|
```js title="dist/bundles/main.js"
|
49
|
+
// SSR 产物
|
47
50
|
function App() {
|
48
51
|
if (false) {
|
49
52
|
}
|
@@ -51,6 +54,7 @@ function App() {
|
|
51
54
|
```
|
52
55
|
|
53
56
|
```js title="dist/static/main.js"
|
57
|
+
// CSR 产物
|
54
58
|
function App() {
|
55
59
|
if (true) {
|
56
60
|
console.log(window.innerHeight);
|
@@ -58,12 +62,17 @@ function App() {
|
|
58
62
|
}
|
59
63
|
```
|
60
64
|
|
61
|
-
|
62
|
-
|
65
|
+
这种方式可以针对不同客户端提供不同的产物,保证代码体积最小化;也便于处理不同环境下代码中的一些副作用。
|
66
|
+
|
67
|
+
:::note 死代码移除
|
68
|
+
在生产环境,Terser 和 SWC 等代码压缩工具会分析代码,并将 dead code 移除,从而减少产物体积,这个过程被称为死代码移除(DCE)。
|
69
|
+
|
70
|
+
例如,上述 `if (false)` 语句包含的代码会被移除,而 `if (true)` 包含的代码将被保留。
|
71
|
+
|
72
|
+
如果你未按照上述写法来使用 `process.env.MODERN_TARGET`,代码压缩工具可能会无法分析出 dead code,从而导致产物体积增大。
|
63
73
|
|
64
74
|
:::
|
65
75
|
|
66
|
-
这种方式可以针对不同客户端提供不同的产物,保证代码体积最小化。也能方便处理不同环境下,代码中的一些副作用,
|
67
76
|
|
68
77
|
## 自定义环境变量
|
69
78
|
|
@@ -18,8 +18,8 @@ sidebar_position: 1
|
|
18
18
|
|
19
19
|
```
|
20
20
|
project
|
21
|
-
└─┬ @modern-js/app-tools@
|
22
|
-
└── @modern-js/core@
|
21
|
+
└─┬ @modern-js/app-tools@2.0.0
|
22
|
+
└── @modern-js/core@2.0.0
|
23
23
|
```
|
24
24
|
|
25
25
|
**pnpm**
|
@@ -30,8 +30,8 @@ project
|
|
30
30
|
|
31
31
|
```
|
32
32
|
devDependencies:
|
33
|
-
@modern-js/app-tools
|
34
|
-
└── @modern-js/core
|
33
|
+
@modern-js/app-tools 2.0.0
|
34
|
+
└── @modern-js/core 2.0.0
|
35
35
|
```
|
36
36
|
|
37
37
|
---
|
@@ -88,7 +88,7 @@ Type '{}' is not assignable to type 'ReactNode'.
|
|
88
88
|
}
|
89
89
|
```
|
90
90
|
|
91
|
-
|
91
|
+
关于锁定依赖版本的方法,请参考 [锁定子依赖](/guides/get-started/upgrade.html#锁定子依赖)。
|
92
92
|
|
93
93
|
---
|
94
94
|
|
@@ -97,3 +97,14 @@ Type '{}' is not assignable to type 'ReactNode'.
|
|
97
97
|
出现该警告的原因是,某些三方 npm 包声明的 peer dependencies 版本范围与 Modern.js 中安装的版本范围不一致。
|
98
98
|
|
99
99
|
绝大多数情况下,peer dependencies 的警告不会影响项目运行,不需要额外进行处理,请忽略相关 warning。
|
100
|
+
|
101
|
+
---
|
102
|
+
|
103
|
+
### Modern.js 框架最低支持的 React 版本是多少?
|
104
|
+
|
105
|
+
**Modern.js 框架推荐使用的 React 版本为 >= 18.0.0**,并且不同功能对 React 版本的要求有所不同。
|
106
|
+
|
107
|
+
- 如果你在使用 React 17,那么部分框架功能将无法使用,比如 Steaming SSR,因为它依赖 React 18 提供的新特性。
|
108
|
+
- 如果你仍然在使用 React 16,那么将无法使用 Modern.js 的运行时或服务端能力。你可以考虑使用 Modern.js 的构建模式,即只使用 Modern.js 的构建能力,这种情况是可以继续使用 React 16 的。
|
109
|
+
|
110
|
+
在 Modern.js 未来的 major 版本中,我们会逐步移除对 React 16 和 React 17 的支持。因此,请尽快升级到 React 18 以上版本。
|
package/package.json
CHANGED
@@ -11,13 +11,13 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "2.
|
14
|
+
"version": "2.15.0",
|
15
15
|
"publishConfig": {
|
16
16
|
"registry": "https://registry.npmjs.org/",
|
17
17
|
"access": "public"
|
18
18
|
},
|
19
19
|
"peerDependencies": {
|
20
|
-
"@modern-js/builder-doc": "^2.
|
20
|
+
"@modern-js/builder-doc": "^2.15.0"
|
21
21
|
},
|
22
22
|
"devDependencies": {
|
23
23
|
"classnames": "^2",
|
@@ -29,9 +29,9 @@
|
|
29
29
|
"fs-extra": "^10",
|
30
30
|
"@types/node": "^16",
|
31
31
|
"@types/fs-extra": "^9",
|
32
|
-
"@modern-js/builder-doc": "2.
|
33
|
-
"@modern-js/doc-tools": "2.
|
34
|
-
"@modern-js/doc-plugin-auto-sidebar": "2.
|
32
|
+
"@modern-js/builder-doc": "2.15.0",
|
33
|
+
"@modern-js/doc-tools": "2.15.0",
|
34
|
+
"@modern-js/doc-plugin-auto-sidebar": "2.15.0"
|
35
35
|
},
|
36
36
|
"scripts": {
|
37
37
|
"dev": "modern dev",
|