question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to initialise federated module sharing with multiple shareScopes

See original GitHub issue

Bug report

What is the current behavior?

When using ModuleFederationPackage, the shared option allows specifying different scopeNames for each package. It is not possible to initialise sharing via (container.init(...)) for any but the default scope.

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

Make an example package with a set up similar to,

new ModuleFederationPlugin({
  shared: {
    'normal-package': { scopeName: 'default' },
    'package-a': { scopeName: 'a' },
    'package-b': { scopeName: 'b' }
  },
  shareScope: 'default'
})

Initialise default sharing with,

await Promise.all([
  __webpack_init_sharing__('default'),
  __webpack_init_sharing__('a'),
  __webpack_init_sharing__('b')
])

const container = window['example-package'];

await container.init(__webpack_share_scopes__['default']);

It is not possible initialise sharing in the example package for the scopes ‘a’, and ‘b’.

What is the expected behavior?

It should be possible to initialise sharing for ‘a’ and ‘b’

The current initialisation code hardcodes the scope name, even though there may be more than one.

var init = (shareScope, initScope) => {
	if (!__webpack_require__.S) return;
	var oldScope = __webpack_require__.S["default"];
	var name = "default"
	if(oldScope && oldScope !== shareScope) throw new Error("Container initialization failed as it has already been initialized with a different share scope");
	__webpack_require__.S[name] = shareScope;
	return __webpack_require__.I(name, initScope);
};

If we changed this container code (keeping API compatibility) to the following,

var init = (shareScope, initScope) => {
	return initSharing("default", shareScope, initScope);
};

var initSharing = (name, shareScope, initScope) => {
	if (!__webpack_require__.S) return;
	var oldScope = __webpack_require__.S[name];
	if(oldScope && oldScope !== shareScope) throw new Error("Container initialization failed as it has already been initialized with a different share scope");
	__webpack_require__.S[name] = shareScope;
	return __webpack_require__.I(name, initScope);
}

// This exports getters to disallow modifications
__webpack_require__.d(exports, {
	get: () => (get),
	init: () => (init),
	initSharing: () => (initSharing)
});

We could then initialise sharing for the other scopes via,

// ...
await Promise.all([
  await container.initSharing('default', __webpack_share_scopes__['default']),
  await container.initSharing('a', __webpack_share_scopes__['a']),
  await container.initSharing('b', __webpack_share_scopes__['b']),
])

See #13800

Other relevant information: webpack version: 5.44.0 Node.js version: v14.15.1 Operating System: macOS Big Sur Additional tools: nil

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sokracommented, Jul 22, 2021

@jacobp100 I think you can put react 17 and 18 in the default scope and put the package with peer dependency on react under a different key into the share scope.

Assuming a react-lib uses react.

// react 17 app which uses "react-lib"
shared: ["react", {
  "react-lib": {
    shareKey: "react-lib for react@17"
  }
}]
// react 18 app which uses "react-lib"
shared: ["react", {
  "react-lib": {
    shareKey: "react-lib for react@18"
  }
}]
0reactions
jacobp100commented, Jul 22, 2021

@sokra Thanks - that works surprisingly well! I’ll assume you don’t need the PR any more - but I’m happy to continue with it if you do need it (for other reasons etc.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module Federation: eagerly shared lib and default ...
When I add an import to a second remote module federation build unless I change the shareScope in the first build to something...
Read more >
Module Federation - webpack
This object is used as a shared scope in the remote container and is filled with the provided modules from a host. It...
Read more >
Advanced Shared API: Module Federation for power users
Official site for all things related to Webpack 5 Module Federation.
Read more >
Distributed Logging in Federated Applications, with Sentry
In this post, I attempt to answer these questions by sharing how to implement distributed logging across applications with Sentry and Webpack's ...
Read more >
Advanced API: Module Federation for power users - YouTube
If playback doesn't begin shortly, try restarting your device. Your browser can't play this video. Learn more. Switch camera. Share.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found