Plugins de runtime
Runtime plugins let você change runtime behavior sem changing o core runtime itself.
Use them quando você precisa:
- rewrite remote URLs em runtime
- customize manifest requests
- patch script carregamento
- override shared resolution
- add recovery ou fallback behavior
- introduce novo carregamento de remote behavior
Se você apenas precisa register um plugin caminho em build configuração, consulte runtimePlugins. Se você need o full hook list, consulte Runtime Hooks.
Mental model
Uma runtime plugin é uma função esse retorna uma ModuleFederationRuntimePlugin:
O função form matters porque it lets você:
- accept opções
- keep estado em uma closure
- reuse o mesmo plugin logic across environments
Register um plugin
Você pode register runtime plugins em three places:
- Build time, por meio de
runtimePlugins - Runtime, on uma specific instance, por meio de
registerPlugins(...)ouinstance.registerPlugins(...) - Runtime, globally, por meio de
registerGlobalPlugins(...)
Build-time registration
Use build-time registration quando o plugin deve always ser present for esse host.
Runtime registration
Use runtime registration quando o plugin depends on user estado, environment, feature flags, ou dados fetched depois startup.
Global runtime registration
Use registerGlobalPlugins(...) quando você want o plugin para ser disponível para all future runtime instances, instead de apenas one current instance.
Este é best suited for instrumentação compartilhada, cross-app policy, ou host-wide defaults.
registerGlobalPlugins(...) deduplicates plugins by name. In practice, call it antes criação ou usando runtime instances so o global plugins são incorporated consistently.
Choose o right hook
Recipe: rewrite o resolved remote entry
afterResolve é o right hook quando o remote has já been resolved e você quiser change o final URL antes carregamento continues.
Este pattern é useful for:
- CDN indirection
- environment switching
- registry-backed routing
- domain normalization
Why este hook:
beforeRequesté earlier; o remote é não resolved yetafterResolvejá gives vocêremoteInfo.entry- changing
args.remoteInfo.entryhere keeps o original caminho, query, e hash intact
Um afterResolve rewrite changes carregamento em runtime behavior. It does não automatically rewrite o remote URL usado by tipo generation ou other build-time tooling. Se você rely on generated remote tipos, keep esse caminho aligned separadamente.
Recipe: customize manifest buscar
Use fetch quando o manifest request itself needs custom behavior.
Use fetch for:
- credentials
- auth headers
- manifest retries
- custom proxy logic
Se você need uma ready-made transport policy, see o built-em retry plugin.
Recipe: prefer uma host-owned dependência compartilhada
resolveShare é o hook for overriding o final shared módulo selection.
O important part: changing args.scope ou args.version alone é não enough. To change o actual winner, replace args.resolver.
Failure handling e shareStrategy
errorLoadRemote é o right place for runtime fallbacks.
One important detail: o behavior changes com shareStrategy.
version-firsteagerly carrega remote entries during startup para initialize dependências compartilhadasloaded-firstdefers carregamento de remote until o remote é actually usado
Esse means:
- com
version-first, um offline remote pode fail early comlifecycle: 'beforeLoadShare' - com
loaded-first, o mesmo remote geralmente fails later, quando code actually tries para usar it
Se você expect remotes para ser intermittently unavailable, pair errorLoadRemote com uma deliberate shareStrategy.
Versão-sensitive hooks
O advanced hooks evolve over time. For comum hooks like afterResolve, fetch, createScript, e loadEntry, prefer checking o installed runtime tipos quando você depend on newer arguments ou behaviors.
Esse é especialmente relevant quando você rely on:
- extra script attributes em
createScript - loader hook details passed em
fetchouloadEntry - menos-comum lifecycle hooks usado by built-em plugins