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/resolve.md.
close
  • 简体中文
  • resolve

    与模块解析相关的选项。

    resolve.aliasStrategy resolve.aliasStrategyresolve.aliasStrategy

    • 类型: 'prefer-tsconfig' | 'prefer-alias'
    • 默认值: 'prefer-tsconfig'

    控制 resolve.alias 选项与 tsconfig.json 中的 paths 选项的优先级:

    • 'prefer-tsconfig':tsconfig.json 中的 paths 优先于 resolve.alias。
    • 'prefer-alias':resolve.alias 优先于 tsconfig.json 中的 paths。

    resolve.alias resolve.aliasresolve.alias

    • 类型: ConfigChain<Record<string, string | false | (string | false)[]>>
    • 默认值: {}

    设置模块路径别名以简化导入路径或重定向模块引用,用法与 Rspack 的 resolve.alias 一致。

    对于 TypeScript 项目,Rstest 会自动识别 tsconfig.json 中的 compilerOptions.paths,无需在 resolve.alias 中重复配置。

    resolve.dedupe resolve.deduperesolve.dedupe

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

    强制 Rstest 从项目根目录解析指定的包,避免产物中出现重复依赖。

    resolve.extensions resolve.extensionsresolve.extensions

    • 类型: string[]
    • 默认值: ['.ts', '.tsx', '.mjs', '.js', '.jsx', '.json', '.cjs']

    在导入模块未写明扩展名时自动解析的文件扩展名列表。在 Node 模式构建(node、jsdom 和 happy-dom)中,Rstest 会继承 Rsbuild 的默认值并追加 .cjs;Browser Mode 则保持 Rsbuild 默认的扩展名列表。

    resolve.conditionNames resolve.conditionNamesresolve.conditionNames

    • 类型: string[]
    • 默认值: 取决于测试环境和依赖类型。

    控制解析包 exports 字段时使用的条件名称列表。

    对于 node、jsdom 和 happy-dom 测试,Rstest 继承 Rspack 的默认条件。在默认的 NODE_ENV=test 下,打包 JavaScript 依赖时使用以下条件:

    测试环境ESM importCommonJS require
    nodeimport、module、webpack、development、noderequire、module、webpack、development、node
    jsdom / happy-dombrowser、import、module、webpack、development、nodebrowser、require、module、webpack、development、node

    Rstest 在打包时将 NODE_ENV=test 视为开发模式,因此即使 NODE_ENV 为 'test',也会启用 development 条件。匹配入口的优先级由包的 exports 键顺序决定,而非 conditionNames 的排列顺序。

    自定义此选项时,可通过 '...' 保留 Rspack 的默认条件。例如,node 测试可以使用 conditionNames: ['custom', '...'],jsdom / happy-dom 测试则使用 conditionNames: ['browser', 'custom', '...']。显式设置此选项后,Rstest 不再自动添加 browser。

    不包含 '...' 的列表会覆盖各依赖类型的默认条件,包括 import 和 require 的区别。

    如果需要移除某个默认条件,同时保留 ESM 和 CommonJS 各自的解析条件,请配置 tools.rspack.resolve.byDependency。可参考 Jest 迁移示例,其中演示了如何排除 development,并说明了何时需要包含 browser。

    resolve.mainFields resolve.mainFieldsresolve.mainFields

    • 类型: string[]
    • 默认值: ['main'](node);['module', 'main'](jsdom / happy-dom);['browser', 'module', 'main'](Browser Mode)

    控制 package.json 入口字段的优先级顺序,例如 main、module 和 browser。

    • 在 node 测试环境中,Rstest 会从 mainFields 中过滤掉 'module'(默认为 ['main']),因为包的 module 字段并不总是原生 Node ESM 模块。
    • 在 node、jsdom 和 happy-dom 中处理 CommonJS require 时,Rstest 还会将 resolve.byDependency.commonjs.mainFields 设为 ['main', '...']。