Solving Module Federation with Import Maps
Posted on by Steve Workman About 5 min reading time
Over the past 8 years I've been leading web development for one of the largest B2B website in the world, maersk.com. On the day that I handed in my badge and laptop as I move on to my next adventure, I spent the day explaining to my colleagues that the site's apps were bundling far too many dependencies, and each one of them were doing it over and over again.
The article that sparked this was Nudeps: bundler-like imports, none of the baggage by Lea Verou, the final part to a series of articles that I have had open in my browser for the past few months. The idea is simple, generate an import map for your client-side rendered app's dependencies, so there's no bundler required. Under the hood it uses jspm's dependency tree tech to make the map, the magic sauce is the creation of the client_modules folder that can be deployed with your application's assets. Wonderful.
To understand why this helps me, let me tell you about my problem

This is, at a high level, how many domain-driven websites are put together. The website is made up of a series of applications, things like booking, schedules, management, payment etc. Above this there are web servers that do all the heavy lifting and redirection work to ensure the right files are served at the right time. In all, there are around a dozen teams that work on three times that many apps. Each app is in its own repository and is deployed independently to a path on the same domain. This is "domain separation" - individual teams have their own priorities and can release on their own cadence. There's very little tying them together, and the all deploy their own dependencies in one big bundle - no sharing between each app.
Everything still work though, and it all looks like one cohesive website. This is because of the great user experience team and the Maersk Design System. Each app, developed independently, joins the others through this shared design language.
The thing is, this is woefully inefficient. Each application has its own copy of the parts of the design system that it needs, plus the core web framework (vue), and often fonts and design tokens too.
Inside each design system component, there are many sub-components and dependencies. Reductively, it looks a bit like this:

There are around 50 interlinked components in the design system. An app's bundle can have some of these which a bundler like vite happily tree shakes away.
Over time, teams made some more complex, higher-order components that are used across different applications and domains. We called them community components, but you can also call them shared or common components, organisms (if you follow atomic design) or even just "modules".

There are over a hundred of these community components available. If you want to include all of them in a bundle, you're looking at nearly 690kB of code gzipped. Most apps only include one or two of these community components, and because of this, using a bundler to remove the excess code is a really large saving compared to pulling everything in every time.

So, over time teams explored making this more efficient, but they struggled to get over the fundamentals that bundling code together was very effective. Teams could move common CSS and design tokens to shared files, as these didn't change often, but the component code was more difficult. The design system only produced bundles of everything for the CDN due to how interlinked each component was. For example, including a button and an input would be two separate bundles, both of which had their own copy of lit and the input also had its own copy of the button, and both of them had the icon code! Having one large bundle of code and taking components you didn't need was more efficient than too much duplicated code.
This scenario trades off a very poor first load speed against a much faster load when moving from app to app.
The premise of import maps and dependencies in a CDN, should be able to eliminate this duplicate code by unbundling their dependencies! This is, however, extremely difficult.
Why is this so hard?

Prior to nudeps being released, this wasn't possible with just web technologies. Import maps themselves have been around since Chrome 89, but weren't supported in all major browsers until Safari 16.4. A major deficiency with import maps in that you couldn't have more than one of them in a document was resolved in Chrome 133 and Safari 18.4 - it is still behind a flag in Firefox. Without the ability to have multiple maps (i.e. injecting them dynamically) the import maps would need to be static or server-rendered, and they would also need to include a huge amount of information as they need to know where all possible code modules were.
This hasn't stopped developers trying to solve this problem, which is where module federation comes from. The idea is simple, you can declare certain packages as being remote to your app and it will load them at runtime. This can be anything from vue or react to the design system or a shared component, or even a whole other app created by other teams.
Module federation would let you share bundles of other code, but not the whole tree. For example, if the design system and community components were accessed through module federation, each app would know where these components are, but each of these bundles would have their own copy of lit and many other sub-dependencies. An import map, with each dependency available from a CDN would make dependency management optimal.
Module federation solves the code sharing issue, but it is still not optimal.
Can nudeps solve this?
At the time of writing, nudeps does not support a CDN for deployment of its dependencies. Adding this, or using jspm's CDN, would go a long way to solving this. To solve the issue described, it would need to allow you to say that each application is deployed to a location and each import map would need to know these things. This may need to be done through some nudeps configuration or a package.json property.
But... should it be solved?
We don't have performance information of this at scale. Large apps will sometimes have hundreds of dependencies, and these will translate into hundreds of network requests at load time. The actual first load performance could be terrible but right now, we don't know. Sure, it's going to be optimal for file size, but for overall performance - I don't know.
Secondly, there's the practical application of this at scale. Lets put aside that you have to convince multiple teams to implement this and deploy it. For there to be any benefit, each app must be using the same version of the dependencies. If not, the dependency will be requested each time anyway. If they have any common sub-dependencies those will be loaded from the cache, but there's still a high likelihood of dependency mismatch between so many systems.
Finally, there's going to be so much constant change in these systems too. You'll end up building a whole CDN of hundreds of versions of application code that will be very difficult to clean up with many older versions of apps hanging around, bots with large caches and more. The overall cost of this will be high.
So, what now? I think it would be great for nudeps to support CDNs, and especially private ones. I'd also love to see how this could interact with server-side rendered apps as well. With full CSR dying out (very) slowly, import maps have a role to play in the future. I'd also love to see more performance numbers. Bundlers arrived for a reason - they were overall faster - so lets see how that holds up today.