zjffun blog

webpack Module Federation

更新于 写于 前端

Module Federation allows a JavaScript application to dynamically load code from another application and in the process, share dependencies.

e.g.: Let application A use the Component1 of application B. Module Federation can make the Component1 like an independent resource and application A and application B use the same one.

js
// A
new ModuleFederationPlugin({
  name: "appA",
  remotes: {
    appB: "appB",
  },
  shared: ["react", "react-dom"],
});

// B
new ModuleFederationPlugin({
  name: "appB",
  filename: "remoteEntry.js",
  exposes: {
    Component1: "./src/Component1",
  },
  shared: ["react", "react-dom"],
});

See: