Skip to content

Commit bb5977a

Browse files
authored
docs: add section on how to specify the package name for module federation (#6519)
* Rebased main * Added more information on package name * Added more information on package name * Added more information on package name * Added more information on package name
1 parent 29647cd commit bb5977a

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

src/content/plugins/module-federation-plugin.mdx

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ contributors:
55
- XiaofengXie16
66
- chenxsan
77
- burhanuday
8+
- christian24
89
related:
910
- title: Module Federation
1011
url: /concepts/module-federation/
@@ -48,11 +49,53 @@ module.exports = {
4849
};
4950
```
5051

51-
### Specify package versions
52+
### Sharing libraries
53+
54+
With the `shared` key in the configuration you can define libraries that are shared between your federated modules. The package name is the same as the one found in the `dependencies` section of your package.json. However, by default webpack will only share the root level of a library.
55+
56+
```js
57+
const { ModuleFederationPlugin } = require('webpack').container;
58+
module.exports = {
59+
plugins: [
60+
new ModuleFederationPlugin({
61+
// adds date-fns as shared module
62+
shared: ['date-fns'],
63+
}),
64+
],
65+
};
66+
```
67+
68+
So in your application you could do something like
69+
70+
```js
71+
import { format } from 'date-fns';
72+
73+
format(new Date(2014, 1, 11), 'MM/dd/yyyy');
74+
```
75+
76+
and webpack will automatically share `date-fns` between all your federated modules that define `date-fns` as a shared library.
77+
However, if you want to access something that is not located at the root level of the package, for example `date-fns/locale/en-GB/index.js`, you need need to append `/` to the package name in your `shared` configuration:
78+
79+
```js
80+
const { ModuleFederationPlugin } = require('webpack').container;
81+
module.exports = {
82+
plugins: [
83+
new ModuleFederationPlugin({
84+
// adds date-fns as shared module
85+
// all files of the package will be shared
86+
shared: ['date-fns/'],
87+
}),
88+
],
89+
};
90+
```
91+
92+
The `/` syntax allows you to access all files of a package. However, it should be used only where necessary, because it has an impact on performance especially in `development` mode.
93+
94+
#### Specify package versions
5295

5396
There are three ways to specify the versions of shared libraries.
5497

55-
#### Array syntax
98+
##### Array syntax
5699

57100
This syntax allows you to share libraries with package name only. This approach is good for prototyping, but it will not allow you to scale to large production environment given that libraries like `react` and `react-dom` will require additional requirements.
58101

@@ -71,7 +114,7 @@ module.exports = {
71114
};
72115
```
73116

74-
#### Object syntax
117+
##### Object syntax
75118

76119
This syntax provides you more control over each shared library in which you can define package name as the key and version ([semver](https://semver.org/)) as the value.
77120

@@ -90,7 +133,7 @@ module.exports = {
90133
};
91134
```
92135

93-
#### Object syntax with sharing hints
136+
##### Object syntax with sharing hints
94137

95138
This syntax allows you to provide additional [hints](#sharing-hints) to each shared package where you define the package name as the key, and the value as an object containing hints to modify sharing behavior.
96139

@@ -112,59 +155,59 @@ module.exports = {
112155
};
113156
```
114157

115-
### Sharing hints
158+
#### Sharing hints
116159

117-
#### **`eager`**
160+
##### **`eager`**
118161

119162
`boolean`
120163

121164
This hint will allow webpack to include the provided and fallback module directly instead of fetching the library via an asynchronous request. In other words, this allows to use this shared module in the initial chunk. Also, be careful that all provided and fallback modules will always be downloaded when this hint is enabled.
122165

123-
#### **`import`**
166+
##### **`import`**
124167

125168
`false | string`
126169

127170
The provided module that should be placed in the shared scope. This provided module also acts as fallback module if no shared module is found in the shared scope or version isn't valid. (The value for this hint defaults to the property name.)
128171

129-
#### **`packageName`**
172+
##### **`packageName`**
130173

131174
`string`
132175

133176
The package name that is used to determine required version from description file. This is only needed when the package name can't be automatically determined from request.
134177

135-
#### **`requiredVersion`**
178+
##### **`requiredVersion`**
136179

137180
`false | string`
138181

139182
The required version of the package. It accepts semantic versioning. For example, "^1.2.3".
140183

141-
#### **`shareKey`**
184+
##### **`shareKey`**
142185

143186
`string`
144187

145188
The requested shared module is looked up under this key from the shared scope.
146189

147-
#### **`shareScope`**
190+
##### **`shareScope`**
148191

149192
`string`
150193

151194
The name of the shared scope.
152195

153-
#### **`singleton`**
196+
##### **`singleton`**
154197

155198
`boolean`
156199

157200
This hint only allows a single version of the shared module in the shared scope (disabled by default). Some libraries use a global internal state (e.g. react, react-dom). Thus, it is critical to have only one instance of the library running at a time.
158201

159202
In cases where there are multiple versions of the same dependency in the shared scope, the highest semantic version is used.
160203

161-
#### **`strictVersion`**
204+
##### **`strictVersion`**
162205

163206
`boolean`
164207

165208
This hint allows webpack to reject the shared module if version is not valid (defaults to `true` when local fallback module is available and shared module is not a singleton, otherwise `false`, it has no effect if there is no required version specified). Throws a runtime error if the required version is not found.
166209

167-
#### **`version`**
210+
##### **`version`**
168211

169212
`false | string`
170213

0 commit comments

Comments
 (0)