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

    • 类型: boolean
    • 默认值: false
    • CLI: --federation

    是否启用模块联邦(Module Federation)支持。

    Rstest 在 Node Mode(包括 jsdom 等 DOM 模拟环境)和 Browser Mode 中均支持模块联邦。

    启用后,Rstest 会给测试 worker 安装运行时垫片,让模块联邦运行时能通过 Rstest 的模块加载器加载远程入口和 chunk,模块 mock 保持生效。

    CLI
    rstest.config.ts
    npx rstest --federation

    测试联邦应用

    federation 只负责开启 Rstest 的测试运行时支持,不会配置远程模块、暴露模块或共享依赖。使用 @module-federation/rstest 时,该插件会配置模块联邦构建,并为基于 Node 的测试环境自动启用 Rstest 兼容模式,因此在这些环境下不需要再设置 federation: true

    Browser Mode 下插件不会自动打开 federation 配置项。但 globalSetup 文件始终在 Node 进程里执行,产物里同样带着模块联邦运行时,没有这个开关,globalSetup 文件一加载就会报错。所以 Browser Mode 项目只要用了 globalSetup,就要手动写上 federation: true

    安装方式和 Rstest 测试流程,请先阅读模块联邦指南。复用已有的 Rsbuild 模块联邦配置以及完整插件选项,请参考 Rstest 官方集成指南

    rstest.config.ts
    import { federation } from '@module-federation/rstest';
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      plugins: [
        federation({
          name: 'main_app',
          remotes: {
            'component-app': 'component_app@http://localhost:3001/remoteEntry.cjs',
          },
          shared: {
            react: { singleton: true },
          },
        }),
      ],
    });

    该 Node 示例消费面向 Node 的 remoteEntry.cjs,Browser Mode 应消费浏览器构建的 remoteEntry.js。两者的区别请参考模块联邦指南;完整配置可参考 Node 示例Browser 示例