Skip to content

Commit 7746d8a

Browse files
authored
Add files via upload
1 parent 8b04c55 commit 7746d8a

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

Day4/MicroUIApplication.txt

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
Creating a Micro UI using React involves several steps. Here's a simplified example of setting up a basic Micro UI architecture with two independent React applications that communicate with each other. We'll use Webpack 5 with Module Federation to demonstrate this.
2+
Step 1: Set Up the Host Application
3+
Initialize the Host Application:
4+
npx create-react-app host-app
5+
cd host-app
6+
Install Dependencies:
7+
npm install webpack webpack-cli html-webpack-plugin webpack-dev-server @babel/core babel-loader @babel/preset-env @babel/preset-react --save-dev
8+
9+
Configure Webpack:
10+
Create webpack.config.js in the root of the host-app directory:
11+
const HtmlWebpackPlugin = require('html-webpack-plugin');
12+
const ModuleFederationPlugin = require('webpack').container.ModuleFederationPlugin;
13+
14+
module.exports = {
15+
mode: 'development',
16+
devServer: {
17+
port: 3000,
18+
},
19+
module: {
20+
rules: [
21+
{
22+
test: /\.jsx?$/,
23+
loader: 'babel-loader',
24+
options: {
25+
presets: ['@babel/preset-react'],
26+
},
27+
},
28+
],
29+
},
30+
plugins: [
31+
new ModuleFederationPlugin({
32+
name: 'host',
33+
remotes: {
34+
remoteApp: 'remoteApp@http://localhost:3001/remoteEntry.js',
35+
},
36+
}),
37+
new HtmlWebpackPlugin({
38+
template: './public/index.html',
39+
}),
40+
],
41+
};
42+
43+
Update index.js:
44+
import React from 'react';
45+
import ReactDOM from 'react-dom';
46+
import App from './App';
47+
48+
ReactDOM.render(<App />, document.getElementById('root'));
49+
50+
Create App.js:
51+
import React, { Suspense } from 'react';
52+
53+
const RemoteButton = React.lazy(() => import('remoteApp/Button'));
54+
55+
const App = () => (
56+
<div>
57+
<h1>Host Application</h1>
58+
<Suspense fallback={<div>Loading...</div>}>
59+
<RemoteButton />
60+
</Suspense>
61+
</div>
62+
);
63+
64+
export default App;
65+
66+
Step 2: Set Up the Remote Application
67+
Initialize the Remote Application:
68+
npx create-react-app remote-app
69+
cd remote-app
70+
Install Dependencies:
71+
npm install webpack webpack-cli html-webpack-plugin webpack-dev-server @babel/core babel-loader @babel/preset-env @babel/preset-react --save-dev
72+
73+
Configure Webpack:
74+
Create webpack.config.js in the root of the remote-app directory:
75+
const HtmlWebpackPlugin = require('html-webpack-plugin');
76+
const ModuleFederationPlugin = require('webpack').container.ModuleFederationPlugin;
77+
78+
module.exports = {
79+
mode: 'development',
80+
devServer: {
81+
port: 3001,
82+
},
83+
module: {
84+
rules: [
85+
{
86+
test: /\.jsx?$/,
87+
loader: 'babel-loader',
88+
options: {
89+
presets: ['@babel/preset-react'],
90+
},
91+
},
92+
],
93+
},
94+
plugins: [
95+
new ModuleFederationPlugin({
96+
name: 'remoteApp',
97+
filename: 'remoteEntry.js',
98+
exposes: {
99+
'./Button': './src/Button',
100+
},
101+
}),
102+
new HtmlWebpackPlugin({
103+
template: './public/index.html',
104+
}),
105+
],
106+
};
107+
108+
Create a Component to Expose:
109+
Create src/Button.js:
110+
import React from 'react';
111+
112+
const Button = () => <button>Remote Button</button>;
113+
114+
export default Button;
115+
116+
Update index.js:
117+
import React from 'react';
118+
import ReactDOM from 'react-dom';
119+
import App from './App';
120+
121+
ReactDOM.render(<App />, document.getElementById('root'));
122+
123+
Create App.js:
124+
125+
import React from 'react';
126+
127+
const App = () => (
128+
<div>
129+
<h1>Remote Application</h1>
130+
<p>This is a remote application exposing a button component.</p>
131+
</div>
132+
);
133+
134+
export default App;
135+
136+
Step 3: Run the Applications
137+
Run the Host Application:
138+
cd host-app
139+
npm start
140+
141+
Run the Remote Application:
142+
cd remote-app
143+
npm start
144+
Summary
145+
Host Application (host-app): This application imports and uses the Button component from the remote application.
146+
Remote Application (remote-app): This application exposes a Button component via Module Federation.
147+
This setup demonstrates how you can create a Micro UI architecture with independent deployment and development using React and Webpack 5's Module Federation. You can expand this example by adding more components and configuring more advanced communication and state management between the micro frontends.
148+

0 commit comments

Comments
 (0)