close
  • 简体中文
  • lib.bundle

    • 类型: boolean
    • 默认值: true
    • 命令行: --bundle / --no-bundle

    指定是否打包库,当 bundle 设置为 true 时称为 bundle 模式,设置为 false 时称为 bundleless 模式。更多详情请参考 bundle / bundleless

    设置入口

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

    bundle: true

    bundle 设置为 true 时,entry 需要设置为入口文件。Bundle 模式下的默认入口为 src/index.(ts|js|tsx|jsx|mjs|cjs)。你需要确保入口文件存在,或通过 source.entry 配置自定义入口。

    例子:

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

    bundle: false

    bundle 设置为 false 时,入口需要设置为 glob 模式 以包含所有文件。Bundleless 模式下的默认入口为 src/**

    例子:

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

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

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

    当开启 类型生成 时,记得在 tsconfig.json 中设置 exclude 字段,以避免相应的文件生成 TypeScript 类型声明文件。

    例如,排除 src 文件夹中的测试文件:

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

    示例

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

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

    bundle: true

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

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

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

    bundle: false

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

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

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