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

Structure for you big Angular applicatoins: npm-Packages, Monorepos, and Micro Apps @AngularDays 2019, Munich

Structure for you big Angular applicatoins: npm-Packages, Monorepos, and Micro Apps @AngularDays 2019, Munich

Manfred Steyer

March 20, 2019
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer Structure for you big Angular applicatoins: npm-Packages, Monorepos, and

    Micro Apps Manfred Steyer SOFTWAREarchitekt.at ManfredSteyer
  2. @ManfredSteyer About me… • Manfred Steyer SOFTWAREarchitekt.at • Angular Trainings

    and Consultancy • Google Developer Expert (GDE) • Trusted Collaborator for Angular Team Page ▪ 4 Manfred Steyer Public: Munich, Frankfurt, Stuttgart, Vienna In-House: everywhere http://softwarearchitekt.at/workshops
  3. @ManfredSteyer Why Packages? Sub Project Sub Project Sub Project Sub

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

    @angular/cli ng new lib-project cd lib-project ng generate library flight-booking-lib ng generate application playground-app ng serve --project playground-app ng build --project flight-booking-lib
  5. @ManfredSteyer Publishing to npm Registry • Increment version in package.json

    • ng build --project flight-booking-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? Decoupling • What if lib authors == app authors?
  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 Advantages Sub Project Sub Project Sub Project Sub Project

    Sub Project Sub Project Sub Project Sub Project
  11. @ManfredSteyer Disadvantages Sub Project Sub Project Sub Project Sub Project

    Sub Project Sub Project Sub Project Sub Project Team A Team B Contract Maintainability? One Architecture/ One Framework
  12. @ManfredSteyer UI Composition w/ Hyperlinks µApp SPA µApp SPA µApp

    SPA <a href="…>…</a> <a href="…>…</a>
  13. @ManfredSteyer ✓ Simple  Loosing State  Load a new

    Application  Consistent Look and Feel
  14. @ManfredSteyer µService Providing a (SPA based) Shell µApp µApp µApp

    Shell ▪ iframes ▪ Bootstrapping several SPAs ▪ Consider Lazy Loading!
  15. @ManfredSteyer Common Interface for µApps • Shell should not know

    details of µApp and used Frameworks! • µApp provides events • µApp consumes events • Wrap µApp as Web Component?
  16. @ManfredSteyer Selected Polyfills • webcomponents-loader.js • Loads Polyfills on demand

    • Custom Elements • Shadom DOM • Templates • native-shim.js • Needed for browsers that DO support polyfills • When downleveled to ES5 • Alternative: Differential Loading (providing ES5 + ES2015 bundles)
  17. @ManfredSteyer API class CustomerSelector extends HTMLElement { constructor() { super();

    this.attachShadow({mode: 'open'}); this.shadowRoot.innerHTML = '<div>[…]</div>'; } […] }
  18. @ManfredSteyer API class CustomerSelector extends HTMLElement { constructor() { super();

    this.attachShadow({mode: 'open'}); this.shadowRoot.innerHTML = '<div>[…]</div>'; } […] } customElements.define('customer-selector', CustomerSelector);
  19. @ManfredSteyer Angular Elements @NgModule({ imports: [BrowserModule], declarations: [DateComponent], bootstrap: [],

    entryComponents: [DateComponent] }) export class AppModule { constructor(private injector: Injector) { } ngDoBootstrap() { } }
  20. @ManfredSteyer Angular Elements @NgModule({ imports: [BrowserModule], declarations: [DateComponent], bootstrap: [],

    entryComponents: [DateComponent] }) export class AppModule { constructor(private injector: Injector) { } ngDoBootstrap() { const DateElement = createCustomElement( DateComponent, { injector: this.injector }); customElements.define('date-picker', DateElement); } }
  21. @ManfredSteyer Lazy Loading const content = document.getElementById('content'); // Add script-Tag

    const script = document.createElement('script'); script.src = [...]; content.appendChild(script);
  22. @ManfredSteyer Lazy Loading const content = document.getElementById('content'); // Add script-Tag

    const script = document.createElement('script'); script.src = [...]; content.appendChild(script); // Add Element const element: HTMLElement = document.createElement('client-a'); element.addEventListener('message', (event) => [...]); element.setAttribute('appState', [...]); content.appendChild(element);
  23. @ManfredSteyer Bundles Custom Element 1 Custom Element 2 Custom Element

    3 Libraries: Angular, Bootstrap, … Libraries: Angular, Bootstrap, …
  24. @ManfredSteyer Sharing Libs Custom Element 1 Custom Element 2 Custom

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

    Legacy Apps or *very very* strong isolation? iframes Separate Deployment/ mix Technologies? Bootstrap several SPAs Monorepo little much yes no yes no Not a good fit for public web sites Wrap as Web Component?
  26. @ManfredSteyer Blog > SOFTWAREarchitekt.at • A Software Architect's Approach Towards

    Using Angular (And SPAs In General) For Microservices Aka Microfrontends • Micro Apps With Web Components Using Angular Elements • Angular Elements: A dynamic Dashboard … • Angular, React, Vue.Js And Co. Peacefully United Thanks To Micro Apps And Web Components