@@ -190,13 +190,15 @@ class ContainerPlugin {
190
190
191
191
compiler . hooks . make . tapAsync (
192
192
PLUGIN_NAME ,
193
- (
193
+ async (
194
194
compilation : Compilation ,
195
195
callback : ( error ?: WebpackError | null | undefined ) => void ,
196
196
) => {
197
197
const hasSingleRuntimeChunk =
198
198
compilation . options ?. optimization ?. runtimeChunk ;
199
199
const hooks = FederationModulesPlugin . getCompilationHooks ( compilation ) ;
200
+ const federationRuntimeDependency =
201
+ federationRuntimePluginInstance . getDependency ( compiler ) ;
200
202
const dep = new ContainerEntryDependency (
201
203
name ,
202
204
//@ts -ignore
@@ -206,78 +208,96 @@ class ContainerPlugin {
206
208
this . _options . experiments ,
207
209
) ;
208
210
dep . loc = { name } ;
209
- compilation . addEntry (
210
- compilation . options . context || '' ,
211
- dep ,
212
- {
213
- name,
214
- filename,
215
- runtime : hasSingleRuntimeChunk ? false : runtime ,
216
- library,
217
- } ,
218
- ( error : WebpackError | null | undefined ) => {
219
- if ( error ) return callback ( error ) ;
220
- hooks . addContainerEntryModule . call ( dep ) ;
221
- callback ( ) ;
222
- } ,
223
- ) ;
211
+
212
+ await new Promise ( ( resolve , reject ) => {
213
+ compilation . addEntry (
214
+ compilation . options . context || '' ,
215
+ dep ,
216
+ {
217
+ name,
218
+ filename,
219
+ runtime : hasSingleRuntimeChunk ? false : runtime ,
220
+ library,
221
+ } ,
222
+ ( error : WebpackError | null | undefined ) => {
223
+ if ( error ) return reject ( error ) ;
224
+ hooks . addContainerEntryModule . call ( dep ) ;
225
+ resolve ( undefined ) ;
226
+ } ,
227
+ ) ;
228
+ } ) . catch ( callback ) ;
229
+
230
+ await new Promise ( ( resolve , reject ) => {
231
+ compilation . addInclude (
232
+ compiler . context ,
233
+ federationRuntimeDependency ,
234
+ { name : undefined } ,
235
+ ( err , module ) => {
236
+ if ( err ) {
237
+ return reject ( err ) ;
238
+ }
239
+ hooks . addFederationRuntimeModule . call (
240
+ federationRuntimeDependency ,
241
+ ) ;
242
+ resolve ( undefined ) ;
243
+ } ,
244
+ ) ;
245
+ } ) . catch ( callback ) ;
246
+
247
+ callback ( ) ;
224
248
} ,
225
249
) ;
226
250
227
251
// this will still be copied into child compiler, so it needs a check to avoid running hook on child
228
252
// we have to use finishMake in order to check the entries created and see if there are multiple runtime chunks
229
253
compiler . hooks . finishMake . tapAsync (
230
254
PLUGIN_NAME ,
231
- async ( compilation , callback ) => {
232
- // its a child compiler
255
+ ( compilation : Compilation , callback ) => {
233
256
if (
234
257
compilation . compiler . parentCompilation &&
235
258
compilation . compiler . parentCompilation !== compilation
236
259
) {
237
- // dont include dependencies on child compilations
238
260
return callback ( ) ;
239
261
}
240
262
241
263
const hooks = FederationModulesPlugin . getCompilationHooks ( compilation ) ;
242
- const createdRuntimes = new Set ( ) ;
264
+ const createdRuntimes = new Set < string > ( ) ;
265
+
243
266
for ( const entry of compilation . entries . values ( ) ) {
244
- if ( entry . options . runtime ) {
245
- if ( createdRuntimes . has ( entry . options . runtime ) ) {
246
- continue ;
247
- }
248
- createdRuntimes . add ( entry . options . runtime ) ;
267
+ const runtime = entry . options . runtime ;
268
+ if ( runtime ) {
269
+ createdRuntimes . add ( runtime ) ;
249
270
}
250
271
}
251
272
252
- // if it has multiple runtime chunks - make another with no name or runtime assigned
253
273
if (
254
- createdRuntimes . size !== 0 ||
255
- compilation . options ?. optimization ?. runtimeChunk
274
+ createdRuntimes . size === 0 &&
275
+ ! compilation . options ?. optimization ?. runtimeChunk
256
276
) {
257
- const dep = new ContainerEntryDependency (
258
- name ,
259
- //@ts -ignore
260
- exposes ,
261
- shareScope ,
262
- federationRuntimePluginInstance . entryFilePath ,
263
- this . _options . experiments ,
264
- ) ;
277
+ return callback ( ) ;
278
+ }
279
+
280
+ const dep = new ContainerEntryDependency (
281
+ name ,
282
+ //@ts -ignore
283
+ exposes ,
284
+ shareScope ,
285
+ federationRuntimePluginInstance . entryFilePath ,
286
+ this . _options . experiments ,
287
+ ) ;
265
288
266
- dep . loc = { name } ;
289
+ dep . loc = { name } ;
267
290
268
- compilation . addInclude (
269
- compilation . options . context || '' ,
270
- dep ,
271
- { name : undefined } ,
272
- ( error : WebpackError | null | undefined ) => {
273
- if ( error ) return callback ( error ) ;
274
- hooks . addContainerEntryModule . call ( dep ) ;
275
- callback ( ) ;
276
- } ,
277
- ) ;
278
- } else {
279
- callback ( ) ;
280
- }
291
+ compilation . addInclude (
292
+ compilation . options . context || '' ,
293
+ dep ,
294
+ { name : undefined } ,
295
+ ( error : WebpackError | null | undefined ) => {
296
+ if ( error ) return callback ( error ) ;
297
+ hooks . addContainerEntryModule . call ( dep ) ;
298
+ callback ( ) ;
299
+ } ,
300
+ ) ;
281
301
} ,
282
302
) ;
283
303
@@ -301,11 +321,6 @@ class ContainerPlugin {
301
321
compiler . hooks . thisCompilation . tap (
302
322
PLUGIN_NAME ,
303
323
( compilation : Compilation , { normalModuleFactory } ) => {
304
- const federationRuntimeDependency =
305
- federationRuntimePluginInstance . getDependency ( compiler ) ;
306
-
307
- const logger = compilation . getLogger ( 'ContainerPlugin' ) ;
308
- const hooks = FederationModulesPlugin . getCompilationHooks ( compilation ) ;
309
324
compilation . dependencyFactories . set (
310
325
FederationRuntimeDependency ,
311
326
normalModuleFactory ,
@@ -314,21 +329,6 @@ class ContainerPlugin {
314
329
FederationRuntimeDependency ,
315
330
new ModuleDependency . Template ( ) ,
316
331
) ;
317
-
318
- compilation . addInclude (
319
- compiler . context ,
320
- federationRuntimeDependency ,
321
- { name : undefined } ,
322
- ( err , module ) => {
323
- if ( err ) {
324
- return logger . error (
325
- 'Error adding federation runtime module:' ,
326
- err ,
327
- ) ;
328
- }
329
- hooks . addFederationRuntimeModule . call ( federationRuntimeDependency ) ;
330
- } ,
331
- ) ;
332
332
} ,
333
333
) ;
334
334
}
0 commit comments