@modern-js/main-doc 0.0.0-nightly-20241031170707 → 0.0.0-nightly-20241101090401
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.
|
@@ -27,8 +27,7 @@ MODERNJS_DEPLOY=netlify npx modern deploy
|
|
|
27
27
|
When deploying on the deployment platforms officially supported by Modern.js, there is no need to specify environment variables.
|
|
28
28
|
:::
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
## Node.js
|
|
30
|
+
## Using ModernJS built-in Node.js Server
|
|
32
31
|
|
|
33
32
|
### Single Repo
|
|
34
33
|
|
|
@@ -284,3 +283,106 @@ Add the following content to the `packages/app/vercel.json` file:
|
|
|
284
283
|
```
|
|
285
284
|
|
|
286
285
|
Just submit your code and deploy it using the Vercel platform.
|
|
286
|
+
|
|
287
|
+
## Using Self-Built Node.js Server
|
|
288
|
+
|
|
289
|
+
Typically, we recommend using the built-in Node.js server of Modern.js to deploy applications. It supports hosting both pure frontend and full-stack projects, ensuring consistent performance in both development and production environments.
|
|
290
|
+
|
|
291
|
+
If your project is purely frontend, you can also deploy the application to the self-built Node.js server. Below is an example of using a Koa server to host the output of a pure frontend project.
|
|
292
|
+
|
|
293
|
+
For instance, if you have a Node.js server repository, you can copy the output of the project into this repository. The structure would look like this:
|
|
294
|
+
|
|
295
|
+
```bash
|
|
296
|
+
.
|
|
297
|
+
├── .output
|
|
298
|
+
│ ├── html
|
|
299
|
+
│ └── static
|
|
300
|
+
└── server.js
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
In `server.js`, assume you have the following code:
|
|
304
|
+
|
|
305
|
+
```ts title="server.js"
|
|
306
|
+
import Koa from 'koa';
|
|
307
|
+
|
|
308
|
+
const app = new Koa();
|
|
309
|
+
app.use(async (ctx, next) => {
|
|
310
|
+
ctx.body = 'Hello Modern.js';
|
|
311
|
+
});
|
|
312
|
+
app.listen(3000);
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
Now, you can add some code to include the logic for accessing static resources and HTML files in `server.js`. We need to use the `mime-types` package to get the MIME types of static resources, so let's install the dependency first:
|
|
316
|
+
|
|
317
|
+
import { PackageManagerTabs } from '@theme';
|
|
318
|
+
|
|
319
|
+
<PackageManagerTabs command="add mime-types" />
|
|
320
|
+
|
|
321
|
+
```ts title="server.js"
|
|
322
|
+
const Koa = require('koa');
|
|
323
|
+
const fs = require('fs');
|
|
324
|
+
const path = require('path');
|
|
325
|
+
const mime = require('mime-types');
|
|
326
|
+
|
|
327
|
+
const app = new Koa();
|
|
328
|
+
app.use(async (ctx, next) => {
|
|
329
|
+
if (ctx.path.startsWith('/static')) {
|
|
330
|
+
ctx.type = mime.lookup(ctx.path);
|
|
331
|
+
ctx.body = fs.createReadStream(path.resolve(__dirname, `.output${ctx.path}`));
|
|
332
|
+
} else if (ctx.path === '/') {
|
|
333
|
+
ctx.type = 'html';
|
|
334
|
+
ctx.body = fs.createReadStream(path.resolve(__dirname, '.output/html/main/index.html'));
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
app.listen(3000);
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
:::note
|
|
341
|
+
The above code is a basic example. Your application might have multiple entry points and require different HTML files for different paths. A custom Node.js server would also involve more complex logic.
|
|
342
|
+
|
|
343
|
+
Please note that if your project uses Modern.js conventional routing or if you have set up client-side routing with React Router, you must access HTML files through the correct `baseURL`.
|
|
344
|
+
|
|
345
|
+
In Modern.js, the default `baseURL` is `'/'`. You can configure it by modifying [`server.baseUrl`](/configure/app/server/base-url) in `modern.config.ts`.
|
|
346
|
+
|
|
347
|
+
:::danger
|
|
348
|
+
For projects with client-side routing, you can never access HTML files through the `/index.html` path.
|
|
349
|
+
:::
|
|
350
|
+
|
|
351
|
+
## Nignx
|
|
352
|
+
|
|
353
|
+
Nginx is a high-performance HTTP and reverse proxy server that can handle static files, reverse proxy, load balancing, and other functions. Deploying on Nginx typically requires configuring the `nginx.conf` file.
|
|
354
|
+
|
|
355
|
+
If your project is a purely front-end project, you can also deploy the application through Nginx. Here is an example of an Nginx configuration to demonstrate how to host the output of a purely front-end project.
|
|
356
|
+
|
|
357
|
+
```conf title="nginx.conf"
|
|
358
|
+
# user [user] [group];
|
|
359
|
+
worker_processes 1;
|
|
360
|
+
|
|
361
|
+
events {
|
|
362
|
+
worker_connections 1024;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
http {
|
|
366
|
+
include mime.types;
|
|
367
|
+
default_type application/octet-stream;
|
|
368
|
+
|
|
369
|
+
server {
|
|
370
|
+
listen 8080;
|
|
371
|
+
server_name localhost;
|
|
372
|
+
|
|
373
|
+
location / {
|
|
374
|
+
# root [projectPath]/.output/html/main;
|
|
375
|
+
index index.html;
|
|
376
|
+
try_files $uri $uri/ =404;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
location /static {
|
|
380
|
+
# alias [projectPath]/.output/static;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
In the above configuration, you need to replace `[projectPath]` with your project path and `[user]` and `[group]` with your current user and user group.
|
|
387
|
+
|
|
388
|
+
You can copy the above configuration into the `nginx.conf` file in the Nginx installation directory and then start the Nginx service. You can also start the configuration file in a specified path using `nginx -c`, in which case you need to ensure that the path configured in the `include` directive is correct.
|
|
@@ -13,11 +13,10 @@ sidebar_position: 15
|
|
|
13
13
|
目前 Modern.js 仅支持在 Node.js 环境中运行,未来将提供更多运行时环境的支持。
|
|
14
14
|
:::
|
|
15
15
|
|
|
16
|
-
|
|
17
16
|
## 构建部署产物
|
|
18
17
|
|
|
19
18
|
执行 `modern deploy` 命令将自动输出部署产物。此过程包括优化 Bundler 构建产物及产物依赖,检测当前部署平台,并自动生成可以在该平台运行的产物。
|
|
20
|
-
|
|
19
|
+
如果你希望在本地生成并测试特定部署平台的产物,可以通过设置环境变量来指定平台:
|
|
21
20
|
|
|
22
21
|
```bash
|
|
23
22
|
MODERNJS_DEPLOY=netlify npx modern deploy
|
|
@@ -27,8 +26,7 @@ MODERNJS_DEPLOY=netlify npx modern deploy
|
|
|
27
26
|
在 Modern.js 官方支持的部署平台中部署时,无需指定环境变量。
|
|
28
27
|
:::
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
## Node.js
|
|
29
|
+
## ModernJS 内置 Node.js 服务器
|
|
32
30
|
|
|
33
31
|
### 单仓库项目
|
|
34
32
|
|
|
@@ -223,7 +221,6 @@ Vercel 是一个面向现代 Web 应用的部署平台,它提供了丰富的
|
|
|
223
221
|
}
|
|
224
222
|
```
|
|
225
223
|
|
|
226
|
-
|
|
227
224
|
### Monorepo 项目
|
|
228
225
|
|
|
229
226
|
:::info
|
|
@@ -277,5 +274,106 @@ Vercel 是一个面向现代 Web 应用的部署平台,它提供了丰富的
|
|
|
277
274
|
提交你的代码,使用 Vercel 平台部署即可。
|
|
278
275
|
|
|
279
276
|
|
|
277
|
+
## 自建 Node.js 服务器
|
|
278
|
+
|
|
279
|
+
通常情况下,我们推荐使用 Modern.js 内置的 Node.js 服务器来部署应用,它支持托管纯前端项目或者全栈项目,并且保证在开发和生产环境下的表现一致。
|
|
280
|
+
|
|
281
|
+
如果你的项目是纯前端项目,也可以通过自建 Node.js 服务器来部署应用,以下用一个 Koa 服务器的示例来演示如何托管一个纯前端项目的产物。
|
|
282
|
+
|
|
283
|
+
例如你有一个 Node.js 服务器的仓库,你可以将项目的产物复制到该仓库下,现在结构如下:
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
.
|
|
287
|
+
├── .output
|
|
288
|
+
│ ├── html
|
|
289
|
+
│ └── static
|
|
290
|
+
└── server.js
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
在 `server.js` 中,假定有如下代码:
|
|
294
|
+
|
|
295
|
+
```ts title="server.js"
|
|
296
|
+
import Koa from 'koa';
|
|
297
|
+
|
|
298
|
+
const app = new Koa();
|
|
299
|
+
app.use(async (ctx, next) => {
|
|
300
|
+
ctx.body = 'Hello Modern.js';
|
|
301
|
+
});
|
|
302
|
+
app.listen(3000);
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
现在,你可以新增部分代码,将静态资源和 HTML 文件的访问逻辑添加到 `server.js` 中。这里需要通过 `mime-types` 包来获取静态资源的 MIME 类型,因此我们先安装依赖:
|
|
306
|
+
|
|
307
|
+
import { PackageManagerTabs } from '@theme';
|
|
308
|
+
|
|
309
|
+
<PackageManagerTabs command="add mime-types" />
|
|
310
|
+
|
|
311
|
+
```ts title="server.js"
|
|
312
|
+
const Koa = require('koa');
|
|
313
|
+
const fs = require('fs');
|
|
314
|
+
const path = require('path');
|
|
315
|
+
const mime = require('mime-types');
|
|
316
|
+
|
|
317
|
+
const app = new Koa();
|
|
318
|
+
app.use(async (ctx, next) => {
|
|
319
|
+
if (ctx.path.startsWith('/static')) {
|
|
320
|
+
ctx.type = mime.lookup(ctx.path);
|
|
321
|
+
ctx.body = fs.createReadStream(path.resolve(__dirname, `.output${ctx.path}`));
|
|
322
|
+
} else if (ctx.path === '/') {
|
|
323
|
+
ctx.type = 'html';
|
|
324
|
+
ctx.body = fs.createReadStream(path.resolve(__dirname, '.output/html/main/index.html'));
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
app.listen(3000);
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
:::note
|
|
331
|
+
以上代码是最基础的例子,你的应用可能是多入口的,需要根据不同的路径访问不同的 HTML 文件,自建 Node.js 服务器也会存在更多的逻辑。
|
|
332
|
+
:::
|
|
333
|
+
|
|
334
|
+
需要注意的是,如果你的项目中使用 Modern.js 约定式路由,或是使用 React Router 自行搭建了浏览器端路由,你必须通过正确的 `baseURL` 来访问 HTML 文件。
|
|
335
|
+
|
|
336
|
+
在 Modern.js 中,默认的 `baseURL` 是 `'/'`,你可以通过在 `modern.config.ts` 中修改 [`server.baseUrl`](/configure/app/server/base-url) 来配置。
|
|
337
|
+
|
|
338
|
+
:::danger
|
|
339
|
+
存在浏览器路由的项目,永远无法通过 `/index.html` 路径来访问到 HTML 文件。
|
|
340
|
+
:::
|
|
341
|
+
|
|
342
|
+
## Nignx
|
|
343
|
+
|
|
344
|
+
Nginx 是一个高性能的 HTTP 和反向代理服务器,它可以处理静态文件、反向代理、负载均衡等功能。在 Nginx 上部署,通常需要配置 `nginx.conf` 文件。
|
|
345
|
+
|
|
346
|
+
如果你的项目是纯前端项目,也可以通过 Nginx 来部署应用,以下提供一个 Nginx 配置的示例来演示如何托管一个纯前端项目的产物。
|
|
347
|
+
|
|
348
|
+
```conf title="nginx.conf"
|
|
349
|
+
# user [user] [group];
|
|
350
|
+
worker_processes 1;
|
|
351
|
+
|
|
352
|
+
events {
|
|
353
|
+
worker_connections 1024;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
http {
|
|
357
|
+
include mime.types;
|
|
358
|
+
default_type application/octet-stream;
|
|
359
|
+
|
|
360
|
+
server {
|
|
361
|
+
listen 8080;
|
|
362
|
+
server_name localhost;
|
|
363
|
+
|
|
364
|
+
location / {
|
|
365
|
+
# root [projectPath]/.output/html/main;
|
|
366
|
+
index index.html;
|
|
367
|
+
try_files $uri $uri/ =404;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
location /static {
|
|
371
|
+
# alias [projectPath]/.output/static;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
```
|
|
280
376
|
|
|
377
|
+
在上述配置中,你需要将 `[projectPath]` 替换为你的项目路径,将 `[user]` 和 `[group]` 替换为你当前的用户和用户组。
|
|
281
378
|
|
|
379
|
+
你可以将上述配置复制到 Nginx 安装目录的 `nginx.conf` 文件中,然后启动 Nginx 服务。你也可以通过 `nginx -c` 启动指定路径下的配置文件,此时你需要额外保证 `include` 指令配置的路径正确。
|
package/package.json
CHANGED
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "0.0.0-nightly-
|
|
18
|
+
"version": "0.0.0-nightly-20241101090401",
|
|
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": "0.0.0-nightly-
|
|
25
|
+
"@modern-js/sandpack-react": "0.0.0-nightly-20241101090401"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@rspress/shared": "1.35.2",
|