You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: src/content/plugins/module-federation-plugin.mdx
+57-14Lines changed: 57 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,7 @@ contributors:
5
5
- XiaofengXie16
6
6
- chenxsan
7
7
- burhanuday
8
+
- christian24
8
9
related:
9
10
- title: Module Federation
10
11
url: /concepts/module-federation/
@@ -48,11 +49,53 @@ module.exports = {
48
49
};
49
50
```
50
51
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.
So in your application you could do something like
69
+
70
+
```js
71
+
import { format } from'date-fns';
72
+
73
+
format(newDate(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:
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
52
95
53
96
There are three ways to specify the versions of shared libraries.
54
97
55
-
#### Array syntax
98
+
#####Array syntax
56
99
57
100
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.
58
101
@@ -71,7 +114,7 @@ module.exports = {
71
114
};
72
115
```
73
116
74
-
#### Object syntax
117
+
#####Object syntax
75
118
76
119
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.
77
120
@@ -90,7 +133,7 @@ module.exports = {
90
133
};
91
134
```
92
135
93
-
#### Object syntax with sharing hints
136
+
#####Object syntax with sharing hints
94
137
95
138
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.
96
139
@@ -112,59 +155,59 @@ module.exports = {
112
155
};
113
156
```
114
157
115
-
### Sharing hints
158
+
####Sharing hints
116
159
117
-
#### **`eager`**
160
+
#####**`eager`**
118
161
119
162
`boolean`
120
163
121
164
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.
122
165
123
-
#### **`import`**
166
+
#####**`import`**
124
167
125
168
`false | string`
126
169
127
170
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.)
128
171
129
-
#### **`packageName`**
172
+
#####**`packageName`**
130
173
131
174
`string`
132
175
133
176
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.
134
177
135
-
#### **`requiredVersion`**
178
+
#####**`requiredVersion`**
136
179
137
180
`false | string`
138
181
139
182
The required version of the package. It accepts semantic versioning. For example, "^1.2.3".
140
183
141
-
#### **`shareKey`**
184
+
#####**`shareKey`**
142
185
143
186
`string`
144
187
145
188
The requested shared module is looked up under this key from the shared scope.
146
189
147
-
#### **`shareScope`**
190
+
#####**`shareScope`**
148
191
149
192
`string`
150
193
151
194
The name of the shared scope.
152
195
153
-
#### **`singleton`**
196
+
#####**`singleton`**
154
197
155
198
`boolean`
156
199
157
200
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.
158
201
159
202
In cases where there are multiple versions of the same dependency in the shared scope, the highest semantic version is used.
160
203
161
-
#### **`strictVersion`**
204
+
#####**`strictVersion`**
162
205
163
206
`boolean`
164
207
165
208
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.
0 commit comments