Back to Rspack

TreeShakingSharedPlugin

website/docs/en/plugins/webpack/tree-shaking-shared-plugin.mdx

2.0.11.5 KB
Original Source

import { Stability, ApiMeta } from '@components/ApiMeta';

TreeShakingSharedPlugin

<ApiMeta stability={Stability.Experimental} />

Overview

  • Performs on-demand build and export optimization for shared dependencies based on Module Federation configuration.

Options

  • mfConfig: ModuleFederationPluginOptions, configuration passed to the Module Federation plugin.
  • plugins: extra plugins reused during independent builds.
  • secondary: whether to perform a second tree-shake during independent builds (typically used when the deployment platform has determined complete dependency info and triggers a fresh build to improve tree-shake accuracy for shared dependencies).

Usage

js
import { TreeShakingSharedPlugin } from '@rspack/core';

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

Behavior

  • Normalizes shared into [shareName, SharedConfig][].
  • Registers SharedUsedExportsOptimizerPlugin when secondary is false to inject used-exports from the stats manifest.
  • Triggers independent compilation for shared entries with tree shaking: true, and writes the produced assets back to the stats/manifest (fallback fields).