Skip to content

Commit

Permalink
feat: add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Aug 26, 2023
1 parent 0d6d59b commit b7bbce8
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
51 changes: 50 additions & 1 deletion packages/document/builder-doc/docs/en/api/builder-instance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ type StartDevServerOptions = {
compiler?: Compiler | MultiCompiler;
// passing through the build-independent dev server configuration
serverOptions?: Partial<ModernDevServerOptions>;
// Whether to get the port silently, the default is false
getPortSliently?: boolean;
// custom logger
logger?: Logger;
};

type StartServerResult = {
Expand Down Expand Up @@ -293,6 +297,52 @@ await builder.startDevServer({
});
```

### Get Port Silently

In some cases, the default startup port number is already occupied. In this situation, Builder will automatically increment the port number until it finds an available one. This process will output a prompt log. If you do not want this log, you can set `getPortSliently` to `true`.

```ts
await builder.startDevServer({
getPortSliently: true,
});
```

### Custom Logger

By default, Builder uses `@modern-js/utils/logger` to output logs. You can customize the log output object through the `logger` parameter.

```ts
const customLogger = {
// You need to define the following methods
info(msg: string) {
console.log(msg);
},
error(msg: string) {
console.error(msg);
},
warn(msg: string) {
console.warn(msg);
},
success(msg: string) {
console.log(`✅ msg`);
},
debug(msg: string) {
if (process.env.DEBUG) {
console.log(msg);
}
},
log(msg: string) {
console.log(msg);
};
}

await builder.startDevServer({
logger: customLogger,
});
```

Then Builder will use the custom logger to output logs.

## builder.serve

Start a server to preview the production build locally. This method should be executed after `builder.build`.
Expand Down Expand Up @@ -332,7 +382,6 @@ console.log(port); // 8080
await server.close();
```


## builder.createCompiler

Create a Compiler object.
Expand Down
52 changes: 50 additions & 2 deletions packages/document/builder-doc/docs/zh/api/builder-instance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ type StartDevServerOptions = {
compiler?: Compiler | MultiCompiler;
// 透传与构建无关的 dev server 配置
serverOptions?: Partial<ModernDevServerOptions>;
// 是否在启动时静默获取端口号,默认为 false
getPortSliently?: boolean;
// 自定义日志输出对象
logger?: Logger;
};

type StartServerResult = {
Expand Down Expand Up @@ -293,6 +297,52 @@ await builder.startDevServer({
});
```

### 静默获取端口号

某些情况下,默认启动的端口号已经被占用,此时 Builder 会自动递增端口号,直至找到一个可用端口。这个过程会输出提示日志,如果你不希望这段日志,可以将 `getPortSliently` 设置为 `true`

```ts
await builder.startDevServer({
getPortSliently: true,
});
```

### 自定义日志输出对象

默认情况下,Builder 会使用 `@modern-js/utils/logger` 来输出日志,你可以通过 `logger` 参数来自定义日志输出对象。

```ts
const customLogger = {
// 你需要定义以下的方法
info(msg: string) {
console.log(msg);
},
error(msg: string) {
console.error(msg);
},
warn(msg: string) {
console.warn(msg);
},
success(msg: string) {
console.log(`✅ msg`);
},
debug(msg: string) {
if (process.env.DEBUG) {
console.log(msg);
}
},
log(msg: string) {
console.log(msg);
};
}

await builder.startDevServer({
logger: customLogger,
});
```

这样,Builder 会使用你自定义的日志输出对象来输出日志。

## builder.serve

在本地启动 Server 来预览生产环境构建的产物,需要在 `builder.build` 方法之后执行。
Expand All @@ -317,7 +367,6 @@ function server(): Promise<StartServerResult>;
await builder.serve();
```


`serve` 会返回以下参数:

- `urls`:访问 Server 的 URLs
Expand All @@ -333,7 +382,6 @@ console.log(port); // 8080
await server.close();
```


## builder.createCompiler

创建一个 compiler 对象。
Expand Down

0 comments on commit b7bbce8

Please sign in to comment.