1655766000
Module Federation 是在 React 应用程序中构建微前端架构的绝佳工具。我将在分步指南中向您展示如何使用它在 React 中构建 Host-Remote 模式的微前端。
为什么是微前端?
微前端帮助我们将大型前端应用程序分解为较小的独立应用程序或模块,这些应用程序或模块可以按照自己的节奏构建和部署。
使用模块联合执行此操作允许我们在运行时在客户端浏览器中组合应用程序,并消除构建时依赖和协调,从而允许构建这些应用程序的团队进行大规模开发。
入门
最终配置可以在这里找到:https ://github.com/rautio/react-micro-frontend-example
我们正在构建两个应用程序:host
和remote
.
该host
应用程序是“主”应用程序,remote
是插入其中的子应用程序。
模块联合确实支持将host
视为远程,并在适合您的用例的情况下使架构对等。稍后再谈。
我们将使用create-react-app
来简化初始步骤。
在您的根目录中:
npx create-react-app hostnpx create-react-app remote
这将为您创建两个应用程序:
host/remote/
依赖项
在每个host/
中remote/
运行:
npm install --save-dev webpack webpack-cli html-webpack-plugin webpack-dev-server babel-loader
这将安装 wepback 和我们的 webpack 配置所需的依赖项。
Webpack Module Federation 仅在 webpack 5 及以上版本中可用。
主机应用
我们将从我们自己的 webpack 配置开始
webpack.config.js
在以下根目录创建一个新文件host/
:
// host/webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index",
mode: "development",
devServer: {
port: 3000,
},
module: {
rules: [
{
test: /\.(js|jsx)?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
},
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./public/index.html",
}),
],
resolve: {
extensions: [".js", ".jsx"],
},
target: "web",
};
这是一个基本的 webpack 示例,用于使用模板转换我们的js
代码jsx
并babel-loader
注入到html
模板中。
更新 package.json 脚本
接下来,我们需要一个start
利用我们的 webpack 配置的新脚本:
"scripts":{
"start": "webpack serve"
}
现在我们可以了解主机应用程序的核心了。
index.js
首先,我们需要index.js
进入我们的应用程序。我们正在导入另一个bootstrap.js
呈现 React 应用程序的文件。
我们需要这个额外的间接层的原因是它让 Webpack 有机会加载渲染远程应用程序所需的所有导入。
否则,您会看到如下错误:
Shared module is not available for eager consumption
// host/src/index.js
import("./bootstrap");
// Note: It is important to import bootstrap dynamically using import() otherwise you will also see the same error.
引导程序.js
接下来,我们定义bootstrap.js
呈现我们的 React 应用程序的文件。
// host/src/bootstrap.js
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
应用程序.js
现在我们准备App.js
在应用程序的主要逻辑发生的地方编写我们的文件。remote
在这里,我们将从稍后定义的 2 个组件中加载。
import("Remote/App")
将动态获取远程应用的App.js
React 组件。
我们需要使用惰性加载器和ErrorBoundary组件来为用户创造流畅的体验,以防提取需要很长时间或在我们的主机应用程序中引入错误。
// host/src/App.js
import React from "react";
import ErrorBoundary from "./ErrorBoundary";
const RemoteApp = React.lazy(() => import("Remote/App"));
const RemoteButton = React.lazy(() => import("Remote/Button"));
const RemoteWrapper = ({ children }) => (
<div
style={{
border: "1px solid red",
background: "white",
}}
>
<ErrorBoundary>{children}</ErrorBoundary>
</div>
);
export const App = () => (
<div style={{ background: "rgba(43, 192, 219, 0.3)" }}>
<h1>This is the Host!</h1>
<h2>Remote App:</h2>
<RemoteWrapper>
<RemoteApp />
</RemoteWrapper>
<h2>Remote Button:</h2>
<RemoteWrapper>
<RemoteButton />
</RemoteWrapper>
<br />
<a href="http://localhost:4000">Link to Remote App</a>
</div>
);
export default App;
添加模块联合
我们还没有准备好运行该应用程序。接下来,我们需要添加 Module Federation 来告诉我们host
从哪里获取Remote/App
和Remote/Button
组件。
在webpack.config.js
我们的介绍中ModuleFederationPlugin
:
// host/webpack.config.js
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const { dependencies } = require("./package.json");
module.exports = {
//...
plugins: [
new ModuleFederationPlugin({
name: "Host",
remotes: {
Remote: `Remote@http://localhost:4000/moduleEntry.js`,
},
shared: {
...dependencies,
react: {
singleton: true,
requiredVersion: dependencies["react"],
},
"react-dom": {
singleton: true,
requiredVersion: dependencies["react-dom"],
},
},
}),
//...
],
//...
};
需要注意的重要事项:
name
用于区分模块。它在这里并不重要,因为我们没有公开任何东西,但它在Remote
应用程序中非常重要。remotes
是我们定义我们想在这个应用程序中使用的联合模块的地方。您会注意到我们指定Remote
为内部名称,因此我们可以使用import("Remote/<component>")
. 但我们也定义了远程模块定义所在的位置:Remote@http://localhost:4000/moduleEntry.js
. 这个 URL 告诉我们三个重要的事情。模块的名称是Remote
,它托管在其上localhost:4000
,其模块定义是moduleEntry.js
。shared
是我们如何在模块之间共享依赖关系。这对于 React 非常重要,因为它具有全局状态,这意味着您应该在任何给定的应用程序中只运行一个 React 和 ReactDOM 实例。为了在我们的架构中实现这一点,我们告诉 webpack 将 React 和 ReactDOM 视为singleton
从任何模块加载的第一个版本用于整个应用程序。只要满足requiredVersion
我们定义的。我们还从package.json
此处导入所有其他依赖项并将它们包括在内,因此我们最大限度地减少了模块之间重复依赖项的数量。现在,如果我们npm start
在主机应用程序中运行,我们应该会看到如下内容:
这意味着我们的host
应用程序已完全配置,但我们的remote
应用程序尚未公开任何内容。所以我们接下来需要配置它。
远程应用
让我们从webpack
配置开始。由于我们现在对模块联合有了一些了解,让我们从一开始就使用它:
// remote/webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const path = require("path");
const { dependencies } = require("./package.json");
module.exports = {
entry: "./src/index",
mode: "development",
devServer: {
static: {
directory: path.join(__dirname, "public"),
},
port: 4000,
},
module: {
rules: [
{
test: /\.(js|jsx)?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
},
},
],
},
],
},
plugins: [
new ModuleFederationPlugin({
name: "Remote",
filename: "moduleEntry.js",
exposes: {
"./App": "./src/App",
"./Button": "./src/Button",
},
shared: {
...dependencies,
react: {
singleton: true,
requiredVersion: dependencies["react"],
},
"react-dom": {
singleton: true,
requiredVersion: dependencies["react-dom"],
},
},
}),
new HtmlWebpackPlugin({
template: "./public/index.html",
}),
],
resolve: {
extensions: [".js", ".jsx"],
},
target: "web",
};
需要注意的重要事项是:
localhost:4000
Remote
filename
是_moduleEntry.js
将这些结合在一起将允许我们的主机在以下位置找到远程代码Remote@http://localhost:4000/moduleEntry.js
exposes
是我们定义要在moduleEntry.js
文件中共享的代码的地方。在这里,我们暴露了两个:<App />
和<Button />
。
现在让我们设置这些组件和我们的远程应用程序,以便它可以自己运行。
index.js
与 Host 应用程序类似,我们需要在 webpack 条目中进行动态导入。
// /remote/src/index.js
import("./bootstrap");
引导程序.js
// remote/src/bootstrap.js
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
应用程序.js
远程应用程序比主机简单得多:
// remote/src/App.js
import React from "react";
export const App = () => {
return <div>Hello from the other side</div>;
};
export default App;
按钮.js
而且我们还想暴露一个<Button /> component
// remote/src/Button.js
import React from "react";
export const Button = () => <button>Hello!</button>;
export default Button;
现在远程应用程序已完全配置,如果您运行npm start
,您应该会看到一个空白页面,其中包含“来自另一端的你好”。
把它们放在一起
现在,如果我们npm start
在host/
和remote/
目录中运行,我们应该看到主机应用程序正在运行,localhost:3000
远程应用程序正在运行localhost:4000
。
主机应用程序看起来像这样:
恭喜!您现在已经使用 React 配置了一个微前端应用程序。
发展
您可以通过在根级别配置纱线工作区来简化开发流程:https ://classic.yarnpkg.com/lang/en/docs/workspaces/
部署
我们只介绍了在本地运行微前端。如果您想部署它们,您可以将每个应用程序分别部署到它们自己的 CDN 或托管服务,并配置 webpack 定义以使用环境变量或其他方式来动态更新ModuleFederationPlugin
定义中的 URL。
这方面的一个例子可以在我更高级的示例应用程序中找到:https ://github.com/rautio/micro-frontend-demo
来源:https ://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0
1598839687
If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?
In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.
Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.
React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.
Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.
Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.
The popularity of React Native comes from its advantages. Some of its advantages are as follows:
Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.
React Native is very close to native. Consider the following aspects as described on the React Native website:
Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.
#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native
1615544450
Since March 2020 reached 556 million monthly downloads have increased, It shows that React JS has been steadily growing. React.js also provides a desirable amount of pliancy and efficiency for developing innovative solutions with interactive user interfaces. It’s no surprise that an increasing number of businesses are adopting this technology. How do you select and recruit React.js developers who will propel your project forward? How much does a React developer make? We’ll bring you here all the details you need.
Facebook built and maintains React.js, an open-source JavaScript library for designing development tools. React.js is used to create single-page applications (SPAs) that can be used in conjunction with React Native to develop native cross-platform apps.
In the United States, the average React developer salary is $94,205 a year, or $30-$48 per hour, This is one of the highest among JavaScript developers. The starting salary for junior React.js developers is $60,510 per year, rising to $112,480 for senior roles.
In context of software developer wage rates, the United States continues to lead. In high-tech cities like San Francisco and New York, average React developer salaries will hit $98K and $114per year, overall.
However, the need for React.js and React Native developer is outpacing local labour markets. As a result, many businesses have difficulty locating and recruiting them locally.
It’s no surprise that for US and European companies looking for professional and budget engineers, offshore regions like India are becoming especially interesting. This area has a large number of app development companies, a good rate with quality, and a good pool of React.js front-end developers.
As per Linkedin, the country’s IT industry employs over a million React specialists. Furthermore, for the same or less money than hiring a React.js programmer locally, you may recruit someone with much expertise and a broader technical stack.
React is a very strong framework. React.js makes use of a powerful synchronization method known as Virtual DOM, which compares the current page architecture to the expected page architecture and updates the appropriate components as long as the user input.
React is scalable. it utilises a single language, For server-client side, and mobile platform.
React is steady.React.js is completely adaptable, which means it seldom, if ever, updates the user interface. This enables legacy projects to be updated to the most new edition of React.js without having to change the codebase or make a few small changes.
React is adaptable. It can be conveniently paired with various state administrators (e.g., Redux, Flux, Alt or Reflux) and can be used to implement a number of architectural patterns.
Is there a market for React.js programmers?
The need for React.js developers is rising at an unparalleled rate. React.js is currently used by over one million websites around the world. React is used by Fortune 400+ businesses and popular companies such as Facebook, Twitter, Glassdoor and Cloudflare.
As you’ve seen, locating and Hire React js Developer and Hire React Native developer is a difficult challenge. You will have less challenges selecting the correct fit for your projects if you identify growing offshore locations (e.g. India) and take into consideration the details above.
If you want to make this process easier, You can visit our website for more, or else to write a email, we’ll help you to finding top rated React.js and React Native developers easier and with strives to create this operation
#hire-react-js-developer #hire-react-native-developer #react #react-native #react-js #hire-react-js-programmer
1651604400
React Starter Kit is an opinionated boilerplate for web development built on top of Node.js, Express, GraphQL and React, containing modern web development tools such as Webpack, Babel and Browsersync. Helping you to stay productive following the best practices. A solid starting point for both professionals and newcomers to the industry.
See getting started guide, demo, docs, roadmap | Join #react-starter-kit chat room on Gitter | Visit our sponsors:
The master
branch of React Starter Kit doesn't include a Flux implementation or any other advanced integrations. Nevertheless, we have some integrations available to you in feature branches that you can use either as a reference or merge into your project:
master
)feature/redux
)feature/apollo
)master
)You can see status of most reasonable merge combination as PRs labeled as TRACKING
If you think that any of these features should be on master
, or vice versa, some features should removed from the master
branch, please let us know. We love your feedback!
React Starter Kit
| React Static Boilerplate
| ASP.NET Core Starter Kit
| |
---|---|---|---|
App type | Isomorphic (universal) | Single-page application | Single-page application |
Frontend | |||
Language | JavaScript (ES2015+, JSX) | JavaScript (ES2015+, JSX) | JavaScript (ES2015+, JSX) |
Libraries | React, History, Universal Router | React, History, Redux | React, History, Redux |
Routes | Imperative (functional) | Declarative | Declarative, cross-stack |
Backend | |||
Language | JavaScript (ES2015+, JSX) | n/a | C#, F# |
Libraries | Node.js, Express, Sequelize, GraphQL | n/a | ASP.NET Core, EF Core, ASP.NET Identity |
SSR | Yes | n/a | n/a |
Data API | GraphQL | n/a | Web API |
♥ React Starter Kit? Help us keep it alive by donating funds to cover project expenses via OpenCollective or Bountysource!
Anyone and everyone is welcome to contribute to this project. The best way to start is by checking our open issues, submit a new issue or feature request, participate in discussions, upvote or downvote the issues you like or dislike, send pull requests.
Copyright © 2014-present Kriasoft, LLC. This source code is licensed under the MIT license found in the LICENSE.txt file. The documentation to the project is licensed under the CC BY-SA 4.0 license.
Author: kriasoft
Source Code: https://github.com/kriasoft/react-starter-kit
License: MIT License
1621573085
Expand your user base by using react-native apps developed by our expert team for various platforms like Android, Android TV, iOS, macOS, tvOS, the Web, Windows, and UWP.
We help businesses to scale up the process and achieve greater performance by providing the best react native app development services. Our skilled and experienced team’s apps have delivered all the expected results for our clients across the world.
To achieve growth for your business, hire react native app developers in India. You can count on us for all the technical services and support.
#react native app development company india #react native app developers india #hire react native developers india #react native app development company #react native app developers #hire react native developers
1655766000
Module Federation 是在 React 应用程序中构建微前端架构的绝佳工具。我将在分步指南中向您展示如何使用它在 React 中构建 Host-Remote 模式的微前端。
为什么是微前端?
微前端帮助我们将大型前端应用程序分解为较小的独立应用程序或模块,这些应用程序或模块可以按照自己的节奏构建和部署。
使用模块联合执行此操作允许我们在运行时在客户端浏览器中组合应用程序,并消除构建时依赖和协调,从而允许构建这些应用程序的团队进行大规模开发。
入门
最终配置可以在这里找到:https ://github.com/rautio/react-micro-frontend-example
我们正在构建两个应用程序:host
和remote
.
该host
应用程序是“主”应用程序,remote
是插入其中的子应用程序。
模块联合确实支持将host
视为远程,并在适合您的用例的情况下使架构对等。稍后再谈。
我们将使用create-react-app
来简化初始步骤。
在您的根目录中:
npx create-react-app hostnpx create-react-app remote
这将为您创建两个应用程序:
host/remote/
依赖项
在每个host/
中remote/
运行:
npm install --save-dev webpack webpack-cli html-webpack-plugin webpack-dev-server babel-loader
这将安装 wepback 和我们的 webpack 配置所需的依赖项。
Webpack Module Federation 仅在 webpack 5 及以上版本中可用。
主机应用
我们将从我们自己的 webpack 配置开始
webpack.config.js
在以下根目录创建一个新文件host/
:
// host/webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index",
mode: "development",
devServer: {
port: 3000,
},
module: {
rules: [
{
test: /\.(js|jsx)?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
},
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./public/index.html",
}),
],
resolve: {
extensions: [".js", ".jsx"],
},
target: "web",
};
这是一个基本的 webpack 示例,用于使用模板转换我们的js
代码jsx
并babel-loader
注入到html
模板中。
更新 package.json 脚本
接下来,我们需要一个start
利用我们的 webpack 配置的新脚本:
"scripts":{
"start": "webpack serve"
}
现在我们可以了解主机应用程序的核心了。
index.js
首先,我们需要index.js
进入我们的应用程序。我们正在导入另一个bootstrap.js
呈现 React 应用程序的文件。
我们需要这个额外的间接层的原因是它让 Webpack 有机会加载渲染远程应用程序所需的所有导入。
否则,您会看到如下错误:
Shared module is not available for eager consumption
// host/src/index.js
import("./bootstrap");
// Note: It is important to import bootstrap dynamically using import() otherwise you will also see the same error.
引导程序.js
接下来,我们定义bootstrap.js
呈现我们的 React 应用程序的文件。
// host/src/bootstrap.js
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
应用程序.js
现在我们准备App.js
在应用程序的主要逻辑发生的地方编写我们的文件。remote
在这里,我们将从稍后定义的 2 个组件中加载。
import("Remote/App")
将动态获取远程应用的App.js
React 组件。
我们需要使用惰性加载器和ErrorBoundary组件来为用户创造流畅的体验,以防提取需要很长时间或在我们的主机应用程序中引入错误。
// host/src/App.js
import React from "react";
import ErrorBoundary from "./ErrorBoundary";
const RemoteApp = React.lazy(() => import("Remote/App"));
const RemoteButton = React.lazy(() => import("Remote/Button"));
const RemoteWrapper = ({ children }) => (
<div
style={{
border: "1px solid red",
background: "white",
}}
>
<ErrorBoundary>{children}</ErrorBoundary>
</div>
);
export const App = () => (
<div style={{ background: "rgba(43, 192, 219, 0.3)" }}>
<h1>This is the Host!</h1>
<h2>Remote App:</h2>
<RemoteWrapper>
<RemoteApp />
</RemoteWrapper>
<h2>Remote Button:</h2>
<RemoteWrapper>
<RemoteButton />
</RemoteWrapper>
<br />
<a href="http://localhost:4000">Link to Remote App</a>
</div>
);
export default App;
添加模块联合
我们还没有准备好运行该应用程序。接下来,我们需要添加 Module Federation 来告诉我们host
从哪里获取Remote/App
和Remote/Button
组件。
在webpack.config.js
我们的介绍中ModuleFederationPlugin
:
// host/webpack.config.js
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const { dependencies } = require("./package.json");
module.exports = {
//...
plugins: [
new ModuleFederationPlugin({
name: "Host",
remotes: {
Remote: `Remote@http://localhost:4000/moduleEntry.js`,
},
shared: {
...dependencies,
react: {
singleton: true,
requiredVersion: dependencies["react"],
},
"react-dom": {
singleton: true,
requiredVersion: dependencies["react-dom"],
},
},
}),
//...
],
//...
};
需要注意的重要事项:
name
用于区分模块。它在这里并不重要,因为我们没有公开任何东西,但它在Remote
应用程序中非常重要。remotes
是我们定义我们想在这个应用程序中使用的联合模块的地方。您会注意到我们指定Remote
为内部名称,因此我们可以使用import("Remote/<component>")
. 但我们也定义了远程模块定义所在的位置:Remote@http://localhost:4000/moduleEntry.js
. 这个 URL 告诉我们三个重要的事情。模块的名称是Remote
,它托管在其上localhost:4000
,其模块定义是moduleEntry.js
。shared
是我们如何在模块之间共享依赖关系。这对于 React 非常重要,因为它具有全局状态,这意味着您应该在任何给定的应用程序中只运行一个 React 和 ReactDOM 实例。为了在我们的架构中实现这一点,我们告诉 webpack 将 React 和 ReactDOM 视为singleton
从任何模块加载的第一个版本用于整个应用程序。只要满足requiredVersion
我们定义的。我们还从package.json
此处导入所有其他依赖项并将它们包括在内,因此我们最大限度地减少了模块之间重复依赖项的数量。现在,如果我们npm start
在主机应用程序中运行,我们应该会看到如下内容:
这意味着我们的host
应用程序已完全配置,但我们的remote
应用程序尚未公开任何内容。所以我们接下来需要配置它。
远程应用
让我们从webpack
配置开始。由于我们现在对模块联合有了一些了解,让我们从一开始就使用它:
// remote/webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const path = require("path");
const { dependencies } = require("./package.json");
module.exports = {
entry: "./src/index",
mode: "development",
devServer: {
static: {
directory: path.join(__dirname, "public"),
},
port: 4000,
},
module: {
rules: [
{
test: /\.(js|jsx)?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
},
},
],
},
],
},
plugins: [
new ModuleFederationPlugin({
name: "Remote",
filename: "moduleEntry.js",
exposes: {
"./App": "./src/App",
"./Button": "./src/Button",
},
shared: {
...dependencies,
react: {
singleton: true,
requiredVersion: dependencies["react"],
},
"react-dom": {
singleton: true,
requiredVersion: dependencies["react-dom"],
},
},
}),
new HtmlWebpackPlugin({
template: "./public/index.html",
}),
],
resolve: {
extensions: [".js", ".jsx"],
},
target: "web",
};
需要注意的重要事项是:
localhost:4000
Remote
filename
是_moduleEntry.js
将这些结合在一起将允许我们的主机在以下位置找到远程代码Remote@http://localhost:4000/moduleEntry.js
exposes
是我们定义要在moduleEntry.js
文件中共享的代码的地方。在这里,我们暴露了两个:<App />
和<Button />
。
现在让我们设置这些组件和我们的远程应用程序,以便它可以自己运行。
index.js
与 Host 应用程序类似,我们需要在 webpack 条目中进行动态导入。
// /remote/src/index.js
import("./bootstrap");
引导程序.js
// remote/src/bootstrap.js
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
应用程序.js
远程应用程序比主机简单得多:
// remote/src/App.js
import React from "react";
export const App = () => {
return <div>Hello from the other side</div>;
};
export default App;
按钮.js
而且我们还想暴露一个<Button /> component
// remote/src/Button.js
import React from "react";
export const Button = () => <button>Hello!</button>;
export default Button;
现在远程应用程序已完全配置,如果您运行npm start
,您应该会看到一个空白页面,其中包含“来自另一端的你好”。
把它们放在一起
现在,如果我们npm start
在host/
和remote/
目录中运行,我们应该看到主机应用程序正在运行,localhost:3000
远程应用程序正在运行localhost:4000
。
主机应用程序看起来像这样:
恭喜!您现在已经使用 React 配置了一个微前端应用程序。
发展
您可以通过在根级别配置纱线工作区来简化开发流程:https ://classic.yarnpkg.com/lang/en/docs/workspaces/
部署
我们只介绍了在本地运行微前端。如果您想部署它们,您可以将每个应用程序分别部署到它们自己的 CDN 或托管服务,并配置 webpack 定义以使用环境变量或其他方式来动态更新ModuleFederationPlugin
定义中的 URL。
这方面的一个例子可以在我更高级的示例应用程序中找到:https ://github.com/rautio/micro-frontend-demo
来源:https ://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0