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

    与输入的源代码相关的选项。

    source.decorators source.decoratorssource.decorators

    • 类型: { version?: 'legacy' | '2022-03' | '2023-11' }
    • 默认值: { version: '2023-11' }

    配置装饰器语法版本。

    如果项目使用了 TypeScript 的 experimentalDecorators,请将 source.decorators.version 设为 'legacy'。

    source.assetsInclude source.assetsIncludesource.assetsInclude

    • 类型: Rspack.RuleSetCondition
    • 默认值: undefined

    指定额外按静态资源处理的文件类型(在 Rsbuild 默认资源规则之外,例如 .txt 或 .md)。

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      source: {
        assetsInclude: /\.(txt|md)$/,
      },
    });

    如果已通过 @rstest/adapter-rsbuild 或 @rstest/adapter-rslib 复用 Rsbuild / Rslib 配置,source.assetsInclude 会自动继承。

    source.define source.definesource.define

    • 类型: Rspack.DefinePluginOptions
    • 默认值: {}(默认会自动将 import.meta.env 映射到 process.env)

    在编译阶段将代码中的全局标识符或表达式替换为指定值,可用于注入构建时环境变量或特性开关。

    source.exclude source.excludesource.exclude

    • 类型: Rspack.RuleSetCondition[]
    • 默认值: undefined

    排除不需要经过 SWC 转译的 JavaScript 或 TypeScript 文件。

    Note

    source.exclude 匹配的文件不会经过 SWC 转译,但被引用的文件仍会被打包进产物。

    如果不希望将某些文件打包进产物,请使用 output.externals 或 Rspack 的 IgnorePlugin。

    source.include source.includesource.include

    • 类型: Rspack.RuleSetCondition[]
    • 默认值: undefined

    指定额外需要由 SWC 编译的 JavaScript 或 TypeScript 文件(例如特定的 node_modules 依赖包)。

    source.transformImport source.transformImportsource.transformImport

    • 类型:
    type TransformImport = {
      libraryName: string;
      libraryDirectory?: string;
      customName?: string;
      customStyleName?: string;
      style?: string | boolean;
      styleLibraryDirectory?: string;
      camelToDashComponentName?: boolean;
      transformToDefaultImport?: boolean;
      ignoreEsComponent?: string[];
      ignoreStyleComponent?: string[];
    };
    
    type TransformImportFn = (
      imports: TransformImport[],
    ) => TransformImport[] | void;
    
    type TransformImportConfig =
      TransformImportFn | (TransformImport | TransformImportFn)[];
    • 默认值: undefined

    配置按需导入转换规则(例如将组件库的聚合导入改写为子路径导入)。

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      source: {
        transformImport: [
          {
            libraryName: 'demo-lib',
            libraryDirectory: '.',
            camelToDashComponentName: false,
          },
        ],
      },
    });
    Caution

    source.transformImport 会在编译阶段改写最终的模块请求。

    仍使用原始 specifier 的模块级 mock API(rs.mock('pkg')、rs.doMock('pkg')、rs.importActual('pkg')、rs.requireActual('pkg'))可能无法命中被改写后的模块请求。

    source.tsconfigPath source.tsconfigPathsource.tsconfigPath

    • 类型: string
    • 默认值: 'tsconfig.json'(存在时在各项目根目录自动探测)
    • CLI: --source.tsconfigPath <path>

    指定自定义 tsconfig.json 文件路径(相对于项目根目录或绝对路径)。

    未设置时,Rstest 会在每个项目的根目录探测 tsconfig.json,并读取其中的 compilerOptions.paths 用于模块解析。

    Rstest 不会自动将其他 compilerOptions 转换为代码转换设置。需要时请显式配置对应的 Rstest 构建选项——例如用 source.decorators 配置装饰器语法(如 version: 'legacy'),用 tools.swc 配置 SWC 输出 target 等——或通过 adapter 自动映射。