Skip to content

Commit

Permalink
chore: improve deploy docs (web-infra-dev#5797)
Browse files Browse the repository at this point in the history
  • Loading branch information
zllkjc committed Jun 4, 2024
1 parent f4c671d commit c52f58c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 64 deletions.
68 changes: 38 additions & 30 deletions packages/document/main-doc/docs/en/guides/basic-features/deploy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ sidebar_position: 15

# Deploy

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.
Currently, Modern.js offers two deployment way:
- You can host your application in a container that includes a Node.js environment on your own, which provides flexibility for the deployment of the application.
- You can also deploy your application through a platform. Currently, Modern.js supports the [Netlify](https://www.netlify.com/) platform.

:::info
Currently Modern.js only supports running in node.js environments, support for other runtime environments will be provided in a later version.
Currently, Modern.js only supports running in a Node.js environment. Support for more runtime environments will be provided in the future.
:::


## Commands to build products
## Build Deployment Products

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.
Running the `modern deploy` command will automatically produce deployment products. This process includes optimizing Bundler build products and their dependencies, detecting the current deployment platform, and automatically generating deployment products that can run on that platform.

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`:

Expand All @@ -33,11 +35,12 @@ When deploying on the deployment platforms officially supported by Modern.js, th
By default, Modern.js outputs builds that can be run in a Node.js environment when no Modern.js-supported deployment platform is detected.

Use the following command to build the project:

```bash
npx modern deploy
```

When running the `modern deploy` command, Modern.js will generate runnable products and output the following content:
When running the `modern deploy` command, Modern.js will generate runnable products and output the following content in terminal:

```bash
Static directory: `.output/static`
Expand All @@ -56,35 +59,35 @@ PORT=3000 node .output/index

### Monorepo

For the Monorepo project, in addition to building our current project, we also need to build other repositories that the current project depends on.
For Monorepo projects, in addition to building the current project, it is also necessary to build other sub-projects in the repository that the current project depends on.

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:
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:

```json title="app/package.json"
{
"scripts": {
"deploy": "modern deploy",
"deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
"build:packages": "pnpm --filter 'app^...' run build",
"deploy": "pnpm run build:packages && modern deploy",
}
}
```

If you use rush to manage repositories, you can add the following command in `package.json`:
If you use Rush as your Monorepo management tool, you can add the following commands to your `package.json`:

```json
{
"scripts": {
"deploy": "modern deploy",
"deploy:app": "rush rebuild --to-except app && rushx deploy",
"build:packages": "rush rebuild --to-except app",
"deploy": "rushx build:packages && modern deploy",
}
}
```

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`.
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 Modern.js server using `node .output/index`.

## Netlify

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.
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.

Depending on the complexity of your project, you can configure it incrementally by this doc.

Expand All @@ -93,26 +96,26 @@ Depending on the complexity of your project, you can configure it incrementally
Add the `netlify.toml` file to the root directory of the current project:

```bash
./
├── src/
.
├── src
├── modern.config.ts
├── netlify.toml
── package.json
── package.json
```

Add the following content to `netlify.toml`:

```toml
[build]
publish = "dist"
command = "npm run deploy"
```

Add a project to the Netlify platform and deploy it!
Now, add a project to the Netlify platform and deploy it!

### Full Stack Project

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,
add the following configuration:
Full-stack projects refer to projects that use Custom Web Servers, SSR, and BFF. These projects need to be deployed on **Netlify Functions**. Based on the `netlify.toml` file mentioned above, add the following configuration:

```toml title="netlify.toml"
[build]
Expand All @@ -133,39 +136,44 @@ Currently, Modern.js does not support deployment on Netlify Edge Functions. We w

### Monorepo

For Monorepo projects, in addition to building our current project, we also need to build the dependencies of the current project from other repositories.
We take a pnpm monorepo repository as an example and deploy the Monorepo project on Netlify.
For Monorepo projects, in addition to building our current project, you also need to build other sub-projects in the repository that the current project depends on.

Suppose we have the following Monorepo repository:
We take a pnpm Monorepo repository as an example and deploy the Monorepo project on Netlify.

Assuming the directory structure of the Monorepo repository is as follows:

```
./
├── packages/
│ ├── app/
.
├── packages
│ ├── app
│ └── app-dep1
├── package.json
├── pnpm-lock.yaml
└── pnpm-workspace.yaml
```

We need to configure Base directory on the netlify platform as `packages/app`:
You need to configure Base directory on the netlify platform as `packages/app`:

<img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/netlify-monorepo-basedir.png?x-resource-account=public" />

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:

```json
"scripts": {
"deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
{
"scripts": {
"build:packages": "pnpm --filter 'app^...' run build",
"deploy": "pnpm run build:packages && modern deploy",
}
}

```

Configure the build command in `netlify.toml`:

```toml
[build]
publish = "dist"
command = "npm run deploy:app"
command = "npm run deploy"

[functions]
directory = ".netlify/functions"
Expand Down
72 changes: 38 additions & 34 deletions packages/document/main-doc/docs/zh/guides/basic-features/deploy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ sidebar_position: 15

# 部署

目前 Modern.js 支持在 node.js 环境下运行,你可以自行托管,同时,Modern.js 官方支持在 Netlify 平台上部署。
目前,Modern.js 提供了两种部署方式:

- 你可以将应用自行托管在包含 Node.js 环境的容器中,这为应用提供了部署的灵活性。
- 你也可以通过平台部署应用,目前 Modern.js 官方支持了 [Netlify](https://www.netlify.com/) 平台。

:::info
当前仅支持在 node.js 环境运行,对其他运行时环境的支持将在后续版本中提供
目前 Modern.js 仅支持在 Node.js 环境中运行,未来将提供更多运行时环境的支持
:::


## 构建产物的命令
## 构建部署产物

执行 `modern deploy` 命令将自动构建适用于生产环境的输出文件。此过程包括优化输出文件及其依赖,并检测当前部署平台,自动生成可以在该平台运行的产物
执行 `modern deploy` 命令将自动输出部署产物。此过程包括优化 Bundler 构建产物及产物依赖,检测当前部署平台,并自动生成可以在该平台运行的产物
如果你希望在本地生成并测试特定部署平台的产物,可以通过设置环境变量来指定平台:

```bash
Expand All @@ -29,24 +32,26 @@ MODERNJS_DEPLOY=netlify npx modern deploy

### 单仓库项目

默认情况下,当未检测到 Modern.js 支持的部署平台时,Modern.js 会输出可以在 Node.js 环境下运行的构建产物。
默认情况下,如果未检测到 Modern.js 支持的部署平台,Modern.js 将生成可以在 Node.js 环境下运行的部署产物。

你可以使用以下命令构建项目:

使用以下命令构建项目:
```bash
npx modern deploy
```

当执行 `modern deploy` 命令时,Modern.js 会产出可运行的生产环境产物,并输出以下内容
当执行 `modern deploy` 命令时,Modern.js 将生成可执行的部署产物,并在控制台输出以下内容

```bash
Static directory: `.output/static`
You can preview this build by `node .output/index`
```

此时,你可以通过 `node .output/index` 运行整个 server,`.output/static` 目录下是页面所需的静态资源,你可以将这些静态资源自行上传至 CDN。
现在,你可以通过执行 `node .output/index` 命令来运行服务器。`.output/static` 目录中,存放了页面运行所需的静态资源,你可以选择将这些资源上传到 CDN 以提高访问速度

:::info
默认情况下,运行 Modern.js Server 时会监听 8080 端口,如果你想修改监听的端口,可以指定 `PORT` 环境变量:
默认情况下,运行 Modern.js 服务器时会监听 8080 端口,如果你想修改监听的端口,可以指定 `PORT` 环境变量:

```
PORT=3000 node .output/index
```
Expand All @@ -55,47 +60,46 @@ PORT=3000 node .output/index

### Monorepo

对于 Monorepo 项目,除了需要构建我们当前的项目外,还需要构建当前项目的依赖的其他仓库
对于 Monorepo 项目,除了需要构建当前的项目外,还需要构建当前项目依赖的仓库中其他子项目

假设当前项目的 `package.json` 中的 name 为 `app`,以 pnpm 作为 monorepo 管理工具为例,你可以在当前项目的 `package.json` 添加以下命令用于构建当前项目的产物
假设当前项目的 `package.json` 中的 name 为 `app`,以 pnpm 作为 Monorepo 管理工具为例,你可以在项目 `package.json` 中添加以下命令用于构建

```json title="app/package.json"
{
"scripts": {
"deploy": "modern deploy",
"deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
"build:packages": "pnpm --filter 'app^...' run build",
"deploy": "pnpm run build:packages && modern deploy",
}
}
```

如果你使用 rush 管理仓库,可以在 `package.json` 中添加以下命令:
如果你使用 rush 作为 Monorepo 管理工具,可以在 `package.json` 中添加以下命令:

```json
{
"scripts": {
"deploy": "modern deploy",
"deploy:app": "rush rebuild --to-except app && rushx deploy",
"build:packages": "rush rebuild --to-except app",
"deploy": "rushx build:packages && modern deploy",
}
}
```

当构建完成后,Modern.js 会将项目中所有的依赖生成在 `.output/node_modules` 目录下。同样的,你可以使用 `node .output/index` 运行整个 Server
构建完成后,框架会将项目中所有的依赖生成在 `.output/node_modules` 目录下。你同样可以使用 `node .output/index` 运行 Modern.js 服务器

## Netlify

Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现代 web 项目而设计,在 Netlify 上部署,主要需要配置 `netlify.toml` 文件,
根据你的项目复杂度,可以渐进配置。
Netlify 是一个流行的 Web 开发平台,专为构建、发布和维护现代 Web 项目而设计。在 Netlify 上部署,通常需要配置 `netlify.toml` 文件,你可以根据项目复杂度,渐进的配置该文件。

### 纯前端项目

在当前项目的根目录添加 `netlify.toml` 文件:

```bash
./
├── src/
├── src
├── modern.config.ts
├── netlify.toml
── package.json
── package.json
```

`netlify.toml` 中添加以下内容:
Expand All @@ -109,8 +113,7 @@ Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现

### 全栈项目

全栈项目是指使用了 自定义 Server,SSR,BFF 的项目,这些项目需要部署在 Netlify Functions 上。基于上述的 `netlify.toml` 文件,
添加以下配置:
全栈项目是指使用了自定义 Web Server、SSR、BFF 的项目,这些项目需要部署在 **Netlify Functions** 上。你需要基于上述的 `netlify.toml` 文件,添加以下配置:

```toml title="netlify.toml"
[build]
Expand All @@ -131,30 +134,32 @@ Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现

### Monorepo 项目

对于 Monorepo 项目,除了需要构建我们当前的项目外,还需要构建当前项目的依赖的其他仓库。我们以一个 pnpm monorepo 仓库为例,
在 Netlify 上对 Monorepo 项目进行部署。
对于 Monorepo 项目,除了需要构建当前的项目外,还需要构建当前项目依赖的仓库中其他子项目。这里以一个 pnpm Monorepo 仓库为例,在 Netlify 上对 Monorepo 项目进行部署。

假设我们有以下 Monorepo 仓库
假设 Monorepo 仓库目录结构如下

```
./
├── packages/
│ ├── app/
.
├── packages
│ ├── app
│ └── app-dep1
├── package.json
├── pnpm-lock.yaml
└── pnpm-workspace.yaml
```

我们需要在 netlify 平台上配置 Base directory 为 `packages/app`:
你需要在 Netlify 平台上配置 **Base directory**`packages/app`:

<img src="https://sf16-sg.tiktokcdn.com/obj/eden-sg/lmeh7nuptpfnuhd/netlify-monorepo-basedir.png?x-resource-account=public" />

`packages/app/package.json` 中添加以下 script,在执行 `app` 仓库的部署命令之前,先执行 workspace 中其他仓库的构建:

```json
"scripts": {
"deploy:app": "pnpm --filter 'app^...' run build && pnpm --filter=app run deploy",
{
"scripts": {
"build:packages": "pnpm --filter 'app^...' run build",
"deploy": "pnpm run build:packages && modern deploy",
}
}
```

Expand All @@ -163,7 +168,7 @@ Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现
```toml
[build]
publish = "dist"
command = "npm run deploy:app"
command = "npm run deploy"

[functions]
directory = ".netlify/functions"
Expand All @@ -174,4 +179,3 @@ Netlify 是一个流行的 web 开发平台,专为构建、发布和维护现
提交你的代码,使用 Netlify 平台部署即可。



0 comments on commit c52f58c

Please sign in to comment.