Structure For Your Huge Angular Solutions: Packages, Monorepos and Micro Frontends @AngularDays

15934fa2aa7b2ce21f091e9b7cffa856?s=47 Manfred Steyer
September 04, 2020
230

Structure For Your Huge Angular Solutions: Packages, Monorepos and Micro Frontends @AngularDays

15934fa2aa7b2ce21f091e9b7cffa856?s=128

Manfred Steyer

September 04, 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

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

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

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

  39. @ManfredSteyer Booking Check-in API

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

  41. @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.
  42. @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
  43. @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
  44. @ManfredSteyer Application Domain Model Infrastructure Isolate your 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 Check-in App Boarding App Luggage App Microfrontends

  50. @ManfredSteyer

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

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

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

    … … … … … … … … Booking App Boarding App Option 1: One App per Domain Monorepo
  54. @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
  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

  69. @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)
  70. @ManfredSteyer Free eBook ANGULARarchitects.io/book Updated for Module Federation and Alternatives

  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 Not a good fit for public web sites