0Pricing
Micro Frontends Architecture with Module Federation · Aula

Dependências Compartilhadas e Gerenciamento de Versões

Aprenda como o Module Federation compartilha bibliotecas entre microfrontends, como funcionam os singletons e a negociação de versões e quais práticas recomendadas evitam pacotes duplicados e falhas em tempo de execução.

Dependências Compartilhadas e Gerenciamento de Versões é uma aula grátis de Micro Frontends Architecture with Module Federation no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Micro Frontends Architecture with Module Federation, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Micro Frontends Architecture with Module Federation inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

Why Sharing Matters

In a Module Federation setup every remote can bundle its own copy of react, react-dom, or a design system. Without coordination, a single page may load three copies of React.

Shared dependencies let independently deployed apps agree on a single instance of a library at runtime, cutting payload and avoiding state bugs.

The shared Key

Both host and remotes declare a shared block in their ModuleFederationPlugin config. Webpack then negotiates which version actually loads.

new ModuleFederationPlugin({
  name: 'host',
  shared: {
    react: { singleton: true },
    'react-dom': { singleton: true }
  }
})

Singletons Explained

singleton: true forces one and only one copy across the whole app. This is essential for libraries that hold internal state, like React (hooks) or a router.

Without it, two React instances cause the dreaded Invalid hook call error.

Version Negotiation

Each shared module declares a requiredVersion. At load time Module Federation picks the highest compatible version among all that are offered.

shared: {
  react: {
    singleton: true,
    requiredVersion: '^18.2.0'
  }
}

Strict Version Mismatch

When a remote needs a version incompatible with the loaded singleton, Module Federation logs a warning and uses the existing one. With strictVersion: true it instead throws, surfacing the mismatch early.

shared: {
  react: { singleton: true, strictVersion: true, requiredVersion: '18.2.0' }
}

Eager vs Lazy Sharing

By default shared modules load asynchronously, which requires a bootstrap entry. Setting eager: true bundles the dependency into the initial chunk so no async boundary is needed.

Use eager only for the host shell; eager everywhere defeats the purpose of sharing.

shared: {
  react: { singleton: true, eager: true }
}

The bootstrap Pattern

Because shared modules resolve asynchronously, the standard pattern splits the entry into index.js (just an import) and bootstrap.js (the real app). This defers execution until the share scope is ready.

// index.js
import('./bootstrap');

// bootstrap.js
import App from './App';
// ... mount App

Sharing a Design System

A shared component library should usually be a singleton too, so theming context and CSS-in-JS instances are not duplicated.

shared: {
  '@acme/ui': { singleton: true, requiredVersion: '^3.0.0' }
}

Auto-Sharing from package.json

Instead of listing versions by hand, you can pass an array and let the plugin read versions from package.json. Tools like the Module Federation Enhanced plugin can even auto-share all dependencies.

shared: ['react', 'react-dom', 'react-router-dom']

Diagnosing Duplicate Copies

To confirm sharing works, inspect the Network tab: you should see one vendor chunk for the shared lib. Bundle analyzers and the runtime share scope (__webpack_share_scopes__.default) help debug.

Best Practices Summary

  • Singleton for stateful libs (React, router, stores).
  • Set requiredVersion to align teams.
  • Keep eager only on the host shell.
  • Audit bundles regularly for accidental duplicates.

Quick Check

Which option ensures only one instance of React loads across all micro frontends?

Recap

You learned how Module Federation shares dependencies: singletons for stateful libs, version negotiation via requiredVersion/strictVersion, eager vs lazy loading, and the bootstrap pattern. Proper sharing slashes bundle size and prevents subtle runtime bugs.

Perguntas Frequentes

A aula “Dependências Compartilhadas e Gerenciamento de Versões” é grátis?

Sim — o texto completo de “Dependências Compartilhadas e Gerenciamento de Versões” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Micro Frontends Architecture with Module Federation, atualize para CoddyKit PRO. O curso de Micro Frontends Architecture with Module Federation inclui 4 aulas no total.

O que vou aprender em “Dependências Compartilhadas e Gerenciamento de Versões”?

Aprenda como o Module Federation compartilha bibliotecas entre microfrontends, como funcionam os singletons e a negociação de versões e quais práticas recomendadas evitam pacotes duplicados e falhas… Você pratica Micro Frontends Architecture with Module Federation com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Micro Frontends Architecture with Module Federation?

Nenhuma experiência prévia é necessária. Micro Frontends Architecture with Module Federation no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Dependências Compartilhadas e Gerenciamento de Versões”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Micro Frontends Architecture with Module Federation?

Sim. Cada aula de Micro Frontends Architecture with Module Federation inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Monorrepositórios versus polirrepositórios
  2. Desafios organizacionais e soluções
  3. Futuro dos microfrontends e da federação
  4. Dependências Compartilhadas e Gerenciamento de Versões
← Voltar para Micro Frontends Architecture with Module Federation