Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Angular-basierte Micro Frontends mit Module Federation @API Summit

Angular-basierte Micro Frontends mit Module Federation @API Summit

15934fa2aa7b2ce21f091e9b7cffa856?s=128

Manfred Steyer
PRO

June 20, 2022
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @BASTAcon & @ManfredSteyer Angular-basierte Micro Frontends mit Module Federation

  2. @ManfredSteyer Goal Learn how to build Frontend Moduliths and Micro

    Frontends
  3. @ManfredSteyer Contents • Frontend Moduliths • Nx Monorepos • Strategic

    Design and DDD • Micro Frontends • Module Federation • Web Components
  4. @ManfredSteyer Manfred Steyer

  5. @ManfredSteyer

  6. @ManfredSteyer Monorepo Structure

  7. @ManfredSteyer Advantages Everyone uses the latest versions No version conflicts

    No burden with distributing libs
  8. @ManfredSteyer Two Flavors • Like Workspaces/Solutions in different IDEs Project

    Monorepo • E. g. used at Google or Facebook Company-wide Monorepo
  9. @ManfredSteyer Moving back and forth Npm Registry

  10. @ManfredSteyer Tooling & Generator https://nrwl.io/nx Smart, Fast and Extensible Build

    System
  11. @ManfredSteyer Visualize Module Structure

  12. @ManfredSteyer Creating a Workspace npm install -g @angular/cli ng new

    workspace cd workspace ng generate app my-app ng generate lib my-lib ng serve --project my-app ng build --project my-app
  13. @ManfredSteyer Creating a Workspace npm install -g @angular/cli npm init

    nx-workspace workspace cd workspace ng generate app my-app ng generate lib my-lib ng serve --project my-app ng build --project my-app
  14. @ManfredSteyer DEMO

  15. @ManfredSteyer DDD in a nutshell

  16. @ManfredSteyer Methodology for bridging the gap b/w requirements and architecture/

    design
  17. @ManfredSteyer

  18. @ManfredSteyer

  19. @ManfredSteyer Domain Driven Design Strategic Design Tactical Design Decomposing a

    System Design Patterns & Practices
  20. @ManfredSteyer Domain Driven Design Strategic Design Tactical Design Decomposing a

    System Design Patterns & Practices
  21. @ManfredSteyer

  22. @ManfredSteyer Example Flight System

  23. @ManfredSteyer Booking Check-in Boarding Luggage Example Sub-Domains

  24. @ManfredSteyer Booking Boarding Shared Feature Feature Feature Feature Feature UI

    UI UI UI UI UI UI UI UI Domain Domain Domain Domain Domain Domain Util Util Util Util Util Util Enterprise Monorepo Patterns, Nrwl 2018: https://tinyurl.com/y2jjxld7 @ManfredSteyer Shared Kernel (if really needed) & other libs Smart Comp. Dumb Comp.
  25. @ManfredSteyer Booking Boarding Shared Feature API Feature Feature Feature Feature

    UI UI UI UI UI UI UI UI UI Domain Domain Domain Domain Domain Domain Util Util Util Util Util Util @ManfredSteyer
  26. @ManfredSteyer DEMO

  27. @ManfredSteyer

  28. @ManfredSteyer Finegrained Libraries • Unit of recompilation • Unit of

    retesting • Access restrictions • Information Hiding • Easy: Just ng g lib … • Future replacement for NgModules?
  29. @ManfredSteyer Micro Frontends? Short outlook

  30. @ManfredSteyer Booking App Check-in App Boarding App Luggage App Microfrontends

  31. @ManfredSteyer

  32. @ManfredSteyer Booking Boarding Shared Feature Feature Feature Feature Feature …

    … … … … … … … … @ManfredSteyer Flight App Deployment Monolith
  33. @ManfredSteyer Booking Boarding Shared Feature Feature Feature Feature Feature …

    … … … … … … … … Booking App Boarding App Microfrontends
  34. @ManfredSteyer Booking Boarding Shared Feature Feature Feature Feature Feature …

    … … … … … … … … Booking App Boarding App Option 1: One App per Domain Monorepo
  35. @ManfredSteyer Booking Boarding Shared Feature Feature Feature Feature Feature …

    … … … … … … … … Booking App Boarding App Option 2: One Monorepo per Domain Publish shared libs seperately via npm Repository n Repository 2 Repository 1
  36. @ManfredSteyer Benefits Autonomous Teams Separate Development Separate Deployment Own architecture

    decisions Own technology descisions
  37. @ManfredSteyer Integration via Hyperlinks

  38. @ManfredSteyer UI Composition w/ Hyperlinks µApp SPA µApp SPA µApp

    SPA
  39. @ManfredSteyer

  40. @ManfredSteyer

  41. @ManfredSteyer Integration via Shell

  42. @ManfredSteyer µService Providing a (SPA based) Shell µApp µApp µApp

    Shell
  43. @ManfredSteyer

  44. @ManfredSteyer Idea const Component = import('http://other-app/xyz') Does not work with

    webpack/ Angular CLI Even lazy parts must be known at compile time!
  45. @ManfredSteyer Webpack 5 Module Federation Shell (Host) Microfrontend (Remote) //

    Maps Urls in // webpack config remotes: { mfe1: "http://..." } // Expose files in // webpack config exposes: { Cmp: './my.cmp.ts' } import('mfe1/Cmp')
  46. @ManfredSteyer How to Get the Microfrontend's URL? Shell (Host) Microfrontend

    (Remote) RemoteEntrypoint.js <script src="…"></script>
  47. @ManfredSteyer How to Share Libs? Shell (Host) Microfrontend (Remote) shared:

    [ "@angular/core", "…" ] shared: [ "@angular/core", "…" ]
  48. @ManfredSteyer

  49. @ManfredSteyer Default Behavior Selecting the highest compatible version 10.0 10.1

  50. @ManfredSteyer Default Behavior Conflict: No highest compatible version 11.0 10.1

  51. @ManfredSteyer Configuring Singletons shared: { "my-lib": { singleton: true }

    } 11.0 10.1
  52. @ManfredSteyer Configuring Singletons shared: { "my-lib": { singleton: true, strictVersion:

    true // Error instead of warning! } } 11.0 10.1
  53. @ManfredSteyer Relaxing Version Requirements shared: { "my-lib": { requiredVersion: ">=1.0.1

    <11.1.1" } }
  54. @ManfredSteyer

  55. @ManfredSteyer

  56. @ManfredSteyer 1) npm i @angular-architects/module-federation -D 2) ng g @angular-architects/module-federation:init

    3) Adjust generated configuration 4) ng serve
  57. @ManfredSteyer

  58. @ManfredSteyer

  59. @ManfredSteyer Wrap them into Web Components Angular App (MFE) React

    App (MFE)
  60. @ManfredSteyer

  61. @ManfredSteyer const Component = await import('other-app/cmp');

  62. @ManfredSteyer const main = await import('other-app/main'); main.bootstrap();

  63. @ManfredSteyer const main = await import('other-app/main'); main.bootstrap(); const rootElm =

    document.createElement('app-root') document.body.appendChild(rootElm);
  64. @ManfredSteyer await import('other-app/main'); // Self-Bootstrapping const rootElm = document.createElement('app-root') document.body.appendChild(rootElm);

  65. @ManfredSteyer await import('other-app/main'); const rootElm = document.createElement('app-root') document.body.appendChild(rootElm); WrapperComponent

  66. @ManfredSteyer

  67. @ManfredSteyer https://red-ocean-0fe4c4610.azurestaticapps.net

  68. @ManfredSteyer Challanges • Bundle Size • Multiple Routers • Bootstrapping

    Several Angular Instances • Share Platform-Object when same version is reused • Share ngZone
  69. @ManfredSteyer

  70. @ManfredSteyer DEMO

  71. @ManfredSteyer Choosing a Solution

  72. @ManfredSteyer Some General Advice Shared state, navigation b/w apps Hyperlinks

    Legacy Apps or *very very* strong isolation? iframes Separate Deployment/ mix Technologies? Load Bundles on Demand Monolith little much yes no yes no Module Federation