webpack-module-federation - 模块联合不适用于 webpack.config.js 中的动态挂钩远程
问题描述
当我在 index.html 中预先加载遥控器时,我已经设置了模块联合并且工作正常
下面的作品
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://localhost:8002/remoteEntry.js"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
host webpack
{
name: "home",
library: {type: "var", name: "home"},
filename: "remoteEntry.js",
remotes: {
nav: "dashboards",
},
shared: ["react", "react-dom"],
}
remote webpack
{
name: "dashboards",
library: {type: "var", name: "dashboards"},
filename: "remoteEntry.js",
remotes: {},
exposes: {
Header: "./src/Header",
},
shared: ["react", "react-dom"],
}
但是,这会预先加载 JS 文件,这是不可取的。我正在关注在 webpack 中动态加载库的示例......这就是我想要做的
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<div id="root"></div>
</body>
</html>
host webpack
{
name: "app-shell",
filename: "remoteEntry.js",
remotes: {
dashboards: "dashboards@http://localhost:8002/remoteEntry.js",
},
}
remote webpack
{
name: "dashboards",
filename: "remoteEntry.js",
remotes: {},
exposes: {
Header: "./src/Header.jsx",
},
}
我在主机应用程序上收到错误
Uncaught syntax error; Invalid or unexpected token:
module.exports = dashboards@http://localhost:8002/dist/remoteEntry.js;
解决方案
你的远程 Webpack 暴露对象不正确。你错过了./
前面的Header
。替换为:
"./Header": "./src/Header.jsx"
此外,如果这不起作用,请尝试删除库部分:
library: {type: "var", name: "dashboards"}
我认为这将remoteType
from更改var
为script
,这显然有帮助。
推荐阅读
- c - 在 C 中修改时间函数
- reactjs - 试图运行 webpack 以获取 bundle.js 文件时卡住了
- python - Python TCP 套接字服务 %100 CPU 使用率
- python - 如何在二进制图像中随机拾取非零像素的位置?
- python - 为什么调用 Python 的“魔术方法”不像对应的运算符那样进行类型转换?
- python - Python Pandas - 在所有列的单元格中搜索正则表达式匹配
- java - 测试 LITTLE_ENDIAN 和 BIG_ENDIAN 方案
- java - 如何显示我自己的 FileNotFoundException 消息
- networkx - 如何删除networkx中的随机边?
- nativescript - Tabview 与 GridLayout 与 DockLayout