마이크로 프론트엔드(Micro Frontend)는 마이크로서비스 개념을 프론트엔드에 적용한 아키텍처 패턴이다. 대규모 웹 애플리케이션을 독립적으로 개발·배포 가능한 작은 프론트엔드 단위로 분리한다.
핵심 원칙
1. 기술 스택 독립성: 각 팀이 다른 프레임워크 선택 가능
2. 격리된 코드베이스: 런타임에만 통합
3. 독립 배포: 전체 재배포 없이 부분 업데이트
4. 팀 자율성: 엔드-투-엔드 기능 팀 구성
구현 방식
Module Federation (Webpack 5)
javascript
// host/webpack.config.js (Shell)
new ModuleFederationPlugin({
name: 'host',
remotes: {
header: 'header@http://localhost:3001/remoteEntry.js',
cart: 'cart@http://localhost:3002/remoteEntry.js',
},
});
// header/webpack.config.js (Remote)
new ModuleFederationPlugin({
name: 'header',
filename: 'remoteEntry.js',
exposes: {
'./Header': './src/Header',
},
shared: ['react', 'react-dom'], // 공유 의존성
});
// host/App.tsx
const Header = lazy(() => import('header/Header'));
iframe 방식
html
<!-- 단순하지만 성능 저하 -->
<iframe src="https://team-a.example.com/widget" />
javascript
// 각 팀이 웹 컴포넌트 배포
// <team-a-header>, <team-b-product-list>
통신 방식
javascript
// Custom Events로 MF 간 통신
// 발신 (cart MF)
window.dispatchEvent(new CustomEvent('cart:updated', {
detail: { itemCount: 3 }
}));
// 수신 (header MF)
window.addEventListener('cart:updated', ({ detail }) => {
setCartCount(detail.itemCount);
});
관련 개념