@@ -6,6 +6,13 @@ const local_package_json = require("../package.json");
6
6
// NOTE: This is also defined in ``module_federation.js``.
7
7
const MF_NAME_PREFIX = "__patternslib_mf__" ;
8
8
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
+ */
9
16
function shared_from_dependencies ( ...dependencies ) {
10
17
const shared = { } ;
11
18
for ( const deps of dependencies ) {
@@ -26,14 +33,41 @@ function shared_from_dependencies(...dependencies) {
26
33
return shared ;
27
34
}
28
35
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
+ } ) {
30
55
// If no name is given, use the package name.
31
56
name = name || package_json ?. name || local_package_json . name ;
32
57
33
58
// Create a JS-variable compatible name and add a prefix.
34
59
const normalized_bundle_name =
35
60
MF_NAME_PREFIX + name . match ( / ( [ _ $ A - Z a - z 0 - 9 ] ) / g) . join ( "" ) ;
36
61
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
+
37
71
return new ModuleFederationPlugin ( {
38
72
name : normalized_bundle_name ,
39
73
...( remote_entry && {
@@ -42,12 +76,7 @@ function config({ name, filename = "remote.min.js", package_json, remote_entry }
42
76
"./main" : remote_entry ,
43
77
} ,
44
78
} ) ,
45
- shared : {
46
- ...shared_from_dependencies (
47
- local_package_json . dependencies ,
48
- package_json ?. dependencies
49
- ) ,
50
- } ,
79
+ shared : shared ,
51
80
} ) ;
52
81
}
53
82
0 commit comments