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

Micro Frontends with Module Federation: Beyond the Basics

Micro Frontends with Module Federation: Beyond the Basics

Manfred Steyer

May 03, 2023
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer
    ManfredSteyer
    Make Module Federation Work
    Manfred Steyer, ANGULARarchitects.io

    View full-size slide

  2. @ManfredSteyer
    Booking App Check-in App
    Boarding App
    Luggage App

    View full-size slide

  3. @ManfredSteyer

    View full-size slide

  4. @ManfredSteyer

    View full-size slide

  5. @ManfredSteyer

    View full-size slide

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

    View full-size slide

  7. @ManfredSteyer
    #1
    Version
    Mismatches
    #2
    Mono vs.
    Multirepo
    #3
    Dynamic
    Federation
    #4
    Multiple
    Frameworks/
    Versions

    View full-size slide

  8. @ManfredSteyer
    #1
    Version
    Mismatches
    #2
    Mono vs.
    Multirepo
    #3
    Dynamic
    Federation
    #4
    Multiple
    Frameworks/
    Versions
    #0
    Module
    Federation 101

    View full-size slide

  9. @ManfredSteyer
    Manfred Steyer

    View full-size slide

  10. @ManfredSteyer

    View full-size slide

  11. @ManfredSteyer
    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')

    View full-size slide

  12. @ManfredSteyer
    Shell (Host) Microfrontend (Remote)
    shared: [
    "@angular/core", "…"
    ]
    shared: [
    "@angular/core", "…"
    ]

    View full-size slide

  13. @ManfredSteyer
    1) ng add @angular-architects/module-federation
    2) Adjust generated configuration
    3) ng serve

    View full-size slide

  14. @ManfredSteyer

    View full-size slide

  15. @ManfredSteyer

    View full-size slide

  16. @ManfredSteyer
    Selecting the highest compatible version
    10.0 10.1

    View full-size slide

  17. @ManfredSteyer
    Make sure, your Angular-based code
    sees exactly the same Angular version
    at runtime it was built with!
    (Don't use ^ or ~ in your package.json)

    View full-size slide

  18. @ManfredSteyer
    Conflict: No highest compatible version
    11.0 10.1

    View full-size slide

  19. @ManfredSteyer
    shared: {
    "my-lib": {
    singleton: true
    }
    }
    11.0 10.1

    View full-size slide

  20. @ManfredSteyer
    shared: {
    "my-lib": {
    singleton: true,
    strictVersion: true // Error instead of warning!
    }
    }
    11.0 10.1

    View full-size slide

  21. @ManfredSteyer
    shared: {
    "my-lib": {
    requiredVersion: ">=1.0.1 <11.1.1"
    }
    }

    View full-size slide

  22. @ManfredSteyer
    •Several options baked-in
    •However, best option: avoid conflicts upfront

    View full-size slide

  23. @ManfredSteyer

    View full-size slide

  24. @ManfredSteyer
    Booking Boarding Shared
    Feature Feature Feature Feature Feature
    … … … … … … … … …
    Booking App Boarding App Publish shared libs
    seperately via npm
    Repository n
    Repository 2
    Repository 1
    Version Mismatches?
    Strict Borders

    View full-size slide

  25. @ManfredSteyer
    Booking Boarding Shared
    Feature Feature Feature Feature Feature
    … … … … … … … … …
    Booking App Boarding App

    View full-size slide

  26. @ManfredSteyer

    View full-size slide

  27. @ManfredSteyer

    View full-size slide

  28. @ManfredSteyer
    https://nrwl.io/nx
    Smart, Fast and Extensible Build System

    View full-size slide

  29. @ManfredSteyer

    View full-size slide

  30. @ManfredSteyer
    Dynamic Module Federation
    Shell (Host) Microfrontend (Remote)
    remotes: {
    mfe1: "http://..."
    }
    exposes: {
    Cmp: './my.cmp.ts'
    }
    import('mfe1/Cmp')

    View full-size slide

  31. @ManfredSteyer
    Dynamic Module Federation
    Shell (Host) Microfrontend (Remote)
    remotes: {
    }
    exposes: {
    Cmp: './my.cmp.ts'
    }
    import('mfe1/Cmp')

    View full-size slide

  32. @ManfredSteyer
    Dynamic Module Federation
    Shell (Host) Microfrontend (Remote)
    remotes: {
    }
    exposes: {
    Cmp: './my.cmp.ts'
    }
    loadRemoteModule({
    type: 'module',
    remoteEntry: 'http://…',
    exposedModule: './Cmp'
    })

    View full-size slide

  33. @ManfredSteyer
    Dynamic Module Federation
    Shell (Host) Microfrontend (Remote)
    remotes: {
    }
    exposes: {
    Cmp: './my.cmp.ts'
    }
    loadRemoteModule({
    type: 'module',
    remoteEntry: 'http://…',
    exposedModule: './Cmp'
    })
    Helper Function using
    the Webpack API

    View full-size slide

  34. @ManfredSteyer

    View full-size slide

  35. @ManfredSteyer

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  41. @ManfredSteyer

    View full-size slide

  42. @ManfredSteyer

    View full-size slide

  43. @ManfredSteyer

    View full-size slide

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

    View full-size slide

  45. @ManfredSteyer
    Free eBook (5th Edition)
    ANGULARarchitects.io/book
    Module Federation & Nx

    View full-size slide

  46. @ManfredSteyer
    Conclusion
    Baked-in Strategies
    for Version
    Mismatches
    Monorepo vs.
    Multirepo:
    Self-restriction vs.
    (Costly) Freedom
    Webpack Runtime
    API: Dynamic
    Federation
    Frankenstein:
    Bootstrapping
    Several SPAs

    View full-size slide

  47. @ManfredSteyer
    d
    Slides & Examples Remote Company Workshops
    and Consulting
    http://angulararchitects.io

    View full-size slide