是 webpack5 新增的功能。
模块联邦的出现是为了不同开发小组可以共同开发一个或多个应用,类似于微前端的 qiankun 和 single-spa。
使用模块联邦可以把一个应用,划分成多个块,比如 头部、主体、等,每个应用块由不同的组开发。
提供给别人使用的应用叫做 remote。
引用别人的应用叫做 host。
host 可以使用 remote 暴露出来的模块。
// 微前端的终极解决方法// 两个项目// 给别人引用的叫做 remote// 引用别人的叫做 host```js// remote 给别人用的import ('./bootstrap')// bootstrapimport React from 'react'import App from './app'// Appimport NewsList from './components/NewsList' // 常规开发 新闻列表组件... ...常规代码webpack.config.js// 引入内置插件 模块联邦const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin')output: {// 产出资源在HTML模板中引入的路径publicPath: 'http://localhost:8080/'}plugins: [new ModuleFederationPlugin({filename: 'remoteEntry.js',name: 'remote',exposes: { 被远程引用的时候可暴露的资源路径和别名'./remoteList': './src/remoteList'}})]
// host// 也要引入 bootstrap引用 remote 暴露出来的模块const RemoteList = React.lazy(() => import('remote/RemoteList'))<RemoteList />new ModuleFederationPlugin({filename: 'remoteEntry.js',name: 'remote',remotes: {remote: 'remote@http://lcoalhost:3000/remoteEntry.js'}})
```
