Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: comparing Rspack dev server #1056

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- **Type:** `boolean | ConnectHistoryApiFallbackOptions`
- **Default:** `false`

When Rsbuild's default [page route](/guide/basic/page-route) logic cannot meet your needs, for example, if you want to be able to access `main.html` when accessing `/`, you can set it up using `server.historyApiFallback`.
When Rsbuild's default [page routing](/guide/basic/server#page-routing) behavior cannot meet your needs, for example, if you want to be able to access `main.html` when accessing `/`, you can set it up using `server.historyApiFallback`.

### Example

Expand Down
2 changes: 1 addition & 1 deletion packages/document/docs/en/guide/basic/_meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
"cli",
"config",
"page-route",
"server",
"output-files",
"css-usage",
"static-assets",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Page Route
# Server

This chapter will introduces how to access pages during local development and preview.
Rsbuild comes with a built-in dev server designed to improve the development experience. When you run the `rsbuild dev` or `rsbuild preview` commands, the server will start, providing features such as page preview, routing, and hot module reloading.

## Default
## Page Routing

Rsbuild Server offers a set of default routing convention, and allows users to customize it through configurations.

### Default Behavior

Rsbuild Server will generate the corresponding page route based on the [source.entry](/config/source/entry) configuration.

Expand All @@ -19,7 +23,7 @@ export default {
};
```

## Default fallback logic
### Fallback Behavior

By default, when the request meets the following conditions and the corresponding resource is not found, it will fallback to `index.html`:

Expand All @@ -34,9 +38,9 @@ export default {
};
```

### Custom fallback logic
### Custom Fallback Behavior

When Rsbuild's default [server.htmlFallback](/config/server/html-fallback) configuration cannot meet your needs, for example, if you want to be able to access `main.html` when accessing `/`, you can set it up using [server.historyApiFallback] (/config/server/history-api-fallback).
When Rsbuild's default [server.htmlFallback](/config/server/html-fallback) configuration cannot meet your needs, for example, if you want to be able to access `main.html` when accessing `/`, you can set it up using [server.historyApiFallback](/config/server/history-api-fallback).

```ts title=rsbuild.config.ts
export default {
Expand All @@ -54,15 +58,15 @@ export default {
};
```

## HTML file output path and Route
### HTML Output Path

Normally, `/` points to the dist root directory, and the HTML file is output to the dist root directory. At this time, the corresponding HTML page can be accessed through `/xxx`.

If you output HTML files to other subdirectories by modifying [output.distPath.html](/config/output/dist-path), you need to access the corresponding HTML page through `/htmlPath/xxx`.

For example, if you set the HTML file to be output to the `HTML` directory, index.html will be accessed through `/html/`, and foo.html will be accessed through `/html/foo`.

```
```ts
export default {
source: {
entry: {
Expand All @@ -77,3 +81,15 @@ export default {
},
};
```

## Comparing Rspack Dev Server

Rsbuild dev server and the dev server of Rspack CLI (`@rspack/dev-server`) share some similarities and differences:

- Both are based on [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware).
- They have different configs, with Rsbuild offering a richer set of configurations.
- The log format of Rspack CLI aligns with Webpack CLI, while the logs of Rsbuild are clearer and more readable.

:::tip
Rsbuild applications cannot use Rspack's [devServer](https://rspack.dev/config/dev-server) config. You can configure the behavior of the server through Rsbuild's `dev` and `server` configs, see [Config Overview](/config/index) for details.
:::
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- **类型:** `boolean | ConnectHistoryApiFallbackOptions`
- **默认值:** `false`

当 Rsbuild 默认的 [页面路由](/guide/basic/page-route) 逻辑无法满足你的需求时,例如,希望在访问 `/` 时可以访问 `main.html`,可通过 `server.historyApiFallback` 进行设置。
当 Rsbuild 默认的 [页面路由](/guide/basic/server#page-routing) 行为无法满足你的需求时,例如,希望在访问 `/` 时可以访问 `main.html`,可通过 `server.historyApiFallback` 进行设置。

### 示例

Expand Down
4 changes: 2 additions & 2 deletions packages/document/docs/zh/guide/basic/_meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
"config",
"page-route",
"cli",
"config",
"server",
"output-files",
"css-usage",
"static-assets",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# 页面路由
# 开发服务器

本章节介绍了如何在本地开发和预览时进行页面访问
Rsbuild 配备了一个内置的开发服务器,旨在提升开发体验。当你执行 `rsbuild dev` 或 `rsbuild preview` 命令时,该服务器将启动,并提供页面预览、路由、模块热更新等功能

## 默认情况
## 页面路由

Rsbuild 的 Server 提供一套默认的路由约定,并允许用户通过配置项定制。

### 默认行为

Rsbuild Server 会根据 [source.entry](/config/source/entry) 配置生成对应的页面路由。

Expand All @@ -19,7 +23,7 @@ export default {
};
```

## 默认 Fallback 逻辑
### Fallback 行为

当请求满足以下条件且未找到对应资源时,会被 `server.htmlFallback` 处理,默认会回退到 index.html。

Expand All @@ -34,7 +38,7 @@ export default {
};
```

### 自定义 Fallback 逻辑
### 自定义 Fallback 行为

当 Rsbuild 默认的 [server.htmlFallback](/config/server/html-fallback) 配置无法满足你的需求,例如,希望在访问 `/` 时可以访问 `main.html`,可通过 [server.historyApiFallback](/config/server/history-api-fallback) 进行设置。

Expand All @@ -54,15 +58,15 @@ export default {
};
```

## HTML 文件输出位置与路由的关系
### HTML 输出路径

通常情况下,`/` 指向 dist 产物根目录, 而 HTML 文件输出到 dist 根目录下,此时可通过 `/xxx` 访问对应的 HTML 页面。

若通过修改 [output.distPath.html](/config/output/dist-path) 将 HTML 文件输出到其他子目录下,此时需通过 `/[htmlPath]/xxx` 访问对应的 HTML 页面。

例如,设置将 HTML 文件输出到 `HTML` 目录下,此时将通过 `/html/` 访问到 index.html,通过 `/html/foo` 访问到 foo.html。

```
```ts
export default {
source: {
entry: {
Expand All @@ -77,3 +81,15 @@ export default {
},
};
```

## 对比 Rspack Dev Server

Rsbuild 的开发服务器与 Rspack CLI 的开发服务器(`@rspack/dev-server`)有一些异同:

- 两者都是基于 [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware) 实现。
- 两者的配置项不同,Rsbuild 提供了更丰富的配置项。
- Rspack CLI 日志格式会与 Webpack CLI 对齐,Rsbuild 的日志则更加清晰和易读。

:::tip
Rsbuild 应用不能使用 Rspack 的 [devServer](https://rspack.dev/config/dev-server) 配置项。你可以通过 Rsbuild 的 `dev` 和 `server` 配置 Server 的行为,详见 [Config 总览](/config/index)。
:::