> 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.bundle

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

指定是否打包库，当 `bundle` 设置为 `true` 时称为 bundle 模式，设置为 `false` 时称为 bundleless 模式。更多详情请参考 [bundle / bundleless](/zh/guide/basic/output-structure.md#bundle--bundleless)。

## 设置入口

我们需要为构建指定入口文件。

### bundle: true

当 `bundle` 设置为 `true` 时，entry 需要设置为入口文件。Bundle 模式下的默认入口为 `src/index.(ts|js|tsx|jsx|mjs|cjs)`。你需要确保入口文件存在，或通过 [source.entry](https://rsbuild.rs/zh/config/source/entry) 配置自定义入口。

**例子：**

```ts title="rslib.config.ts"
export default {
  lib: [
    {
      format: 'cjs',
      bundle: true,
    },
  ],
  source: {
    entry: {
      index: './foo/index.ts',
    },
  },
};
```

### bundle: false

当 `bundle` 设置为 `false` 时，入口需要设置为 [glob 模式](https://github.com/micromatch/picomatch#globbing-features) 以包含所有文件。Bundleless 模式下的默认入口为 `src/**`。

**例子：**

```ts title="rslib.config.ts"
export default {
  lib: [
    {
      format: 'cjs',
      bundle: false,
    },
  ],
  source: {
    entry: {
      index: './foo/**',
    },
  },
};
```

你也可以使用感叹号来排除一些文件。例如，排除 `src` 文件夹中的测试文件：

```ts title="rslib.config.ts"
export default {
  lib: [
    {
      format: 'cjs',
      bundle: false,
    },
  ],
  source: {
    entry: {
      index: ['./src/**', '!src/**/*.test.ts'],
    },
  },
};
```

::: note

当开启 [类型生成](/zh/config/lib/dts.md) 时，记得在 `tsconfig.json` 中设置 [exclude](https://www.typescriptlang.org/tsconfig/#exclude) 字段，以避免相应的文件生成 TypeScript 类型声明文件。

例如，排除 `src` 文件夹中的测试文件：

```json
// tsconfig.json
{
  "include": ["src"],
  "exclude": ["src/**/*.test.ts"]
}
```

:::

## 示例

对于以下的源代码文件结构：

```
.
├── src
│   ├── index.ts
│   ├── foo.ts
│   └── bar.ts
└── package.json
```

### bundle: true

```ts title="rslib.config.ts"
export default defineConfig({
  lib: [
    {
      format: 'cjs',
      bundle: true,
    },
  ],
});
```

当 `bundle` 设置为 `true` 时，也称为 bundle 模式，Rslib 会将库打包成单个文件。

```diff
  .
+ ├── dist
+ │   └── index.js
  ├── src
  │   ├── index.ts
  │   ├── foo.ts
  │   └── bar.ts
  └── package.json
```

### bundle: false

```ts title="rslib.config.ts"
export default defineConfig({
  lib: [
    {
      format: 'cjs',
      bundle: false,
    },
  ],
  source: {
    entry: {
      index: ['./src/**'],
    },
  },
});
```

当 `bundle` 设置为 `false` 时，也称为 bundleless 模式，Rslib 会将代码转换为多个文件。

```diff
  .
+ ├── dist
+ │   ├── index.js
+ │   ├── foo.js
+ │   └── bar.js
  ├── src
  │   ├── index.ts
  │   ├── foo.ts
  │   └── bar.ts
  └── package.json
```
