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

Import Maps: The Next Evolution Step for Micro Frontends?

Import Maps: The Next Evolution Step for Micro Frontends?

Manfred Steyer
PRO

October 27, 2022
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer Import Maps: The Next Evolution Step for Micro Frontends?

  2. @ManfredSteyer

  3. @ManfredSteyer

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

  5. @ManfredSteyer const Component = await import('other-app/xyz')

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

  7. @ManfredSteyer

  8. @ManfredSteyer

  9. @ManfredSteyer

  10. @ManfredSteyer #1 Module Federation 101 #2 Import Maps #3 Module

    Federation on Import Maps
  11. @ManfredSteyer Manfred Steyer

  12. @ManfredSteyer

  13. @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')
  14. @ManfredSteyer Shell (Host) Microfrontend (Remote) RemoteEntry.js <script src="…"></script>

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

    shared: [ "@angular/core", "…" ]
  16. @ManfredSteyer Selecting the highest compatible version ^10.0 ^10.1

  17. @ManfredSteyer Make sure, your Angular-based libs see exactly the same

    Angular version at runtime it was built with! (Don't use ^ or ~ in your package.json)
  18. @ManfredSteyer Conflict: No highest compatible version 11.0 10.1

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

    10.1
  20. @ManfredSteyer shared: { "my-lib": { singleton: true, strictVersion: true //

    Error instead of warning! } } 11.0 10.1
  21. @ManfredSteyer

  22. @ManfredSteyer <script type="module"> import { format, parseISO } from 'date-fns';

    const date = parseISO('2022-08-15'); const weekday = format(date, 'EEE'); console.log(`It's a ${weekday}.`); </script>
  23. @ManfredSteyer <script type="module"> import { format, parseISO } from 'date-fns';

    const date = parseISO('2022-08-15'); const weekday = format(date, 'EEE'); console.log(`It's a ${weekday}.`); </script>
  24. @ManfredSteyer <script type="importmap"> { "imports": { "date-fns": "./libs/date-fns.js" } }

    </script>
  25. @ManfredSteyer <script type="importmap"> { "imports": { "date-fns": "./libs/date-fns.js", "is-long-weekend": "http://this-app/module.mjs",

    "is-bridging-day": "http://that-app/module.mjs" } } </script>
  26. @ManfredSteyer <script type="importmap"> { "imports": { […] }, "scopes": {

    "http://that-app/module.mjs": { "date-fns": "./libs/other-date-fns.js" } } } </script>
  27. @ManfredSteyer const im = document.createElement('script'); im.type = 'importmap'; im.textContent =

    JSON.stringify(importMap); document.currentScript.after(im);
  28. @ManfredSteyer [https://caniuse.com/import-maps]

  29. @ManfredSteyer

  30. @ManfredSteyer

  31. @ManfredSteyer

  32. @ManfredSteyer Meta Data • Providing at compile time • Loading

    at runtime Bundling • Remotes • Shared packages • Angular compiler Import Map • Generate using meta data • Scopes for version mismatches
  33. @ManfredSteyer

  34. @ManfredSteyer @gioboa/vite-module-federation @softarc/native-federation @angular-architects/native-federation yours Example: VanillaJS and React with

    esbuild Example: Vite with Svelte and Angular (AnalogJS)
  35. @ManfredSteyer // Step 1: Initialize Native Federation await federationBuilder.init({ ...

    })
  36. @ManfredSteyer // Step 1: Initialize Native Federation await federationBuilder.init({ ...

    }) // Step 2: Trigger your build process await esbuild.build({ [...] external: federationBuilder.externals, [...] });
  37. @ManfredSteyer // Step 1: Initialize Native Federation await federationBuilder.init({ ...

    }) // Step 2: Trigger your build process await esbuild.build({ [...] external: federationBuilder.externals, [...] }); // Step 3: Run additional tasks await federationBuilder.build();
  38. @ManfredSteyer module.exports = withNativeFederation({ name: 'mfe1', exposes: { './component': './mfe1/component',

    }, shared: { ...shareAll({ [...] }), }, });
  39. @ManfredSteyer import { initFederation } from '@softarc/native-federation'; (async () =>

    { await initFederation({ mfe1: 'http://localhost:3001/remoteEntry.json', }); await import('./app'); })();
  40. @ManfredSteyer import { initFederation } from '@softarc/native-federation'; (async () =>

    { await initFederation({ mfe1: 'http://localhost:3001/remoteEntry.json', }); await import('./app'); })();
  41. @ManfredSteyer import { initFederation } from '@softarc/native-federation'; (async () =>

    { await initFederation('assets/manifest.json'); await import('./app'); })();
  42. @ManfredSteyer const module = await loadRemoteModule({ remoteName: 'mfe1', exposedModule: './component',

    });
  43. @ManfredSteyer const module = await import('mfe1/component');

  44. @ManfredSteyer <script type="esms-options"> { "shimMode": true } </script> <script src="../es-module-shims.js"></script>

    <script type="module-shim" src="main.js"></script>
  45. @ManfredSteyer

  46. @ManfredSteyer

  47. @ManfredSteyer

  48. @ManfredSteyer

  49. @ManfredSteyer

  50. @ManfredSteyer

  51. @ManfredSteyer If you use webpack: No need to hurry. Stick

    with webpack Module Federation
  52. @ManfredSteyer If you use esbuild or vite: Give Native Federation

    a try and provide feedback
  53. @ManfredSteyer Once, Angular CLI supports esbuild: Give Native Federation a

    try and provide feedback
  54. @ManfredSteyer In general: Native Federation is your insurance for the

    mid- and long-term
  55. @ManfredSteyer Free eBook (5th Edition) ANGULARarchitects.io/book Module Federation & Nx

  56. @ManfredSteyer Module Federation: Mental Model rocks! Import Maps: Provide low

    level building blocks Native Federation: Insurance Try out with esbuild, vite, etc.
  57. @ManfredSteyer

  58. @ManfredSteyer d Slides & Examples Remote Company Workshops and Consulting

    http://angulararchitects.io