Vite Module Federation中remoteEntry.js返回HTML页面导致模块加载失败的问题求助
Vite Module Federation中remoteEntry.js返回HTML页面导致模块加载失败的问题求助
大家好,我最近在尝试用Vite搭配vite-plugin-federation来搭建微前端架构,碰到了一个卡壳的问题,想过来请教下各位:
我目前有两个独立的应用:
- Host App:运行在 http://localhost:5000
- Remote App(会计模块):运行在 http://localhost:5002
我的配置情况
在Remote App的vite.config.ts里,我是这么配置federation的:
federation({ name: 'accounting', filename: 'remoteEntry.js', exposes: { './AccountingPage': './src/pages/AccountingPage.tsx', }, shared: ['react', 'react-dom'], })
然后在Host App里,我用懒加载的方式导入这个远程页面:
const AccountingPage = lazy(() => import('accounting/AccountingPage'));
遇到的问题
当我启动Host App并尝试加载这个远程模块时,控制台直接抛出了错误:
Uncaught TypeError: Failed to fetch dynamically imported module: http://localhost:5002/assets/remoteEntry.js
更奇怪的是,我直接在浏览器地址栏访问http://localhost:5002/assets/remoteEntry.js时,返回的不是预期的JavaScript文件,而是Remote App的index.html页面!这显然直接搞崩了模块加载的流程。
想请教的问题
- 我该怎么配置才能让Vite正确返回
remoteEntry.js文件,而不是默认的HTML页面? - 是不是我在
vite.config.ts或者vite-plugin-federation的设置里漏了什么关键配置,才能确保不管是开发环境还是生产环境,remoteEntry.js都能被正常访问到?
内容来源于stack exchange
相关产品推荐
相关产品推荐

