การสร้างแอปแบบสหพันธ์อย่างง่าย
พัฒนาตัวอย่างแอปพลิเคชันไมโครฟรอนต์เอนด์ที่ทำงานได้จริง โดยใช้ Module Federation
การสร้างแอปแบบสหพันธ์อย่างง่าย เป็นบทเรียน Micro Frontends Architecture with Module Federation ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Micro Frontends Architecture with Module Federation และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Our First Federated App
Welcome to building your first Micro Frontend application! In previous lessons, we learned about setting up Webpack for Module Federation and the core concepts of exposing and consuming modules.
Now, we'll bring those concepts together to build a basic, runnable example. Our goal is to create a Host Application that consumes a simple Button component from a separate Remote Application.
Remote App: The Component
First, let's create a simple React component in our Remote Application that we intend to share. This will be a basic button.
Imagine this component lives in src/components/MyButton.js within your remote project.
import React from 'react';
const MyButton = () => {
return (
<button style={{
padding: '10px 15px',
backgroundColor: '#61dafb',
color: 'white',
border: 'none',
borderRadius: '5px',
cursor: 'pointer'
}}>
Remote Button
</button>
);
};
export default MyButton;Remote App: Entry Point
This is the main entry file for our Remote Application. It will render its own content and also make the MyButton component available for exposure.
This code represents src/index.js in your Remote App.
import React from 'react';
import ReactDOM from 'react-dom';
import MyButton from './components/MyButton';
const App = () => {
return (
<div style={{ padding: '20px', border: '2px dashed #61dafb', margin: '10px' }}>
<h2>Remote App Content</h2>
<p>This part is rendered directly by the Remote App.</p>
<MyButton />
<p>The button above is our shared component.</p>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('root'));Remote App: Webpack Exposing
To make MyButton available to other applications, we configure the Remote App's webpack.config.js using the ModuleFederationPlugin. We specify its name, the filename for its manifest, and the exposes property.
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
// ... other webpack configuration
plugins: [
new ModuleFederationPlugin({
name: 'remoteApp',
filename: 'remoteEntry.js',
exposes: {
'./MyButton': './src/components/MyButton',
},
shared: {
react: { singleton: true, requiredVersion: '^17.0.2' },
'react-dom': { singleton: true, requiredVersion: '^17.0.2' },
},
}),
],
};Host App: Main Structure
Now, let's look at our Host Application. Initially, it's a standard React application. We'll modify its main entry file (src/index.js) to consume the remote component.
import React from 'react';
import ReactDOM from 'react-dom';
const App = () => {
return (
<div style={{ padding: '20px', border: '2px solid #50b0ff', margin: '10px' }}>
<h2>Host Application Content</h2>
<p>This content is rendered by the Host App.</p>
{/* The remote button will appear below this text */}
</div>
);
};
ReactDOM.render(<App />, document.getElementById('root'));Host App: Webpack Consuming
The Host App's webpack.config.js needs to know about the Remote App. We use the remotes property within ModuleFederationPlugin to define the remote and its entry file URL.
Remember to adjust the http://localhost:8081 URL to where your remote application is actually served.
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
// ... other webpack configuration
plugins: [
new ModuleFederationPlugin({
name: 'hostApp',
remotes: {
remoteApp: 'remoteApp@http://localhost:8081/remoteEntry.js', // Adjust port if needed
},
shared: {
react: { singleton: true, requiredVersion: '^17.0.2' },
'react-dom': { singleton: true, requiredVersion: '^17.0.2' },
},
}),
],
};Host App: Loading Remote Component
With the Webpack configuration in place, we can now dynamically import and render the MyButton component from our Remote App into the Host App. We use React's lazy and Suspense for this.
This code updates src/index.js in your Host App.
import React, { Suspense } from 'react';
import ReactDOM from 'react-dom';
// Dynamically import the remote component
const RemoteMyButton = React.lazy(() => import('remoteApp/MyButton'));
const App = () => {
return (
<div style={{ padding: '20px', border: '2px solid #50b0ff', margin: '10px' }}>
<h2>Host Application Content</h2>
<p>This content is rendered by the Host App.</p>
<Suspense fallback={<div>Loading Remote Button...</div>}>
<RemoteMyButton />
</Suspense>
<p>The button above is loaded from the Remote App!</p>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('root'));Running Both Applications
To see your federated application in action, you need to run both the Remote and Host applications concurrently. Each application will typically have its own development server.
- Step 1: Start the Remote App
Navigate to your Remote App's directory in a terminal and runnpm start(or similar command). - Step 2: Start the Host App
Open another terminal, navigate to your Host App's directory, and runnpm start. - Step 3: Open the Host App
Access the Host App's URL in your browser (e.g.,http://localhost:8080if that's its port).
Verify the Integration
When you open the Host App in your browser, you should see:
- Content rendered directly by the Host Application.
- A 'Loading Remote Button...' fallback message briefly.
- The 'Remote Button' component, visually styled and provided entirely by the Remote Application, appearing within the Host App's layout.
This demonstrates a basic, yet powerful, example of Module Federation in action!
Federated App Check
Let's check your understanding of building a simple federated app.
Recap: Federated Success!
Congratulations! You've successfully built a basic Micro Frontend application using Module Federation.
- We defined a component in a Remote App.
- Configured the Remote App's Webpack to expose that component.
- Set up the Host App's Webpack to consume from the remote.
- Used React's
lazyandSuspenseto dynamically load the remote component.
This foundational example opens the door to creating more complex, independently deployable Micro Frontends!
คำถามที่พบบ่อย
บทเรียน “การสร้างแอปแบบสหพันธ์อย่างง่าย” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างแอปแบบสหพันธ์อย่างง่าย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Micro Frontends Architecture with Module Federation ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างแอปแบบสหพันธ์อย่างง่าย”
พัฒนาตัวอย่างแอปพลิเคชันไมโครฟรอนต์เอนด์ที่ทำงานได้จริง โดยใช้ Module Federation คุณปฏิบัติ Micro Frontends Architecture with Module Federation ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Micro Frontends Architecture with Module Federation หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Micro Frontends Architecture with Module Federation บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างแอปแบบสหพันธ์อย่างง่าย” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Micro Frontends Architecture with Module Federation นี้ได้ไหม
ได้ บทเรียน Micro Frontends Architecture with Module Federation ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งค่าโครงการและการกำหนดค่า
- การแชร์โมดูลและคอมโพเนนต์
- การสร้างแอปแบบสหพันธ์อย่างง่าย
- การเรียกใช้และแก้ไขข้อบกพร่องของแอปแบบรวมภายในเครื่อง