Webpack模块联合从错误的URL加载块

unhi4e5o  于 2022-11-13  发布在  Webpack
关注(0)|答案(1)|浏览(178)

我正在使用Webpack模块联合构建一个项目,设置如下:

  • React主机(在localhost:3000上运行)
  • Angular 遥控器1(在localhost:4201上运行)
  • Angular 遥控器2(在localhost:4202上运行)

目标是让两个遥控器都能工作。如果我只运行其中一个,删除另一个,它就能完美地工作。
我面临的问题是,当加载远程时,__webpack_require__.p是由其中一个远程的脚本设置的,因此另一个远程的块是从错误的url加载的。
下面是我得到的错误:

我的模块联合配置如下:

  • React主机:
.
.
.
new ModuleFederationPlugin({
      name: "react_host",
      filename: "remoteEntry.js",
      remotes: {
        angular_remote_1: "angular_remote_1@http://localhost:4201/angular_remote_1.js",
        angular_remote_2: "angular_remote_2@http://localhost:4202/angular_remote_2.js"
      },
      exposes: {},
      shared: {
        ...deps,
        react: {
          singleton: true,
          requiredVersion: deps.react,
        },
        "react-dom": {
          singleton: true,
          requiredVersion: deps["react-dom"],
        },
      },
    }),
.
.
.
  • Angular 遥控器1
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");

module.exports = {
    output: {
        publicPath: "auto",
        uniqueName: "angular_remote_1",
        scriptType: "text/javascript"
    },
    optimization: {
        runtimeChunk: false
    },
    experiments: {
        outputModule: true
    },
    plugins: [
        new ModuleFederationPlugin({
            name: "angular_remote_1",
            library: { type: "var", name: "angular_remote_1" },
            filename: "angular_remote_1.js",
            exposes: {
                './angularRemote1': './src/loadAngularRemote1.ts',
            },
            shared: ["@angular/core", "@angular/common", "@angular/router"]
        })
    ],
};
  • Angular 遥控器2
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");

module.exports = {
    output: {
        publicPath: "auto",
        uniqueName: "angular_remote_2",
        scriptType: "text/javascript"
    },
    optimization: {
        runtimeChunk: false
    },
    experiments: {
        outputModule: true,
    },
    plugins: [
        new ModuleFederationPlugin({
            name: "angular_remote_2",
            library: { type: "var", name: "angular_remote_2" },
            filename: "angular_remote_2.js",
            exposes: {
                './angularRemote2': './src/loadAngularRemote2.ts',
            },
            shared: ["@angular/core", "@angular/common", "@angular/router"]
        })
    ],
};

到目前为止,我尝试过的方法:

  • 使用公共路径(介于auto和硬编码之间)
  • 为两个远程设备(而不是主机)设置自定义chunkLoadingGlobal

此问题的精确复制可在此处找到:https://github.com/BarniPro/react-host-angular-remotes-microfrontend
任何帮助都是非常感谢的。

bhmjp9jg

bhmjp9jg1#

这个问题可以通过在遥控器的webpack.config.js中将topLevelAwait实验设置为true来解决:

experiments: {
    topLevelAwait: true,
},

这会导致两个远程同步加载,从而防止路径相互覆盖。
我必须做的另一个更新是禁用远程优化设置中的splitChunks选项(请参见SplitChunksPlugin):

optimization: {
    runtimeChunk: false, // This is also needed, but was added in the original question as well
    splitChunks: false,
}

Github repo已更新为工作溶液。

相关问题