การกำหนดค่าการพึ่งพาร่วม
เรียนรู้ว่าตัวเลือก shared ใน Module Federation ช่วยให้แอปโฮสต์และแอประยะไกลใช้ไลบรารีอย่าง React ร่วมกันได้อย่างไร โดยหลีกเลี่ยงการดาวน์โหลดซ้ำและความขัดแย้งของเวอร์ชัน
การกำหนดค่าการพึ่งพาร่วม เป็นบทเรียน Micro Frontends Architecture with Module Federation ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Micro Frontends Architecture with Module Federation และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Duplication Problem
Without coordination, a host and each remote would each bundle their own copy of React, Lodash, and other libraries. Users would download the same code many times.
The shared option solves this.
What shared Does
The shared key in ModuleFederationPlugin tells webpack which dependencies should be loaded once and reused across federated apps at run time.
Basic shared Syntax
The simplest form lists package names as an array. Webpack then negotiates a single shared instance at run time.
new ModuleFederationPlugin({
name: "host",
shared: ["react", "react-dom"]
});Object Form for Options
The object form gives fine-grained control per dependency, such as marking it required or singleton.
shared: {
react: { singleton: true, requiredVersion: "^18.0.0" },
"react-dom": { singleton: true }
}The singleton Option
singleton: true forces every app to use ONE instance of the library. This is essential for React, whose hooks break if two copies load at once.
requiredVersion and Negotiation
Each app declares a requiredVersion. At run time Module Federation picks the highest version that satisfies everyone. If versions are incompatible, it warns or falls back.
shared: {
react: { singleton: true, requiredVersion: "^18.2.0" }
}The eager Option
eager: true bundles the shared dependency into the initial chunk instead of loading it asynchronously. Useful for the host shell but increases initial bundle size.
shared: {
react: { singleton: true, eager: true }
}Reading from package.json
To avoid hardcoding versions, you can derive them from your dependencies, keeping shared config in sync with what you actually install.
const deps = require("./package.json").dependencies;
shared: {
react: { singleton: true, requiredVersion: deps.react }
}strictVersion
strictVersion: true makes Module Federation throw instead of falling back when no compatible version is found — surfacing mismatches early during testing.
Common Pitfalls
Watch out for:
- Forgetting
singletonon React, causing hook errors - Mismatched major versions across remotes
- Overusing
eager, bloating the first load
Verifying Sharing Works
Open the browser network tab: if React loads only once across host and remotes, sharing is working. The webpack build log also reports shared-module resolution.
Quick Check
Test your shared-dependency knowledge.
Recap
You configured shared dependencies:
sharedavoids duplicate libraries across apps- Use the object form for per-package control
singletonis critical for React and React-DOMrequiredVersiondrives run-time negotiation- Use
eagerandstrictVersiondeliberately
Good sharing keeps federated apps fast and conflict-free.
คำถามที่พบบ่อย
บทเรียน “การกำหนดค่าการพึ่งพาร่วม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การกำหนดค่าการพึ่งพาร่วม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Micro Frontends Architecture with Module Federation ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกำหนดค่าการพึ่งพาร่วม”
เรียนรู้ว่าตัวเลือก shared ใน Module Federation ช่วยให้แอปโฮสต์และแอประยะไกลใช้ไลบรารีอย่าง React ร่วมกันได้อย่างไร โดยหลีกเลี่ยงการดาวน์โหลดซ้ำและความขัดแย้งของเวอร์ชัน คุณปฏิบัติ Micro Frontends Architecture with Module Federation ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Micro Frontends Architecture with Module Federation หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Micro Frontends Architecture with Module Federation บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การกำหนดค่าการพึ่งพาร่วม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Micro Frontends Architecture with Module Federation นี้ได้ไหม
ได้ บทเรียน Micro Frontends Architecture with Module Federation ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทบทวนพื้นฐาน Webpack
- รู้จัก Module Federation
- แอปพลิเคชันโฮสต์และรีโมต
- การกำหนดค่าการพึ่งพาร่วม