For AI agents: the complete documentation index is available at https://rslib.rs/zh/llms.txt, the full documentation bundle is available at https://rslib.rs/zh/llms-full.txt, and this page is available as Markdown at https://rslib.rs/zh/config/other/exports.md.
close
  • 简体中文
  • exports

    • 类型:
    type Exports =
      | boolean
      | {
          include?: string[];
          exclude?: string[];
          source?: boolean | string;
          custom?: object | Function | Array<object | Function>;
        };
    • 默认值: false
    • 命令行: --exports / --no-exports
    • 顶层配置: 仅支持顶层

    在构建完成后根据产物生成 package.json 中的 exports 字段。

    rslib.config.ts
    export default defineConfig({
      dts: true,
      exports: true,
    });
    Note

    生成的结果会覆盖 package.json 中原有的 exports 和 publishConfig.exports 字段,如需保留手写的子路径,请通过 exports.custom 添加。

    在 watch 模式下,或使用 --lib 只构建部分 lib 时,Rslib 不会生成 exports 字段。

    生成规则

    • 仅支持导出 ESM 和 CJS 产物。
    • index 入口导出为 .,button/index 导出为 ./button,其他入口按入口名导出。
    • 如果同一入口同时有 ESM 和 CJS 产物,会使用 import 和 require 条件区分;如果只有一种格式,则直接使用产物路径。
    • 当开启 dts 时,如果 TypeScript 无法根据产物路径找到类型声明文件,会添加 types 条件。
    • bundle 模式下,入口生成的 CSS 文件也会被导出。

    以下示例展示了一个库同时构建出 ESM 和 CJS 产物的生成结果:

    package.json
    {
      "exports": {
        ".": {
          "import": "./dist/index.js",
          "require": "./dist/index.cjs"
        }
      }
    }

    exports.include

    • 类型: string[]
    • 默认值: bundle 模式下导出所有入口,bundleless 模式下只导出 index

    用于选择要导出的产物。配置的 glob 表达式会与产物相对于输出目录的路径进行匹配,支持省略扩展名。

    例如,在 bundleless 模式下导出根目录和各组件目录下的 index:

    rslib.config.ts
    export default defineConfig({
      bundle: false,
      exports: {
        include: ['index', '*/index'],
      },
    });

    exports.exclude

    • 类型: string[]
    • 默认值: []

    用于从 include 匹配到的产物中排除一部分,写法与 include 相同。

    例如,在 bundleless 模式下导出各目录下的 index,但排除 internal 目录:

    rslib.config.ts
    export default defineConfig({
      bundle: false,
      exports: {
        include: ['**/index'],
        exclude: ['internal/**'],
      },
    });

    exports.source

    • 类型: boolean | string
    • 默认值: false

    用于导出入口对应的源文件,以便在开发时直接引用源码。

    • 设置为 true 时,exports 会指向源文件,指向构建产物的导出则写入 publishConfig.exports。
    • 设置为字符串时,会在每个 JS 子路径中添加一个以该字符串命名、指向源文件的条件,可以配合 TypeScript 的 customConditions 或 @rsbuild/plugin-source-build 使用。

    例如,source: '@scope/source' 会生成:

    package.json
    {
      "exports": {
        ".": {
          "@scope/source": "./src/index.ts",
          "default": "./dist/index.js"
        }
      }
    }

    exports.custom

    • 类型:
    type ExportsValue =
      string | null | ExportsValue[] | { [condition: string]: ExportsValue };
    
    type ExportsField = Record<string, ExportsValue>;
    
    type ExportsCustomItem =
      | ExportsField
      | ((
          exports: ExportsField,
          context: { pkg: Record<string, unknown>; isPublishConfig: boolean },
        ) => ExportsField | Promise<ExportsField>);
    
    type ExportsCustom = ExportsCustomItem | ExportsCustomItem[];
    • 默认值: undefined

    用于自定义生成的 exports。

    • 传入对象时,其中的子路径会被添加到生成结果中,同名的子路径会被替换。
    • 传入函数时,函数接收生成的 exports,并返回最终结果。当 source 为 true 时,exports 和 publishConfig.exports 会分别经过该函数处理,可以通过第二个参数中的 isPublishConfig 区分。
    • 传入数组时,会按顺序依次应用其中的每一项。

    例如,追加 ./package.json 子路径:

    rslib.config.ts
    export default defineConfig({
      exports: {
        custom: {
          './package.json': './package.json',
        },
      },
    });

    也可以通过函数修改生成的子路径,例如为 . 添加 browser 条件:

    rslib.config.ts
    export default defineConfig({
      exports: {
        custom: (exports) => ({
          ...exports,
          '.': { browser: './dist/browser.js', default: exports['.'] },
        }),
      },
    });

    版本历史

    版本变更
    v1.1.0新增 exports 配置。