싱글턴 모듈 및 버전 관리
모듈이 하나의 인스턴스만 로드하도록 보장하고 버전 충돌을 관리하는 방법을 이해합니다.
싱글턴 모듈 및 버전 관리은(는) CoddyKit의 무료 Micro Frontends Architecture with Module Federation 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Micro Frontends Architecture with Module Federation 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Micro Frontends Architecture with Module Federation 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What are Singleton Modules?
In Micro Frontend architectures, a singleton module is a piece of code or a library that should only ever exist as a single instance across your entire application, even if multiple Micro Frontends try to load it.
Think of it as a unique resource that all parts of your system need to share consistently.
- Ensures a single source of truth.
- Prevents conflicting states or behaviors.
Why Singletons Matter for MFEs
Imagine multiple Micro Frontends (MFEs) all loading their own copy of a UI library like React, or a state management library like Redux.
Without singletons, you could end up with:
- Multiple instances of React Context, breaking component communication.
- Separate Redux stores, leading to inconsistent application state.
- Increased bundle size due to duplicate code.
Singletons solve these issues by guaranteeing a unified dependency.
Implementing Singletons with MF
Module Federation makes it easy to declare shared modules as singletons using the singleton: true option in your Webpack configuration.
When this is set, Webpack ensures that only one version of the specified module is loaded and used by all federated applications (hosts and remotes) at runtime.
Code: Configuring a Singleton
Here's how you'd configure a shared module, like react, to be a singleton in your webpack.config.js:
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
// ... other Webpack config
plugins: [
new ModuleFederationPlugin({
name: 'host_app',
remotes: {},
shared: {
react: {
singleton: true, // Only one React instance allowed
requiredVersion: '^18.0.0',
},
'react-dom': {
singleton: true, // Only one ReactDOM instance allowed
requiredVersion: '^18.0.0',
},
},
}),
],
};Understanding Shared Module Versioning
Beyond ensuring a single *instance* (singleton), you also need to manage which *version* of a shared module gets loaded.
What if one MFE needs React v17 and another needs React v18? Module Federation has mechanisms to handle these version conflicts and ensure compatibility or gracefully fail.
`requiredVersion` for Specificity
The requiredVersion option in the shared configuration allows you to specify the semantic version (semver) range that your application expects for a shared module.
Webpack will try to find a compatible version among the host and remote applications. If a compatible version is found, it's used. If not, it might load a fallback or throw an error, depending on other settings.
Code: Specifying Required Versions
Here's how you might define a requiredVersion for a shared library:
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
// ...
plugins: [
new ModuleFederationPlugin({
// ...
shared: {
lodash: {
singleton: false,
requiredVersion: '^4.17.21', // Any version compatible with 4.17.21
},
moment: {
singleton: true,
requiredVersion: '2.29.1', // Exactly version 2.29.1
},
},
}),
],
};`strictVersion: true` for Strictness
While requiredVersion guides version selection, strictVersion: true enforces it rigorously.
If strictVersion: true is enabled and a remote module requests a version that is incompatible with the host's requiredVersion (or the version already loaded), Module Federation will throw an error and prevent the application from loading. This prevents unexpected behavior from version mismatches.
Code: Enforcing Strict Versioning
Combining singleton, requiredVersion, and strictVersion for critical dependencies:
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
// ...
plugins: [
new ModuleFederationPlugin({
// ...
shared: {
react: {
singleton: true,
requiredVersion: '^18.0.0',
strictVersion: true, // Fail if versions are incompatible
},
// ... other shared modules
},
}),
],
};Best Practices for Shared Dependencies
To effectively manage singleton modules and versioning:
- Use Semantic Versioning (SemVer): Adhere to major.minor.patch for all shared modules.
- Communicate: Ensure teams are aware of shared module updates and version requirements.
- Test Compatibility: Regularly test your federated applications to ensure shared modules work across all remotes and hosts.
- Be Strategic: Not all shared modules need to be singletons or have strict versioning. Apply these settings thoughtfully based on the module's criticality.
Quick Check: Singleton & Versioning
When configuring shared modules in Module Federation, what are the primary benefits of using singleton: true and strictVersion: true?
Recap & Next Steps
You've now learned about singleton modules and versioning in Module Federation!
- Singleton modules ensure only one instance of a shared dependency exists across your federated application, crucial for stateful libraries.
- The
requiredVersionandstrictVersionoptions help manage compatibility and prevent issues arising from conflicting module versions.
These techniques are vital for building robust and predictable Micro Frontend applications.
자주 묻는 질문
“싱글턴 모듈 및 버전 관리” 강의는 무료인가요?
네 — “싱글턴 모듈 및 버전 관리” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Micro Frontends Architecture with Module Federation 강의 전체를 잠금 해제할 수 있습니다. Micro Frontends Architecture with Module Federation 강의에는 총 4개의 강의가 포함되어 있습니다.
“싱글턴 모듈 및 버전 관리”에서 뭘 배우나요?
모듈이 하나의 인스턴스만 로드하도록 보장하고 버전 충돌을 관리하는 방법을 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 Micro Frontends Architecture with Module Federation을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Micro Frontends Architecture with Module Federation을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Micro Frontends Architecture with Module Federation은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“싱글턴 모듈 및 버전 관리” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Micro Frontends Architecture with Module Federation 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Micro Frontends Architecture with Module Federation 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 공유 종속성 사용
- 싱글턴 모듈 및 버전 관리
- 동적 모듈 로딩
- 원격 앱 간 상태와 유틸리티 공유