@@ -56,7 +56,8 @@ const EmbeddedRuntimePath = require.resolve(
56
56
57
57
const federationGlobal = getFederationGlobalScope ( RuntimeGlobals ) ;
58
58
59
- const onceForCompler = new WeakSet < Compiler > ( ) ;
59
+ const onceForCompiler = new WeakSet < Compiler > ( ) ;
60
+ const onceForCompilerEntryMap = new WeakMap < Compiler , string > ( ) ;
60
61
61
62
class FederationRuntimePlugin {
62
63
options ?: moduleFederationPlugin . ModuleFederationPluginOptions ;
@@ -150,41 +151,31 @@ class FederationRuntimePlugin {
150
151
] ) ;
151
152
}
152
153
153
- static getFilePath (
154
- compiler : Compiler ,
155
- options : moduleFederationPlugin . ModuleFederationPluginOptions ,
156
- bundlerRuntimePath ?: string ,
157
- experiments ?: moduleFederationPlugin . ModuleFederationPluginOptions [ 'experiments' ] ,
158
- ) {
159
- const containerName = options . name ;
160
- const hash = createHash (
161
- `${ containerName } ${ FederationRuntimePlugin . getTemplate (
162
- compiler ,
163
- options ,
164
- bundlerRuntimePath ,
165
- experiments ,
166
- ) } `,
167
- ) ;
168
- return path . join ( TEMP_DIR , `entry.${ hash } .js` ) ;
169
- }
170
154
getFilePath ( compiler : Compiler ) {
171
- if ( this . entryFilePath ) {
172
- return this . entryFilePath ;
173
- }
174
-
175
155
if ( ! this . options ) {
176
156
return '' ;
177
157
}
178
158
159
+ const existedFilePath = onceForCompilerEntryMap . get ( compiler ) ;
160
+
161
+ if ( existedFilePath ) {
162
+ return existedFilePath ;
163
+ }
164
+
165
+ let entryFilePath = '' ;
179
166
if ( ! this . options ?. virtualRuntimeEntry ) {
180
- this . entryFilePath = FederationRuntimePlugin . getFilePath (
181
- compiler ,
182
- this . options ,
183
- this . bundlerRuntimePath ,
184
- this . options . experiments ,
167
+ const containerName = this . options . name ;
168
+ const hash = createHash (
169
+ `${ containerName } ${ FederationRuntimePlugin . getTemplate (
170
+ compiler ,
171
+ this . options ,
172
+ this . bundlerRuntimePath ,
173
+ this . options . experiments ,
174
+ ) } `,
185
175
) ;
176
+ entryFilePath = path . join ( TEMP_DIR , `entry.${ hash } .js` ) ;
186
177
} else {
187
- this . entryFilePath = `data:text/javascript;charset=utf-8;base64,${ pBtoa (
178
+ entryFilePath = `data:text/javascript;charset=utf-8;base64,${ pBtoa (
188
179
FederationRuntimePlugin . getTemplate (
189
180
compiler ,
190
181
this . options ,
@@ -193,8 +184,12 @@ class FederationRuntimePlugin {
193
184
) ,
194
185
) } `;
195
186
}
196
- return this . entryFilePath ;
187
+
188
+ onceForCompilerEntryMap . set ( compiler , entryFilePath ) ;
189
+
190
+ return entryFilePath ;
197
191
}
192
+
198
193
ensureFile ( compiler : Compiler ) {
199
194
if ( ! this . options ) {
200
195
return ;
@@ -203,7 +198,7 @@ class FederationRuntimePlugin {
203
198
if ( this . options ?. virtualRuntimeEntry ) {
204
199
return ;
205
200
}
206
- const filePath = this . getFilePath ( compiler ) ;
201
+ const filePath = this . entryFilePath ;
207
202
try {
208
203
fs . readFileSync ( filePath ) ;
209
204
} catch ( err ) {
@@ -227,7 +222,7 @@ class FederationRuntimePlugin {
227
222
this . ensureFile ( compiler ) ;
228
223
229
224
this . federationRuntimeDependency = new FederationRuntimeDependency (
230
- this . getFilePath ( compiler ) ,
225
+ this . entryFilePath ,
231
226
) ;
232
227
return this . federationRuntimeDependency ;
233
228
}
@@ -275,7 +270,7 @@ class FederationRuntimePlugin {
275
270
} ,
276
271
) ;
277
272
} else {
278
- const entryFilePath = this . getFilePath ( compiler ) ;
273
+ const entryFilePath = this . entryFilePath ;
279
274
modifyEntry ( {
280
275
compiler,
281
276
prependEntry : ( entry : Record < string , EntryDescription > ) => {
@@ -432,6 +427,8 @@ class FederationRuntimePlugin {
432
427
) ;
433
428
}
434
429
430
+ this . entryFilePath = this . getFilePath ( compiler ) ;
431
+
435
432
if ( this . options ?. experiments ?. federationRuntime === 'hoisted' ) {
436
433
new EmbedFederationRuntimePlugin ( ) . apply ( compiler ) ;
437
434
@@ -451,11 +448,11 @@ class FederationRuntimePlugin {
451
448
) . apply ( compiler ) ;
452
449
}
453
450
// dont run multiple times on every apply()
454
- if ( ! onceForCompler . has ( compiler ) ) {
451
+ if ( ! onceForCompiler . has ( compiler ) ) {
455
452
this . prependEntry ( compiler ) ;
456
453
this . injectRuntime ( compiler ) ;
457
454
this . setRuntimeAlias ( compiler ) ;
458
- onceForCompler . add ( compiler ) ;
455
+ onceForCompiler . add ( compiler ) ;
459
456
}
460
457
}
461
458
}
0 commit comments