Module Federation, Electron & React

huangapple go评论85阅读模式
英文:

Module Federation, Electron & React

问题

可以在 electron 中使用模块联邦,但是在 webpack.config.js 文件中可能需要调整路径。在远程应用的 webpack.config.js 中,尝试将远程模块的路径更改为相对路径或 electron 协议路径。例如:

remotes: {
  FIRST_APP: "FIRST_APP@electron://localhost:1212/remoteEntry.js",
},

然后,在主机应用的 webpack.config.js 中,确保使用正确的路径将模块联邦公开:

exposes: {
  "./app": "./src/renderer/App",
},

这样应该有助于解决在使用 electron 时找不到远程应用导入语句的问题。

英文:

Is it possible to use module federation with electron? I am new to both and I'm unsure if the following code in webpack.config.js of the remote app makes sense since I'm using electron and not a regular web app that runs on localhost:

plugins: [
    new HtmlWebpackPlugin({
      template: `ejs-webpack-loader!src/renderer/index.ejs`,
    }),
    new ModuleFederationPlugin({
      name: "MICRO",
      remotes: {
        FIRST_APP: "FIRST_APP@http://localhost:1212/remoteEntry.js",
      },
    }),
  ],
],

Will the remote App understand where FIRST_APP (host) is when I import it if I am using electron? It seems to find the path when not using electron.

This is how FIRST_APP is exposed in webpack.config.js of the host app:

plugins: [
    new HtmlWebpackPlugin({
        template: `ejs-webpack-loader!src/renderer/index.ejs`,
    }),
    new ModuleFederationPlugin({
      name: "FIRST_APP",
      filename: "remoteEntry.js",
      exposes: {
        "./app": "./src/renderer/App",
      },
    }),
  ],

I followed the instructions as per this tutorial:
https://blog.bitsrc.io/build-microfrontend-in-react-in-3-easy-steps-74790fd0c9fb

When not using electron module federation functions as expected but when adding electron it fails to find the import statement from the remote app and gives the error: "Module not found: Error: Can't resolve 'FIRST_APP/app'"

import React, { lazy, Suspense } from "react";
const FirstApp = React.lazy(() => import("FIRST_APP/app")); //Is not finding this


const App = () => {
  const [name, setName] = React.useState(null);

  return (
    <div className="App">
      <h1>This is second app</h1>
      <h2>Micro host app is integrated here</h2>
      { name ? <p>Your name is: {name}</p> : null }
      <div>
      <Suspense fallback={<span>Loading...</span>}>
        <FirstApp />
      </Suspense>
      </div>
    </div>
  );
};

export default App;

Please let me know if anyone has any ideas. Thanks!

答案1

得分: 1

已解决:
我在使用 npx create-react-app 创建的默认 react 应用程序时遇到了一些问题,很难使模块联合与 electron 配合使用。我认为提供了一些额外的代码,这使得模块联合在 electron 中难以运行。我通过以下步骤解决了这个问题:

  1. 我按照这个教程的步骤来设置在 react 应用程序中使用模块联合:
    https://blog.bitsrc.io/build-microfrontend-in-react-in-3-easy-steps-74790fd0c9fb
    这个教程从头开始创建一个 react 应用程序,并指导您如何通过仅添加所需的库而不添加任何额外内容来实现模块联合。
  2. 我将 react 应用程序升级为使用 TypeScript。这相当简单,可以按照 react 文档的说明进行。
  3. 我按照这个教程来了解如何同时在主机和远程应用程序中添加 electron 并并行运行 react 和 electron:
    https://www.youtube.com/watch?v=2_fROfS8FPE&ab_channel=zayne
    这使我能够通过仅添加所需的库而没有其他内容来使 electron 正常工作。

我发现使用默认的 react 应用程序或 electron 文档提供的 electron-react-boiler-plate 存储库会导致许多额外的代码,从而引发问题。

英文:

Solved:
I had trouble getting this to work using the out of the box react apps that are created when using npx create-react-app. I think there is some extra code that is provided that made it difficult to get module federation working with electron. I solved this by taking the following steps:

  1. I followed this tutorial that explains how to setup module federation in a react app:
    https://blog.bitsrc.io/build-microfrontend-in-react-in-3-easy-steps-74790fd0c9fb
    This creates a react app from scratch and walks you through how to implement module federation by only adding the libraries that you need and nothing extra.
  2. I upgraded the react app to use typescript. This is pretty straightforward and can be done by following react documentation.
  3. I followed this tutorial to figure out how to add electron to both the host and remote apps and concurrently run react and electron:
    https://www.youtube.com/watch?v=2_fROfS8FPE&ab_channel=zayne
    This allowed me to get electron working by only adding the libraries I need and nothing more.

I have found using out of the box react apps or the electron-react-boiler-plate repo provided by the electron documentation provided a lot of extra code that was causing issues.

huangapple
  • 本文由 发表于 2023年5月10日 22:14:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定