webpack: Using Webpack Module Federation breaks Workers

Bug report

What is the current behavior?

When using shared modules with Webpack Module Federations, the worker chunk will contain references to shared contexts, but the worker runtime does not contain any information on how to load these contexts.

If the current behavior is a bug, please provide the steps to reproduce.

Simple reproduction repo: https://github.com/edusperoni/wmf-worker-bug

What is the expected behavior?

Worker runtime should contain the relevant information to load the modules, even if they’re limited to eagerly loaded dependencies.

This “fixes” the issue by getting the needed information from the main chunk and adding them in the worker chunk ‘consume-shared’ section:

// lib/sharing/ConsumeSharedRuntimeModule.js
                 // Here we grab the "main" chunk, which contains all the needed information about eagerly shared modules
                 const extraChunk = [Array.from(compilation.chunks)[0]];
		// const extraChunk = [];
		for (const chunk of [...this.chunk.getAllAsyncChunks(),...extraChunk]) {
			const modules = chunkGraph.getChunkModulesIterableBySourceType(
				chunk,
				"consume-shared"
			);
			if (!modules) continue;
			addModules(modules, chunk, (chunkToModuleMapping[chunk.id] = []));
		}
		for (const chunk of [...this.chunk.getAllInitialChunks(),...extraChunk]) {
			const modules = chunkGraph.getChunkModulesIterableBySourceType(
				chunk,
				"consume-shared"
			);
			if (!modules) continue;
			addModules(modules, chunk, initialConsumes);
		}

I’m unfortunately not that well versed in the webpack source code, but it seems like we should be able to resolve this by passing the parent information related to shared modules to the worker to solve this issue.

Note: the above fix doesn’t work if you have other chunks, as it only gets the information from the “main” chunk

Other relevant information: webpack version: 5.74.0 Node.js version: v16.13.2 Operating System: macOS 12.5

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 6
  • Comments: 32 (16 by maintainers)

Most upvoted comments

bump

Oh come on… can s a human reopen it please?

bump

bump

I use runtime plugins. We have a loadEntry hook, so we do not use any of webpack’s built-in loading mechanisms for scripts. We support a few out of the box. And for React Native or Node, we just use a runtime plugin.

https://module-federation.io/plugin/dev/index.html

PR here: https://github.com/webpack/webpack/pull/18859

@unadlib wanna check if it fixes your issue?

You can open a pr if you want. Otherwise I will. I even mention this change is needed to also remove need for async import.

https://github.com/webpack/webpack/issues/18812

It’s the last section of my issue. Where get all referenced chunks is. Even mentions the files. It’s night here and I have flights this week. But if you send a PR for just that update, it needed to be done anyways. But in my body of work it might take longer. So send a PR just with the referenced chunks, if you can.

Otherwise I’ll try to send one before I leave.

Okay I’ve run into this. It’s related to runtime chunks etc. Just change get all async chunks to get all referenced chunks. Allows tracking of split chunks then as well. Can send pr it’s a 2 line change in 2 files. This is what I do in the v2 plugin, but worker has other issues