该错误通常是因为在使用模块联邦时,主应用程序不能正确的加载远程模块。要解决此问题,需要进行以下步骤:
// webpack.config.js
const { ModuleFederationPlugin } = require('webpack').container; const path = require('path');
module.exports = { ...,
plugins: [ new ModuleFederationPlugin({ name: 'remote_app', filename: 'remoteEntry.js', exposes: { './MyModule': './src/MyModule', }, shared: { react: { singleton: true }, 'react-dom': { singleton: true }, }, }), ], };
// webpack.config.js
const { ModuleFederationPlugin } = require('webpack').container; const path = require('path');
module.exports = { ...,
plugins: [ new ModuleFederationPlugin({ name: 'main_app', remotes: { remote_app: 'remote_app@https://remote-app-url/remoteEntry.js', }, shared: { react: { singleton: true }, 'react-dom': { singleton: true }, }, }), ], };
// index.html
完成以上步骤后,您的主应用程序应该能够正确加载远程应用程序中暴露的模块,而不再出现 ChunkLoad 错误。