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

# lib.autoExtension

- **类型：** `boolean`
- **默认值：** `true`
- **命令行：** `--auto-extension` / `--no-auto-extension`
- **顶层配置：** 支持

是否根据 [`format`](/zh/config/lib/format.md) 配置项自动设置 JavaScript 输出文件的扩展名。

## 默认扩展名

默认情况下，当 `autoExtension` 设置为 `true` 时，文件扩展名将会是：

- 当 `package.json` 中设置 `type: module` 时，`esm` 格式使用 `.js`，`cjs` 格式使用 `.cjs`。

- 当 `package.json` 中设置 `type: commonjs` 或没有 `type` 字段时，`cjs` 格式使用 `.js`，`esm` 格式使用 `.mjs`。

当 `autoExtension` 设置为 `false` 时，文件扩展名将默认为 `.js`。

## 自定义扩展名

你可以将 `autoExtension` 设置为 `false`，并使用 [output.filename](https://rsbuild.rs/zh/config/output/filename) 来自定义 JavaScript 输出文件。

```ts title="rslib.config.ts"
export default defineConfig({
  lib: [
    {
      format: 'cjs',
      autoExtension: false,
      output: {
        filename: {
          js: '[name].cjs',
        },
      },
    },
    {
      format: 'esm',
      output: {
        filename: {
          js: '[name].mjs',
        },
      },
    },
  ],
});
```
