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

Architectures for huge Angular Applications: Packages, Monorepos, and Microfrontends @AngularDays Berlin 2019

Architectures for huge Angular Applications: Packages, Monorepos, and Microfrontends @AngularDays Berlin 2019

Manfred Steyer

October 09, 2019
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @BASTAcon & @ManfredSteyer Architectures for huge Angular Applications: Packages, Monorepos,

    and Microfrontends Manfred Steyer SOFTWAREarchitekt.at ManfredSteyer
  2. @ManfredSteyer Über mich • Manfred Steyer SOFTWAREarchitekt.at • Angular Trainings

    and Consultancy • Google Developer Expert (GDE) • Trusted Collaborator for Angular Page ▪ 4 Manfred Steyer Public: Frankfurt, Munich, Vienna In-House: Everywhere! http://www.softwarearchitekt.at/workshops
  3. @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
  4. @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
  5. @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
  6. @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
  7. @ManfredSteyer Disadvantages Distribution • Annoying within project • Prevents gritting

    further libs Versioning • Old versions • Conflicts • How to force devs to use latest version?
  8. @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, …
  9. @ManfredSteyer Two Flavors • Like Workspaces/Solutions in different IDEs Project

    Monorepo • E. g. used at Google or Facebook Company-wide Monorepo
  10. @ManfredSteyer Booking Boarding Shared Feature API Feature Feature Feature Feature

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

    UI UI UI UI UI UI UI UI UI Domain Domain Domain Util Util Util Util Util Util @ManfredSteyer Domain
  12. @ManfredSteyer Application Domain Infrastructure Isolate your domain! Domain Domain Infrastructure

    e. g. data access Application Use case specific facades, state management Entities, biz logic
  13. @ManfredSteyer Further Features of Nx • Define folders for libraries

    • Restrict which apps/libs can access which other libs • Just recompile changed apps • Visualize module structure and dependencies • Scaffold Boilerplate
  14. @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
  15. @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
  16. @ManfredSteyer Finegrained Libraries • Unit of recompilation • Unit of

    retesting • Access restrictions • Prevent Cycles • Information Hiding • Future replacement for NgModules?
  17. @ManfredSteyer Catalog Ordering Shared Feature Feature Feature Feature Feature …

    … … … … … … … … @ManfredSteyer e-Procurement App Deployment Monolith
  18. @ManfredSteyer Catalog Ordering Shared Feature Feature Feature Feature Feature …

    … … … … … … … … @ManfredSteyer Catalog App Ordering App Option 1: One App per Domain
  19. @ManfredSteyer Catalog Ordering Shared Feature Feature Feature Feature Feature …

    … … … … … … … … @ManfredSteyer Catalog App Ordering App Option 2: One Monorepo per Domain Publish shared libs seperately via npm
  20. @ManfredSteyer UI Composition w/ Hyperlinks µApp SPA µApp SPA µApp

    SPA <a href="…">…</a> <a href="…">…</a>
  21. @ManfredSteyer µService Composing on Server-Side µFrontend µFrontend µFrontend Server-side process

    ▪ Works well for traditional web applications ▪ Optimized payload sizes ▪ Does not work well for SPAs
  22. @ManfredSteyer µService Providing a (SPA based) Shell µApp µApp µApp

    Shell ▪ iframes ▪ Bootstrapping several SPAs ▪ + Lazy Loading
  23. @ManfredSteyer Wrap µApps into Web Components • Not a typical

    web component! • But: Unified API for Shell • Nesting • At least: Use Shadow DOM
  24. @ManfredSteyer µService µFrontend3 µFrontend 2 µFrontend1 Shell Web Components for

    Macro-Architecture Shared Widget Shared Widget Shared Widget Web Components for shared Widgets
  25. @ManfredSteyer Lazy Loading const script = document.createElement('script'); script.src = 'assets/micro-app-bundle.js';

    document.body.appendChild(script); const clientA = document.createElement('micro-app'); clientA['visible'] = true; document.body.appendChild(clientA);
  26. @ManfredSteyer Communication b/w Micro Apps (Web Components) <client-a [state]="appState" (message)="handle($event)"></client-a>

    <client-b [state]="appState" (message)="handle($event)"></client-b> JavaScript in Shell
  27. @ManfredSteyer Bundles Custom Element 1 Custom Element 2 Custom Element

    3 Libraries: Angular, Bootstrap, … Libraries: Angular, Bootstrap, …
  28. @ManfredSteyer Where Downleveling can help ✓ Widgets based on @angular/core,

    @angular/common  Libs like @angular/forms or @angular/router
  29. @ManfredSteyer Sharing Libs Custom Element 1 Custom Element 2 Custom

    Element 3 Libraries: Angular, RxJS, … UMD ngx-build-plus Drawbacks: Complexity, no isolation
  30. @ManfredSteyer Some General Advice Shared state, navigation b/w apps Hyperlinks

    Legacy Apps or *very very* strong isolation? iframes Separate Deployment/ mix Technologies? Web Components Monolith w/ Libs and Monorepo little much yes no yes no Not a good fit for public web sites
  31. @ManfredSteyer Blog > SOFTWAREarchitekt.at • A Software Architect's Approach Towards

    Using Angular (And SPAs In General) For Microservices Aka Microfrontends • A Lightweight And Solid Approach Towards Micro Frontends (Micro Service Clients) With Angular And/Or Other Frameworks • Micro Apps With Web Components Using Angular Elements
  32. @ManfredSteyer Conclusion Monorepo: Subdivide App Strategic DDD: Find Domains Domain:

    Folder w/ Libs Nx: Access Restrictins App per Domain: Microfrontend Hyperlinks vs. Shell? You need to evaluate!