> For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt.

# Performance

`performance` 用于配置 Rspack 的性能提示机制，帮助你在构建阶段识别体积过大的输出结果。

它会根据资源（asset）体积和入口（entrypoint）总体积与阈值的比较结果，决定是否输出提示，并由 `hints` 决定提示级别（warning/error/关闭）。

你可以通过 `maxAssetSize`、`maxEntrypointSize` 设置预算阈值，并通过 `assetFilter` 排除不需要纳入统计的文件，从而让提示更符合项目的真实性能目标。

## performance


- 类型： `false | object`


默认情况下，性能提示仅在 production 模式下为浏览器目标启用。

例如，未配置 `performance` 时：

| `mode`                     | `target`                | 默认的 `performance`      |
| -------------------------- | ----------------------- | ---------------------- |
| `'production'`             | `'web'` 或 `'webworker'` | `{ hints: 'warning' }` |
| `'production'`             | `'node'`                | `false`                |
| `'development'` 或 `'none'` | `'web'`                 | `false`                |

如需在 Node.js 的 production 构建中启用性能提示，可设置 `performance: {}`。如需在 development 或 none 模式下启用，还需要将 `performance.hints` 设为 `'warning'` 或 `'error'`。

将 `performance` 设为 `false` 可禁用性能提示功能：

```js title="rspack.config.mjs"
export default {
  performance: false,
};
```

## performance.assetFilter


- 类型： `(assetFilename: string) => boolean`


用于筛选哪些资源会参与性能提示计算。返回 `true` 的文件会被纳入计算，返回 `false` 的文件会被忽略。

默认情况下，Rspack 会排除被标记为开发资源的产物，例如 source map。自定义的 `assetFilter` 会替换默认筛选规则，并同时应用于单个资源体积和入口总体积的计算。

例如在性能提示计算时忽略 CSS 文件：

```js title="rspack.config.mjs"
export default {
  performance: {
    assetFilter: (assetFilename) => !assetFilename.endsWith('.css'),
  },
};
```

## performance.hints


- 类型： `false | 'error' | 'warning'`
- 默认值：[production 模式](/config/mode#production) 为`warning`, [development 模式](/config/mode#development) 为`false`


在 none 模式下，`hints` 也默认为 `false`。这些默认值在 `performance` 为对象时生效。当 `performance` 为 `false` 时，无论使用哪种模式，性能提示都会被禁用。

控制是否启用性能提示，以及提示级别：

- `false`：关闭性能提示
- `'warning'`：以警告形式提示
- `'error'`：以错误形式提示，会使构建失败

例如将性能提示视为构建错误：

```js title="rspack.config.mjs"
export default {
  performance: {
    hints: process.env.NODE_ENV === 'production' ? 'error' : false,
  },
};
```

## performance.maxAssetSize

- **类型：**: `number`
- **默认值：**: `307200` (`300 KiB`)

设置单个资源体积阈值（单位：bytes）。当某个资源超过该值时，Rspack 会触发性能提示。

降低单个资源体积上限，以更早发现体积过大的文件：

```js title="rspack.config.mjs"
export default {
  performance: {
    maxAssetSize: 100000,
  },
};
```

## performance.maxEntrypointSize

- **类型：**: `number`
- **默认值：**: `512000` (`500 KiB`)

设置入口总体积阈值（单位：bytes）。这里的入口总体积指初始加载该入口时需要的所有资源总体积，当入口总体积超过该值时，Rspack 会触发性能提示。

例如设置入口总体积阈值为 500 KB：

```js title="rspack.config.mjs"
export default {
  performance: {
    maxEntrypointSize: 500000,
  },
};
```


本页改编自 [webpack 文档](https://webpack.docschina.org/configuration/performance/)，遵循 [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)，且已作修改。

