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/plugins/webpack/tree-shaking-shared-plugin.md.
close

TreeShakingSharedPlugin

Stability: Experimental

概览

基于模块联邦配置对 shared 依赖进行按需构建与导出优化。

选项

  • mfConfigModuleFederationPluginOptions,传入模块联邦插件所需要的配置项。
  • secondary:是否在独立编译阶段执行二次摇树优化(二次摇树通常在部署平台确定了完整的依赖信息后重新触发的一次全新构建,提高共享依赖 tree shaking 命中的准确率)。

使用示例

rspack.config.mjs
import { sharing } from '@rspack/core';

export default {
  plugins: [
    new sharing.TreeShakingSharedPlugin({
      secondary: true,
      mfConfig: {
        name: 'app',
        shared: {
          'lodash-es': { treeShaking: { mode: 'server-calc' } },
        },
        library: { type: 'var', name: 'App' },
        manifest: true,
      },
    }),
  ],
};

行为说明

  • 读取 shared 配置后标准化为 [shareName, SharedConfig][]
  • secondaryfalse 时,注册 SharedUsedExportsOptimizerPlugin,基于 stats 清单注入已用导出集合。
  • 仅当共享依赖启用了 treeShaking 且保留本地实现(即未设置 import: false)时,插件才会为它创建独立构建。
  • 启用 mfConfig.manifest 后,独立构建的产物信息会写入 stats 和 manifest,供运行时作为 fallback 加载。