@modern-js/main-doc 2.50.0 → 2.51.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -87,12 +87,10 @@ It is recommended to use [UnstableMiddleware](/guides/advanced-features/web-serv
|
|
87
87
|
Modern.js provides a set of APIs by default for projects to use:
|
88
88
|
|
89
89
|
```ts
|
90
|
-
import {
|
90
|
+
import { Middleware } from '@modern-js/runtime/server';
|
91
91
|
|
92
|
-
export const middleware:
|
93
|
-
const {
|
94
|
-
source: { req, res },
|
95
|
-
} = context;
|
92
|
+
export const middleware: Middleware = (context, next) => {
|
93
|
+
const { source: { req, res } } = context;
|
96
94
|
console.log(req.url);
|
97
95
|
next();
|
98
96
|
};
|
@@ -0,0 +1,176 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 15
|
3
|
+
---
|
4
|
+
|
5
|
+
# Deploy
|
6
|
+
|
7
|
+
Currently, Modern.js can run in the node.js environment, you can host it yourself. At the same time, Modern.js officially supports deployment on the Netlify platform.
|
8
|
+
|
9
|
+
:::info
|
10
|
+
Currently Modern.js only supports running in node.js environments, support for other runtime environments will be provided in a later version.
|
11
|
+
:::
|
12
|
+
|
13
|
+
|
14
|
+
## Commands to build products
|
15
|
+
|
16
|
+
Running the `modern deploy` command will automatically build the output file for the production environment. This process includes optimizing the output file and its dependencies, and detecting the current deployment platform and automatically generating a product that will run on that platform.
|
17
|
+
|
18
|
+
If you want to generate and test the output locally for a specific deployment platform, you can specify the platform by setting the environment variable: `modern deploy`:
|
19
|
+
|
20
|
+
```bash
|
21
|
+
MODERNJS_DEPLOY=netlify npx modern deploy
|
22
|
+
```
|
23
|
+
|
24
|
+
:::info
|
25
|
+
When deploying on the deployment platforms officially supported by Modern.js, there is no need to specify environment variables.
|
26
|
+
:::
|
27
|
+
|
28
|
+
|
29
|
+
## Node.js
|
30
|
+
|
31
|
+
### Single Repo
|
32
|
+
|
33
|
+
By default, Modern.js outputs builds that can be run in a Node.js environment when no Modern.js-supported deployment platform is detected.
|
34
|
+
|
35
|
+
Use the following command to build the project:
|
36
|
+
```bash
|
37
|
+
npx modern deploy
|
38
|
+
```
|
39
|
+
|
40
|
+
When running the `modern deploy` command, Modern.js will generate runnable products and output the following content:
|
41
|
+
|
42
|
+
```bash
|
43
|
+
Static directory: `.output/static`
|
44
|
+
You can preview this build by `node .output/index`
|
45
|
+
```
|
46
|
+
|
47
|
+
At this point, you can run the entire server by `node .output/index`, and the static resources required for the page are in the `.output/static` directory. You can upload these static resources to a CDN yourself:
|
48
|
+
|
49
|
+
:::info
|
50
|
+
By default, when running Modern.js Server, it listens on port 8080. If you want to change the listening port, you can specify the `PORT` environment variable:
|
51
|
+
```
|
52
|
+
PORT=3000 node .output/index
|
53
|
+
```
|
54
|
+
:::
|
55
|
+
|
56
|
+
|
57
|
+
### Monorepo
|
58
|
+
|
59
|
+
For the Monorepo project, in addition to building our current project, we also need to build other repositories that the current project depends on.
|
60
|
+
|
61
|
+
We assume that the name in the `package.json` of the current project is `app`. Taking pnpm as an example of a monorepo management tool, you can add the following command to the `package.json` of the current project to build products for the current project:
|
62
|
+
|
63
|
+
```json title="app/package.json"
|
64
|
+
{
|
65
|
+
"scripts": {
|
66
|
+
"deploy": "modern deploy",
|
67
|
+
"deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
|
68
|
+
}
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
If you use rush to manage repositories, you can add the following command in `package.json`:
|
73
|
+
|
74
|
+
```json
|
75
|
+
{
|
76
|
+
"scripts": {
|
77
|
+
"deploy": "modern deploy",
|
78
|
+
"deploy:app": "rush rebuild --to-except app && rushx deploy",
|
79
|
+
}
|
80
|
+
}
|
81
|
+
```
|
82
|
+
|
83
|
+
After the build is completed, Modern.js will generate all dependencies in the `.output/node_modules` directory of the project. Similarly, you can run the entire server using `node .output/index`.
|
84
|
+
|
85
|
+
## Netlify
|
86
|
+
|
87
|
+
Netlify is a popular web development platform designed for building, deploying, and maintaining modern web projects. Deploying on Netlify mainly requires configuring the `netlify.toml` file.
|
88
|
+
|
89
|
+
Depending on the complexity of your project, you can configure it incrementally by this doc.
|
90
|
+
|
91
|
+
### Pure Front-end Project
|
92
|
+
|
93
|
+
Add the `netlify.toml` file to the root directory of the current project:
|
94
|
+
|
95
|
+
```bash
|
96
|
+
./
|
97
|
+
├── src/
|
98
|
+
├── modern.config.ts
|
99
|
+
├── netlify.toml
|
100
|
+
├── package.json
|
101
|
+
```
|
102
|
+
|
103
|
+
Add the following content to `netlify.toml`:
|
104
|
+
```toml
|
105
|
+
[build]
|
106
|
+
publish = "dist"
|
107
|
+
command = "npm run deploy"
|
108
|
+
```
|
109
|
+
|
110
|
+
Add a project to the Netlify platform and deploy it!
|
111
|
+
|
112
|
+
### Full Stack Project
|
113
|
+
|
114
|
+
Full-stack projects refer to projects that use custom servers, SSR, and BFF. These projects need to be deployed on Netlify Functions. Based on the `netlify.toml` file mentioned above,
|
115
|
+
add the following configuration:
|
116
|
+
|
117
|
+
```toml title="netlify.toml"
|
118
|
+
[build]
|
119
|
+
publish = "dist"
|
120
|
+
command = "npm run deploy"
|
121
|
+
|
122
|
+
[functions]
|
123
|
+
directory = ".netlify/functions"
|
124
|
+
node_bundler = "none"
|
125
|
+
included_files = [".netlify/functions/**"]
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
:::info
|
130
|
+
Currently, Modern.js does not support deployment on Netlify Edge Functions. We will support it in future versions.
|
131
|
+
:::
|
132
|
+
|
133
|
+
|
134
|
+
### Monorepo
|
135
|
+
|
136
|
+
For Monorepo projects, in addition to building our current project, we also need to build the dependencies of the current project from other repositories.
|
137
|
+
We take a pnpm monorepo repository as an example and deploy the Monorepo project on Netlify.
|
138
|
+
|
139
|
+
Suppose we have the following Monorepo repository:
|
140
|
+
|
141
|
+
```
|
142
|
+
./
|
143
|
+
├── packages/
|
144
|
+
│ ├── app/
|
145
|
+
│ └── app-dep1
|
146
|
+
├── package.json
|
147
|
+
├── pnpm-lock.yaml
|
148
|
+
└── pnpm-workspace.yaml
|
149
|
+
```
|
150
|
+
|
151
|
+
We need to configure Base directory on the netlify platform as `packages/app`:
|
152
|
+
|
153
|
+
<img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/netlify-monorepo-basedir.png?x-resource-account=public" />
|
154
|
+
|
155
|
+
Add the following script in `packages/app/package.json`, before executing the deployment command of the `app` repository, first execute the build of other repositories in the workspace:
|
156
|
+
|
157
|
+
```json
|
158
|
+
"scripts": {
|
159
|
+
"deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
|
160
|
+
}
|
161
|
+
```
|
162
|
+
|
163
|
+
Configure the build command in `netlify.toml`:
|
164
|
+
|
165
|
+
```toml
|
166
|
+
[build]
|
167
|
+
publish = "dist"
|
168
|
+
command = "npm run deploy:app"
|
169
|
+
|
170
|
+
[functions]
|
171
|
+
directory = ".netlify/functions"
|
172
|
+
node_bundler = "none"
|
173
|
+
included_files = [".netlify/functions/**"]
|
174
|
+
```
|
175
|
+
|
176
|
+
Just submit your code and deploy it using the Netlify platform.
|
@@ -85,9 +85,9 @@ export const afterRender: AfterRenderHook = (ctx, next) => {
|
|
85
85
|
Modern.js 默认提供了一套 API 供项目使用:
|
86
86
|
|
87
87
|
```ts
|
88
|
-
import {
|
88
|
+
import { Middleware } from '@modern-js/runtime/server';
|
89
89
|
|
90
|
-
export const middleware:
|
90
|
+
export const middleware: Middleware = (context, next) => {
|
91
91
|
const {
|
92
92
|
source: { req, res },
|
93
93
|
} = context;
|
@@ -0,0 +1,177 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 15
|
3
|
+
---
|
4
|
+
|
5
|
+
# 部署
|
6
|
+
|
7
|
+
目前 Modern.js 支持在 node.js 环境下运行,你可以自行托管,同时,Modern.js 官方支持在 Netlify 平台上部署。
|
8
|
+
|
9
|
+
:::info
|
10
|
+
当前仅支持在 node.js 环境运行,对其他运行时环境的支持将在后续版本中提供。
|
11
|
+
:::
|
12
|
+
|
13
|
+
|
14
|
+
## 构建产物的命令
|
15
|
+
|
16
|
+
执行 `modern deploy` 命令将自动构建适用于生产环境的输出文件。此过程包括优化输出文件及其依赖,并检测当前部署平台,自动生成可以在该平台运行的产物。
|
17
|
+
如果你希望在本地生成并测试特定部署平台的产物,可以通过设置环境变量来指定平台:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
MODERNJS_DEPLOY=netlify npx modern deploy
|
21
|
+
```
|
22
|
+
|
23
|
+
:::info
|
24
|
+
在 Modern.js 官方支持的部署平台中部署时,无需指定环境变量。
|
25
|
+
:::
|
26
|
+
|
27
|
+
|
28
|
+
## Node.js
|
29
|
+
|
30
|
+
### 单仓库项目
|
31
|
+
|
32
|
+
默认情况下,当未检测到 Modern.js 支持的部署平台时,Modern.js 会输出可以在 Node.js 环境下运行的构建产物。
|
33
|
+
|
34
|
+
使用以下命令构建项目:
|
35
|
+
```bash
|
36
|
+
npx modern deploy
|
37
|
+
```
|
38
|
+
|
39
|
+
当执行 `modern deploy` 命令时,Modern.js 会产出可运行的生产环境产物,并输出以下内容:
|
40
|
+
|
41
|
+
```bash
|
42
|
+
Static directory: `.output/static`
|
43
|
+
You can preview this build by `node .output/index`
|
44
|
+
```
|
45
|
+
|
46
|
+
此时,你可以通过 `node .output/index` 运行整个 server,在 `.output/static` 目录下是页面所需的静态资源,你可以将这些静态资源自行上传至 CDN。
|
47
|
+
|
48
|
+
:::info
|
49
|
+
默认情况下,运行 Modern.js Server 时会监听 8080 端口,如果你想修改监听的端口,可以指定 `PORT` 环境变量:
|
50
|
+
```
|
51
|
+
PORT=3000 node .output/index
|
52
|
+
```
|
53
|
+
:::
|
54
|
+
|
55
|
+
|
56
|
+
### Monorepo
|
57
|
+
|
58
|
+
对于 Monorepo 项目,除了需要构建我们当前的项目外,还需要构建当前项目的依赖的其他仓库。
|
59
|
+
|
60
|
+
假设当前项目的 `package.json` 中的 name 为 `app`,以 pnpm 作为 monorepo 管理工具为例,你可以在当前项目的 `package.json` 添加以下命令用于构建当前项目的产物:
|
61
|
+
|
62
|
+
```json title="app/package.json"
|
63
|
+
{
|
64
|
+
"scripts": {
|
65
|
+
"deploy": "modern deploy",
|
66
|
+
"deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
|
67
|
+
}
|
68
|
+
}
|
69
|
+
```
|
70
|
+
|
71
|
+
如果你使用 rush 管理仓库,可以在 `package.json` 中添加以下命令:
|
72
|
+
|
73
|
+
```json
|
74
|
+
{
|
75
|
+
"scripts": {
|
76
|
+
"deploy": "modern deploy",
|
77
|
+
"deploy:app": "rush rebuild --to-except app && rushx deploy",
|
78
|
+
}
|
79
|
+
}
|
80
|
+
```
|
81
|
+
|
82
|
+
当构建完成后,Modern.js 会将项目中所有的依赖生成在 `.output/node_modules` 目录下。同样的,你可以使用 `node .output/index` 运行整个 Server。
|
83
|
+
|
84
|
+
## Netlify
|
85
|
+
|
86
|
+
Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现代 web 项目而设计,在 Netlify 上部署,主要需要配置 `netlify.toml` 文件,
|
87
|
+
根据你的项目复杂度,可以渐进配置。
|
88
|
+
|
89
|
+
### 纯前端项目
|
90
|
+
|
91
|
+
在当前项目的根目录添加 `netlify.toml` 文件:
|
92
|
+
|
93
|
+
```bash
|
94
|
+
./
|
95
|
+
├── src/
|
96
|
+
├── modern.config.ts
|
97
|
+
├── netlify.toml
|
98
|
+
├── package.json
|
99
|
+
```
|
100
|
+
|
101
|
+
在 `netlify.toml` 中添加以下内容:
|
102
|
+
```toml
|
103
|
+
[build]
|
104
|
+
publish = "dist"
|
105
|
+
command = "npm run deploy"
|
106
|
+
```
|
107
|
+
|
108
|
+
在 Netlify 平台上添加项目,部署即可。
|
109
|
+
|
110
|
+
### 全栈项目
|
111
|
+
|
112
|
+
全栈项目是指使用了 自定义 Server,SSR,BFF 的项目,这些项目需要部署在 Netlify Functions 上。基于上述的 `netlify.toml` 文件,
|
113
|
+
添加以下配置:
|
114
|
+
|
115
|
+
```toml title="netlify.toml"
|
116
|
+
[build]
|
117
|
+
publish = "dist"
|
118
|
+
command = "npm run deploy"
|
119
|
+
|
120
|
+
[functions]
|
121
|
+
directory = ".netlify/functions"
|
122
|
+
node_bundler = "none"
|
123
|
+
included_files = [".netlify/functions/**"]
|
124
|
+
|
125
|
+
```
|
126
|
+
|
127
|
+
:::info
|
128
|
+
目前 Modern.js 还不支持在 Netlify Edge Functions 进行部署,我们将在后续的版本中支持。
|
129
|
+
:::
|
130
|
+
|
131
|
+
|
132
|
+
### Monorepo 项目
|
133
|
+
|
134
|
+
对于 Monorepo 项目,除了需要构建我们当前的项目外,还需要构建当前项目的依赖的其他仓库。我们以一个 pnpm monorepo 仓库为例,
|
135
|
+
在 Netlify 上对 Monorepo 项目进行部署。
|
136
|
+
|
137
|
+
假设我们有以下 Monorepo 仓库:
|
138
|
+
|
139
|
+
```
|
140
|
+
./
|
141
|
+
├── packages/
|
142
|
+
│ ├── app/
|
143
|
+
│ └── app-dep1
|
144
|
+
├── package.json
|
145
|
+
├── pnpm-lock.yaml
|
146
|
+
└── pnpm-workspace.yaml
|
147
|
+
```
|
148
|
+
|
149
|
+
我们需要在 netlify 平台上配置 Base directory 为 `packages/app`:
|
150
|
+
|
151
|
+
<img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/netlify-monorepo-basedir.png?x-resource-account=public" />
|
152
|
+
|
153
|
+
在 `packages/app/package.json` 中添加以下 script,在执行 `app` 仓库的部署命令之前,先执行 workspace 中其他仓库的构建:
|
154
|
+
|
155
|
+
```json
|
156
|
+
"scripts": {
|
157
|
+
"deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
|
158
|
+
}
|
159
|
+
```
|
160
|
+
|
161
|
+
在 `netlify.toml` 配置构建命令:
|
162
|
+
|
163
|
+
```toml
|
164
|
+
[build]
|
165
|
+
publish = "dist"
|
166
|
+
command = "npm run deploy:app"
|
167
|
+
|
168
|
+
[functions]
|
169
|
+
directory = ".netlify/functions"
|
170
|
+
node_bundler = "none"
|
171
|
+
included_files = [".netlify/functions/**"]
|
172
|
+
```
|
173
|
+
|
174
|
+
提交你的代码,使用 Netlify 平台部署即可。
|
175
|
+
|
176
|
+
|
177
|
+
|
package/package.json
CHANGED
@@ -15,17 +15,17 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "2.
|
18
|
+
"version": "2.51.0",
|
19
19
|
"publishConfig": {
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
21
21
|
"access": "public",
|
22
22
|
"provenance": true
|
23
23
|
},
|
24
24
|
"dependencies": {
|
25
|
-
"@modern-js/sandpack-react": "2.
|
25
|
+
"@modern-js/sandpack-react": "2.51.0"
|
26
26
|
},
|
27
27
|
"peerDependencies": {
|
28
|
-
"@modern-js/builder-doc": "^2.
|
28
|
+
"@modern-js/builder-doc": "^2.51.0"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
31
|
"classnames": "^2",
|
@@ -39,8 +39,8 @@
|
|
39
39
|
"@rspress/shared": "1.18.2",
|
40
40
|
"@types/node": "^16",
|
41
41
|
"@types/fs-extra": "9.0.13",
|
42
|
-
"@modern-js/doc
|
43
|
-
"@modern-js/
|
42
|
+
"@modern-js/builder-doc": "2.51.0",
|
43
|
+
"@modern-js/doc-plugin-auto-sidebar": "2.51.0"
|
44
44
|
},
|
45
45
|
"scripts": {
|
46
46
|
"dev": "rspress dev",
|