Structure For Your Huge Angular Solutions: Packages, Monorepos and Micro Frontends @AngularDays2020 Berlin/Online

Structure For Your Huge Angular Solutions: Packages, Monorepos and Micro Frontends @AngularDays2020 Berlin/Online

15934fa2aa7b2ce21f091e9b7cffa856?s=128

Manfred Steyer

October 08, 2020
Tweet

Transcript

  1. @BASTAcon & @ManfredSteyer Structure For Your Huge Angular Solutions: Packages,

    Monorepos and Micro Frontends
  2. @ManfredSteyer Typical Module Structure Page ▪ 2 AppModule … …

    … SharedModule Root Module Feature Modules Shared Modules SharedModule
  3. @ManfredSteyer Contents • (npm-)Packages • Nx Monorepos • Strategic Design

    and DDD • Microfrontends
  4. @ManfredSteyer Manfred Steyer

  5. @ManfredSteyer npm Packages

  6. @ManfredSteyer Create Library with CLI >= 6 npm install -g

    @angular/cli ng new lib-project cd lib-project ng generate library logger-lib ng generate application playground-app ng serve --project playground-app ng build --project logger-lib
  7. @ManfredSteyer Folder Structure

  8. @ManfredSteyer Create Library with CLI >= 6 npm install -g

    @angular/cli ng new lib-project --create-application false cd lib-project ng generate library logger-lib ng generate application playground-app ng serve --project playground-app ng build --project logger-lib
  9. @ManfredSteyer Publishing

  10. @ManfredSteyer Publishing to npm Registry • Increment version in package.json

    • ng build --project logger-lib • npm publish --registry http://localhost:4873 • npm install --registry http://localhost:4873
  11. @ManfredSteyer Alternatives for setting the Registry • Global: npm set

    registry http://localhost:4873 • Default: registry.npmjs.org • npm get registry • Project: .npmrc in project root
  12. @ManfredSteyer npm Registries Nexus Artifactory Team Foundation Server Verdaccio npm

    i -g verdaccio verdaccio
  13. @ManfredSteyer DEMO

  14. @ManfredSteyer Advantages • Distribution • Versioning

  15. @ManfredSteyer Disadvantages • Distribution • Versioning ;-)

  16. @ManfredSteyer Disadvantages Distribution • Annoying within project • Prevents gritting

    further libs Versioning • Old versions • Conflicts • How to force devs to use latest version?
  17. @ManfredSteyer Monorepos

  18. @ManfredSteyer Monorepo Structure

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

    No burden with distributing libs Creating new libs: Adding folder Experience: Successfully used at Google, Facebook, …
  20. @ManfredSteyer Two Flavors • Like Workspaces/Solutions in different IDEs Project

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

  22. @ManfredSteyer Tooling & Generator https://nrwl.io/nx

  23. @ManfredSteyer Visualize Module Structure

  24. @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
  25. @ManfredSteyer Creating a Workspace npm install -g @angular/cli npm init

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

  27. @ManfredSteyer LAB

  28. @ManfredSteyer DDD in a nutshell

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

    design
  30. @ManfredSteyer

  31. @ManfredSteyer

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

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

    System Design Patterns & Practices
  34. @ManfredSteyer

  35. @ManfredSteyer Example Flight System Flight System

  36. @ManfredSteyer Booking Booking Check-in Check-in Boarding Boarding Luggage Luggage Example

    Sub-Domains
  37. @ManfredSteyer Booking Booking Luggage Luggage Boarding Boarding Check-in Check-in Context

    Map
  38. @ManfredSteyer Luggage Luggage Booking Booking Context Map Check-in Check-in Boarding

    Boarding Shared Kernel Shared Kernel
  39. @ManfredSteyer Booking Booking Check-in Check-in API API

  40. @ManfredSteyer Lots of approaches for cross-domain communication and more …

  41. @ManfredSteyer Booking Booking Boarding Boarding Shared Shared Feature Feature Feature

    Feature Feature Feature Feature Feature Feature Feature UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI Domain Domain Domain Domain Domain Domain Domain Domain Domain Domain Domain Domain Util Util Util Util Util Util 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.
  42. @ManfredSteyer Booking Booking Boarding Boarding Shared Shared Feature Feature API

    API Feature Feature Feature Feature Feature Feature Feature Feature UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI Domain Domain Domain Domain Domain Domain Domain Domain Domain Domain Domain Domain Util Util Util Util Util Util Util Util Util Util Util Util @ManfredSteyer
  43. @ManfredSteyer Booking Booking Boarding Boarding Shared Shared Feature Feature API

    API Feature Feature Feature Feature Feature Feature Feature Feature UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI UI Domain Domain Domain Domain Domain Domain Domain Domain Domain Domain Domain Domain Util Util Util Util Util Util Util Util Util Util Util Util @ManfredSteyer
  44. @ManfredSteyer Application Domain Model Infrastructure Application Domain Model Infrastructure Isolate

    your domain! Domain Domain e. g. data access Use case specific facades, state management Entities, biz logic
  45. @ManfredSteyer Alternatives to layering • e. g. Hexagonal Architecture, Clean

    Architecture • Anyway: We need to restrict access b/w libraries
  46. @ManfredSteyer DEMO

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

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

  49. @ManfredSteyer Booking App Booking App Check-in App Check-in App Boarding

    App Boarding App Luggage App Luggage App Microfrontends
  50. @ManfredSteyer

  51. @ManfredSteyer Booking Booking Boarding Boarding Shared Shared Feature Feature Feature

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

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

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

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

    decisions Own technology descisions
  56. @ManfredSteyer Integration via Hyperlinks

  57. @ManfredSteyer

  58. @ManfredSteyer

  59. @ManfredSteyer Integration via Shell

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

    Shell
  61. @ManfredSteyer Loading Separately Compiled SPAs const script = document.createElement('script'); script.src

    = 'assets/micro-app.bundle.js'; document.body.appendChild(script); const clientA = document.createElement('client-a'); clientA['visible'] = true; document.body.appendChild(clientA);
  62. @ManfredSteyer

  63. @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!
  64. @ManfredSteyer Webpack 5 Module Federation Shell (Host) Microfrontend (Remote) //

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

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

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

  68. @ManfredSteyer Example for SemVer and Fallback • Shell: my-lib: ^10.0.0

    • MFE1: my-lib: ^10.1.1 • MFE2: my-lib: ^9.0.0 Result: • Shell and MFE1 share ^10.1.1 • MFE2 falls back to its own version ^9.0.0
  69. @ManfredSteyer However … • Shell: my-lib: ^10.0.0 • MFE1: my-lib:

    ^10.1.1 + MFE1: dynamic Module Federation Result: • Shell uses ^10.0.0 • MFE1 falls back to its own version ^10.1.1
  70. @ManfredSteyer However … • Shell: my-lib: ^10.1.1 • MFE1: my-lib:

    ^10.0.0 + MFE1: dynamic Module Federation Result: • Shell and MFE1 share ^10.1.1
  71. @ManfredSteyer Configuring Singletons shared: { "my-lib": { singleton: true, strictVersion:

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

    <11.1.1" } }
  73. @ManfredSteyer

  74. @ManfredSteyer Well … Webpack 5 is currently beta Shown examples:

    PoC w/ custom webpack conf + patched CLI lib CLI: Not before version 11 (fall 2020) Custom Builder (e. g. ngx-build-plus)
  75. @ManfredSteyer Free eBook ANGULARarchitects.io/book Updated for Module Federation and Alternatives

  76. @ManfredSteyer Choosing a Solution

  77. @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 Not a good fit for public web sites
  78. @ManfredSteyer d Slides & Examples Public: Frankfurt, Munich, Vienna In-House:

    everywhere http://softwarearchitekt.at/workshops