Giter Site home page Giter Site logo

kfrederix / single-spa-angular-esm Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 1.0 1.39 MB

Example Nx monorepo, demonstrating how single-spa can be used for microfrontends with the new Angular builder based on Vite/esbuild.

Home Page: https://single-spa-angular-esm.vercel.app

License: MIT License

HTML 15.34% JavaScript 3.95% TypeScript 77.94% CSS 2.77%

single-spa-angular-esm's Introduction

Single-Spa with Angular v17+ and native ESM

Nx - Angular - esbuild - Single-Spa

What is this?

This is an example Nx monorepo demonstrating how single-spa can be used for microfrontends with the new Angular builder based on Vite/esbuild.

Goals:

  • Angular micro-frontend apps, served/bundled as native ES Modules by Vite/esbuild
  • Leverage native browser importmap support
  • Support for having multiple importmaps (thanks to import-map-injector)
  • Support for external importmaps (thanks to import-map-injector)
  • Support for overriding importmaps (thanks to import-map-overrides)
  • Route-based loading of micro-frontends (handled by single-spa)
  • live-reload functionality during development
  • TODO: share dependencies between Angular micro-frontends (it seems simply using the externalDependencies option with @nx/angular:application is not fully working yet...?)

Want to learn more?

I've written some more in-depth background in a seprate markdown doc. I encourage you to read it if you are interested to learn more about the reasoning behind all of this: background.md

Try it out

To get started, first we need to install our dependencies:

pnpm i

NOTE: I chose pnpm for this repo because of personal preference, but nothing should stop us from using plain npm, yarn or even bun.

To run the application, we need to start the dev-server on each of the apps in the repo:

  • the app-shell (the root html file with importmaps etc)
  • the host module (MF orchestration logic, or in simple terms: some vanilla JS that calls singleSpa.registerApplication())
  • the navbar MF
  • the 2 main MF apps (cats and dogs)

To do this, we could open 5 separate terminal windows and run the serve target for each of these apps separately (e.g. pnpm nx serve app-shell etc).

OR, to make it easier, we can run the dev script which leverages Nx to start them all at once in parallel:

pnpm dev

Give it a few seconds until all the dev servers are up-and-running. Then we are ready to see it in action by opening the app-shell url in our browser: http://localhost:4300/

Override the importmap

One feature that makes this architecture really shine, is the ability to override specific modules in the importmap, for local development. To enable this, we used the import-map-overrides library (we load this script from our app-shell page).

To try it out, you can follow these steps:

Step 1. Serve 1 micro-frontend app locally. For example:

pnpm nx serve cats

Step 2. Visit deployed application in your browser: https://single-spa-angular-esm.vercel.app/

Step 3. Open the JS console and execute the following javascript command

localStorage.setItem('overrides-ui', true)

Step 4. Refresh the page in the browser.

Step 5. Now you should see a yellow-ish rounded square button near the bottom-right corner of the page. Click it, and override the module named @myorg/cats with the URL of the locally served micro-frontend app: http://localhost:4201/main.js

Step 6. Refresh the page once more. Now you are set up to start making local changes to the cats app and see these changes reflected in the browser immediately 🎉🚀

single-spa-angular-esm's People

Contributors

kfrederix avatar grimaceofdespair avatar

Stargazers

 avatar Onán Tanco Paredes avatar  avatar Dan Buvid avatar Vanderson Guidi avatar  avatar Denis Bendrikov avatar hemanth Kona avatar Jonathan Gelin avatar

Watchers

 avatar Vanderson Guidi avatar

single-spa-angular-esm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.