Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP5 Module Federation: Sharing deep imports

Say I have the following Module Federation setup:

new ModuleFederationPlugin({
  name: 'shell',
  filename: 'shellDefinition.js',
  shared: {
      'my-shared-lib': { singleton: true, eager: true, requiredVersion: '^1.0.0' }
  },
})

This let's me share an import like the following with remotes:

import { myThing } from 'my-shared-lib';

However, what if I want to share a deep import, such as the following?

import myThing from 'my-shared-lib/things';

Doing this with the above setup seems to create a separate instance of the module in both the host and any remotes, which is perhaps understandable since we are not technically sharing the deep import. However, is there any way to get Module Federation to do so?

like image 267
csvan Avatar asked Nov 01 '25 22:11

csvan


2 Answers

You should be able to the deep import as :

shared: {
  'my-shared-lib': { singleton: true, eager: true, requiredVersion: '^1.0.0' }
  'my-shared-lib/things': { singleton: true, eager: true, requiredVersion: '^1.0.0' }
}

They will still be two separate chunks, but will be shared by multiple remotes/hosts.

like image 102
Daniel Tabuenca Avatar answered Nov 03 '25 12:11

Daniel Tabuenca


Webpack also handles any prefix path with a / at the end so if you don't care about being specific, you can use:

shared: {
  'my-shared-lib': { singleton: true, eager: true, requiredVersion: '^1.0.0' }
  'my-shared-lib/': { singleton: true, eager: true, requiredVersion: '^1.0.0' }
}

Reference: https://github.com/webpack/webpack/blob/main/lib/sharing/resolveMatchedConfigs.js#L77-L79

like image 29
Asher Lim Avatar answered Nov 03 '25 11:11

Asher Lim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!