Rspack 插件

注意

需要 Rspack 0.5.0 及以上版本

  • 使用 Rspack 构建工具的应用构建出满足 Module Federation 加载规范的微模块
  • 能够在构建插件里面使用别名消费微模块
  • 能够设置微模块的共享依赖配置,当加载微模块的宿主环境已经存在对应依赖时将不会重复加载
  • 当微模块具备远程类型时将会自动把远程模块的类型下载下来消费
  • 消费远程模块时将具备热更新能力

快速开始

安装

你可以通过如下的命令安装插件:

npm
yarn
pnpm
bun
npm add @module-federation/enhanced

创建 module-federation.config.ts

创建 module-federation.config.ts 文件,内容如下:

module-federation.config.ts
import { createModuleFederationConfig } from '@module-federation/enhanced/rspack';

export default createModuleFederationConfig({
  name: 'host',
  remotes: {
    provider: 'provider@http://localhost:2004/mf-manifest.json',
  },
  exposes: {
    './Button': './src/components/Button.tsx',
  },
  shared: {
    react: {
      singleton: true,
    },
    'react-dom': {
      singleton: true,
    },
  },
});

注册插件

Rspack 中,你可以通过 plugins 配置项来添加插件:

rspack.config.ts
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
import mfConfig from './module-federation.config';

export default defineConfig({
  plugins: [new ModuleFederationPlugin(mfConfig)],
});

配置

你可以在 Config 总览 页面找到所有配置项的详细说明。