treeShakingSharedPlugins

A list of plugin module paths used when building independent shared fallbacks. During the secondary Tree Shaking build, each item will be require(p)-loaded and instantiated to participate in compilation.

  • Type: string[]
  • Required: No
  • Default: undefined

If shared.treeShaking.mode is set to 'server-calc', the deployment service will rebuild the shared dependencies that need Tree Shaking. In this process, only the shared dependencies are built, and the original project's build configuration is not loaded.

If the original project depends on special build configuration (for example externals), you can wrap that configuration into an NPM plugin and publish it, then list the package name in treeShakingSharedPlugins. The secondary Tree Shaking pass will automatically require and instantiate these plugins so that the shared module build inherits the original configuration.

For example, suppose you provide a plugin my-build-plugin that sets externals:

my-build-plugin
class MyBuildPlugin {
  apply(compiler){
    compiler.options.externals = {
      'react': 'React',
    }
  }
}
export default MyBuildPlugin;

Once you publish this plugin to npm, simply list its package name in treeShakingSharedPlugins:

module-federation.config.ts

export default {
  // ...
  treeShakingSharedPlugins: ['my-build-plugin@0.0.2'],
}