简体   繁体   English

如果宿主 React 应用程序不使用 Webpack,如何使用联合 React 模块?

[英]How to use a federated React module if the host React application isn't using Webpack?

I have two React applications.我有两个 React 应用程序。 One is the remote app, and other is the host app.一个是远程应用程序,另一个是主机应用程序。 The remote app uses Webpack 5 using which I have federated the module.远程应用程序使用我已经联合模块的 Webpack 5。 The host app uses Rollup for all purposes.主机应用程序将汇总用于所有目的。 I want the host application to use the federated module.我希望主机应用程序使用联合模块。 How can I achieve this?我怎样才能做到这一点?

The below is how we have exposed the remote app which is running on port 3000.下面是我们如何暴露在端口 3000 上运行的远程应用程序。

            name: "ResetPassword",
            filename: "remoteEntry.js",
            remotes: {},
            exposes: {
                "./ResetPassword": "./src/pages/create-password",
            },
            shared: { 
                ...packageJsonDeps,
                react: { singleton: true, requiredVersion: packageJsonDeps.react },
                "react-dom": { singleton: true, requiredVersion: packageJsonDeps["react-dom"] }
            },
        })

This official Module Federation example solves exactly this issue. 这个官方的 Module Federation 示例正好解决了这个问题。

Basically -基本上 -

  1. In Rollup host - add Module federation in the plugins section of the config.在汇总主机中 - 在配置的插件部分添加模块联合。
    a.一个。 define remotes .定义remotes
    b.湾。 define shared定义shared
  2. Add Rollup remote in MF app.在 MF 应用中添加 Rollup 遥控器。

Check this article for a simplistic walkthrough查看这篇文章以获得简单的演练

btw it's good practice to define the shared libraries/packages in your package.json something like顺便说一句,在 package.json 中定义共享库/包是一种很好的做法,例如

shared: require('../package.json').dependencies

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Webpack4和Babel的React应用程序模块构建失败 - Module Build Failed for react application using Webpack4 and Babel 如何使用webpack在React应用程序中异步需要模块? - How to asynchronously require modules in React application using webpack? 无法使 Context API 在 React 应用程序上工作,未捕获的 TypeError: react__WEBPACK_IMPORTED_MODULE_0__.useContext(...) is not iterable - Can't make Context API work on React application, Uncaught TypeError: react__WEBPACK_IMPORTED_MODULE_0__.useContext(...) is not iterable 无法使用webpack 4和babel 7在我的react-application中导入jsx模块 - Unable to import a jsx module in my react-application with webpack 4 and babel 7 如何在不弹出的情况下将 Webpack 模块联合插件与 create-react-app 一起使用 - How to use Webpack Module Federation plugin with create-react-app without ejecting 如何在React和Webpack中使用srcset 2x? - How to use srcset 2x with React and Webpack? 如何在反应中使用 webpack 和 web3? - how to use webpack and web3 in react? 如何在 React 和 Webpack 中使用 Sass-loader? - How to use Sass-loader with React and Webpack? 如何使用Webpack部署Express + React应用 - How to use webpack to deploy an Express + React app 如何在React 16.7和Webpack 4.29中使用LESS? - How to use LESS in React 16.7 with webpack 4.29?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM