@@ -50,7 +50,7 @@ class AddRuntimeRequiremetToPromiseExternal {
50
50
compiler . hooks . compilation . tap (
51
51
"AddRuntimeRequiremetToPromiseExternal" ,
52
52
( compilation ) => {
53
- const RuntimeGlobals = compiler . webpack . RuntimeGlobals ;
53
+ const { RuntimeGlobals } = compiler . webpack ;
54
54
if ( compilation . outputOptions . trustedTypes ) {
55
55
compilation . hooks . additionalModuleRuntimeRequirements . tap (
56
56
"AddRuntimeRequiremetToPromiseExternal" ,
@@ -81,7 +81,7 @@ class FederationDashboardPlugin {
81
81
*/
82
82
constructor ( options ) {
83
83
this . _options = Object . assign (
84
- { debug : false , filename : "dashboard.json" , useAST : false } ,
84
+ { debug : false , filename : "dashboard.json" , useAST : false , fetchClient : false } ,
85
85
options
86
86
) ;
87
87
this . _dashData = null ;
@@ -92,11 +92,9 @@ class FederationDashboardPlugin {
92
92
* @param {Compiler } compiler
93
93
*/
94
94
apply ( compiler ) {
95
- compiler . options . output . uniqueName = "v" + Date . now ( ) ;
95
+ compiler . options . output . uniqueName = `v ${ Date . now ( ) } ` ;
96
96
new AddRuntimeRequiremetToPromiseExternal ( ) . apply ( compiler ) ;
97
- const FederationPlugin = compiler . options . plugins . find ( ( plugin ) => {
98
- return plugin . constructor . name === "ModuleFederationPlugin" ;
99
- } ) ;
97
+ const FederationPlugin = compiler . options . plugins . find ( ( plugin ) => plugin . constructor . name === "ModuleFederationPlugin" ) ;
100
98
if ( FederationPlugin ) {
101
99
this . FederationPluginOptions = Object . assign (
102
100
{ } ,
@@ -125,6 +123,7 @@ class FederationDashboardPlugin {
125
123
126
124
if ( this . FederationPluginOptions . name ) {
127
125
new DefinePlugin ( {
126
+ 'process.dashboardURL' : JSON . stringify ( this . _options . dashboardURL ) ,
128
127
"process.CURRENT_HOST" : JSON . stringify (
129
128
this . FederationPluginOptions . name
130
129
) ,
@@ -140,23 +139,13 @@ class FederationDashboardPlugin {
140
139
// Explore each module within the chunk (built inputs):
141
140
chunk . getModules ( ) . forEach ( ( module ) => {
142
141
// Loop through all the dependencies that has the named export that we are looking for
143
- const matchedNamedExports = module . dependencies . filter ( ( dep ) => {
144
- return dep . name === "federateComponent" ;
145
- } ) ;
142
+ const matchedNamedExports = module . dependencies . filter ( ( dep ) => dep . name === "federateComponent" ) ;
146
143
147
- if ( matchedNamedExports . length > 0 ) {
148
- // we know that this module exported the function we care about
149
- // now we need to know how many times this function is invoked in the source code
150
- // along with all the arguments of it
151
-
152
- // these modules could be a combination of multiple source files, so we need to traverse
153
- // through its fileDependencies
154
- if ( module . resource ) {
155
- filePaths . push ( {
156
- resource : module . resource ,
157
- file : module . resourceResolveData . relativePath ,
158
- } ) ;
159
- }
144
+ if ( matchedNamedExports . length > 0 && module . resource ) {
145
+ filePaths . push ( {
146
+ resource : module . resource ,
147
+ file : module . resourceResolveData . relativePath ,
148
+ } ) ;
160
149
}
161
150
} ) ;
162
151
@@ -174,20 +163,18 @@ class FederationDashboardPlugin {
174
163
* More node types are documented here: https://babeljs.io/docs/en/babel-types#api
175
164
*/
176
165
CallExpression : ( path ) => {
177
- const node = path . node ;
166
+ const { node } = path ;
178
167
const { callee, arguments : args } = node ;
179
168
180
169
if ( callee . loc . identifierName === "federateComponent" ) {
181
- const argsAreStrings = args . every ( ( arg ) => {
182
- return arg . type === "StringLiteral" ;
183
- } ) ;
170
+ const argsAreStrings = args . every ( ( arg ) => arg . type === "StringLiteral" ) ;
184
171
if ( ! argsAreStrings ) {
185
172
return ;
186
173
}
187
174
const argsValue = [ file ] ;
188
175
189
176
// we collect the JS representation of each argument used in this function call
190
- for ( let i = 0 ; i < args . length ; i += 1 ) {
177
+ for ( let i = 0 ; i < args . length ; i ++ ) {
191
178
const a = args [ i ] ;
192
179
let { code } = generate ( a ) ;
193
180
@@ -201,12 +188,12 @@ class FederationDashboardPlugin {
201
188
// If the value is a Node, that means it was a variable name
202
189
// There is no easy way to resolve the variable real value, so we just skip any function calls
203
190
// that has variable as its args
204
- if ( ! isNode ( value ) ) {
205
- argsValue . push ( value ) ;
206
- } else {
191
+ if ( isNode ( value ) ) {
207
192
// by breaking out of the loop here,
208
193
// we also prevent this args to be pushed to `allArgumentsUsed`
209
194
break ;
195
+ } else {
196
+ argsValue . push ( value ) ;
210
197
}
211
198
212
199
if ( i === args . length - 1 ) {
@@ -277,7 +264,7 @@ class FederationDashboardPlugin {
277
264
278
265
if ( graphData ) {
279
266
const dashData = ( this . _dashData = JSON . stringify ( graphData ) ) ;
280
- // this.writeStatsFiles(stats, dashData);
267
+ this . writeStatsFiles ( stats , dashData ) ;
281
268
if ( this . _options . dashboardURL && ! this . _options . nextjs ) {
282
269
this . postDashboardData ( dashData ) . catch ( ( err ) => {
283
270
if ( err ) {
@@ -316,11 +303,8 @@ class FederationDashboardPlugin {
316
303
const remoteEntry = curCompiler . getAsset (
317
304
this . FederationPluginOptions . filename
318
305
) ;
319
- let cleanVersion = "_" + rawData . version . toString ( ) ;
306
+ const cleanVersion = typeof rawData . version === "string" ? `_ ${ rawData . version . split ( "." ) . join ( "_" ) } ` : `_ ${ rawData . version . toString ( ) } ` ;
320
307
321
- if ( typeof rawData . version === "string" ) {
322
- cleanVersion = "_" + rawData . version . split ( "." ) . join ( "_" ) ;
323
- }
324
308
let codeSource ;
325
309
if ( ! remoteEntry . source . _value && remoteEntry . source . source ) {
326
310
codeSource = remoteEntry . source . source ( ) ;
@@ -374,18 +358,13 @@ class FederationDashboardPlugin {
374
358
}
375
359
376
360
getRemoteEntryChunk ( stats , FederationPluginOptions ) {
377
- const remoteEntryChunk = stats . chunks . find ( ( chunk ) => {
378
- const specificChunk = chunk . names . find ( ( name ) => {
379
- return name === FederationPluginOptions . name ;
380
- } ) ;
381
- return specificChunk ;
382
- } ) ;
383
-
384
- return remoteEntryChunk ;
361
+
362
+ return stats . chunks . find ( ( chunk ) => chunk . names . find ( ( name ) => name === FederationPluginOptions . name ) ) ;
385
363
}
386
364
387
365
getChunkDependencies ( validChunkArray ) {
388
- const chunkDependencies = validChunkArray . reduce ( ( acc , chunk ) => {
366
+
367
+ return validChunkArray . reduce ( ( acc , chunk ) => {
389
368
const subset = chunk . getAllReferencedChunks ( ) ;
390
369
const stringifiableChunk = Array . from ( subset ) . map ( ( sub ) => {
391
370
const cleanSet = Object . getOwnPropertyNames ( sub ) . reduce ( ( acc , key ) => {
@@ -400,8 +379,6 @@ class FederationDashboardPlugin {
400
379
[ chunk . id ] : stringifiableChunk ,
401
380
} ) ;
402
381
} , { } ) ;
403
-
404
- return chunkDependencies ;
405
382
}
406
383
407
384
buildVendorFederationMap ( liveStats ) {
@@ -445,7 +422,7 @@ class FederationDashboardPlugin {
445
422
}
446
423
447
424
mapToObjectRec ( m ) {
448
- let lo = { } ;
425
+ const lo = { } ;
449
426
for ( let [ key , value ] of Object . entries ( m ) ) {
450
427
if ( value instanceof Map && value . size > 0 ) {
451
428
lo [ key ] = this . mapToObjectRec ( value ) ;
@@ -567,12 +544,12 @@ class FederationDashboardPlugin {
567
544
}
568
545
569
546
async postDashboardData ( dashData ) {
570
- console . log ( this . _options . dashboardURL ) ;
571
547
if ( ! this . _options . dashboardURL ) {
572
548
return Promise . resolve ( ) ;
573
549
}
550
+ const client = this . _options . fetchClient ? this . _options . fetchClient : fetch ;
574
551
try {
575
- const res = await fetch ( this . _options . dashboardURL , {
552
+ const res = await client ( this . _options . dashboardURL , {
576
553
method : "POST" ,
577
554
body : dashData ,
578
555
headers : {
@@ -601,7 +578,7 @@ class NextMedusaPlugin {
601
578
? path . join ( compiler . options . output . path , this . _options . filename )
602
579
: path . join (
603
580
compiler . options . output . path ,
604
- " sidecar-" + this . _options . filename
581
+ ` sidecar-${ this . _options . filename } `
605
582
) ;
606
583
const hostData = path . join (
607
584
compiler . options . output . path ,
@@ -617,7 +594,7 @@ class NextMedusaPlugin {
617
594
compiler . hooks . afterEmit . tap ( PLUGIN_NAME , ( ) => {
618
595
const sidecarData = path . join (
619
596
compiler . options . output . path ,
620
- " sidecar-" + this . _options . filename
597
+ ` sidecar-${ this . _options . filename } `
621
598
) ;
622
599
const hostData = path . join (
623
600
compiler . options . output . path ,
@@ -644,35 +621,33 @@ class NextMedusaPlugin {
644
621
645
622
const withMedusa =
646
623
( { name, ...medusaConfig } ) =>
647
- ( nextConfig = { } ) => {
648
- return Object . assign ( { } , nextConfig , {
649
- webpack ( config , options ) {
650
- if (
651
- options . nextRuntime !== "edge" &&
652
- ! options . isServer &&
653
- process . env . NODE_ENV === "production"
654
- ) {
655
- if ( ! name ) {
656
- throw new Error (
657
- "Medusa needs a name for the app, please ensure plugin options has {name: <appname>}"
658
- ) ;
659
- }
660
- config . plugins . push (
661
- new NextMedusaPlugin ( {
662
- standalone : { name } ,
663
- ...medusaConfig ,
664
- } )
624
+ ( nextConfig = { } ) => Object . assign ( { } , nextConfig , {
625
+ webpack ( config , options ) {
626
+ if (
627
+ options . nextRuntime !== "edge" &&
628
+ ! options . isServer &&
629
+ process . env . NODE_ENV === "production"
630
+ ) {
631
+ if ( ! name ) {
632
+ throw new Error (
633
+ "Medusa needs a name for the app, please ensure plugin options has {name: <appname>}"
665
634
) ;
666
635
}
636
+ config . plugins . push (
637
+ new NextMedusaPlugin ( {
638
+ standalone : { name } ,
639
+ ...medusaConfig ,
640
+ } )
641
+ ) ;
642
+ }
667
643
668
- if ( typeof nextConfig . webpack === "function" ) {
669
- return nextConfig . webpack ( config , options ) ;
670
- }
644
+ if ( typeof nextConfig . webpack === "function" ) {
645
+ return nextConfig . webpack ( config , options ) ;
646
+ }
671
647
672
- return config ;
673
- } ,
674
- } ) ;
675
- } ;
648
+ return config ;
649
+ } ,
650
+ } ) ;
676
651
677
652
module . exports = FederationDashboardPlugin ;
678
653
module . exports . clientVersion = require ( "./client-version" ) ;
0 commit comments