Skip to content

Commit ea2d74a

Browse files
authored
Merge pull request #1006 from Patternslib/module-federation-improvements
Module federation improvements
2 parents a19fb98 + 7eba043 commit ea2d74a

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
"url": "http://cornae.com"
115115
}
116116
],
117+
"keywords": [
118+
"patternslib"
119+
],
117120
"publishConfig": {
118121
"access": "public"
119122
}

webpack/webpack.mf.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ const local_package_json = require("../package.json");
66
// NOTE: This is also defined in ``module_federation.js``.
77
const MF_NAME_PREFIX = "__patternslib_mf__";
88

9+
/**
10+
* Get dependencies and versions for the module federation plugin from
11+
* package.json dependency lists.
12+
*
13+
* @param {Array} - List of package.json dependencies fields.
14+
* @returns {Object} - Object with dependencies for the module federation plugin.
15+
*/
916
function shared_from_dependencies(...dependencies) {
1017
const shared = {};
1118
for (const deps of dependencies) {
@@ -26,14 +33,41 @@ function shared_from_dependencies(...dependencies) {
2633
return shared;
2734
}
2835

29-
function config({ name, filename = "remote.min.js", package_json, remote_entry }) {
36+
/**
37+
* Webpack module federation config factory.
38+
*
39+
* Use this to extend your webpack configuration for module federation support.
40+
*
41+
* @param {String} name - Bundle/remote name. If not given, the package.json name is used.
42+
* @param {String} filename - Name of the generated remote entry file. Default ``remote.min.js``.
43+
* @param {Object} package_json - Your imported project's package.json file. This is to automatically set the shared dependencies.
44+
* @param {String} remote_entry - Path to which the new remote entry file is written to.
45+
* @param {Object} shared - Object with dependency name - version specifier pairs. Can be used instead of the package_json parameter.
46+
* @returns {Object} - Webpack config partial with instantiated module federation plugins.
47+
*/
48+
function config({
49+
name,
50+
filename = "remote.min.js",
51+
package_json,
52+
remote_entry,
53+
shared = {},
54+
}) {
3055
// If no name is given, use the package name.
3156
name = name || package_json?.name || local_package_json.name;
3257

3358
// Create a JS-variable compatible name and add a prefix.
3459
const normalized_bundle_name =
3560
MF_NAME_PREFIX + name.match(/([_$A-Za-z0-9])/g).join("");
3661

62+
shared = {
63+
...(!Object.keys(shared).length && // only include package.json depenencies, if shared is empty.
64+
shared_from_dependencies(
65+
local_package_json.dependencies,
66+
package_json?.dependencies
67+
)),
68+
...shared,
69+
};
70+
3771
return new ModuleFederationPlugin({
3872
name: normalized_bundle_name,
3973
...(remote_entry && {
@@ -42,12 +76,7 @@ function config({ name, filename = "remote.min.js", package_json, remote_entry }
4276
"./main": remote_entry,
4377
},
4478
}),
45-
shared: {
46-
...shared_from_dependencies(
47-
local_package_json.dependencies,
48-
package_json?.dependencies
49-
),
50-
},
79+
shared: shared,
5180
});
5281
}
5382

0 commit comments

Comments
 (0)