@@ -181,6 +181,7 @@ const withModuleFederation =
181181 experiments = {
182182 flushChunks : false ,
183183 } ,
184+ ssr = true ,
184185 removePlugins = [
185186 "BuildManifestPlugin" ,
186187 "DropClientPage" ,
@@ -391,44 +392,38 @@ const withModuleFederation =
391392 log . warning ( "warning:" ) ;
392393 log . warning ( info . warnings ) ;
393394 }
394- const { outputPath, assets, name, chunks } = info ;
395+ const { chunks } = info ;
396+ const distPath = path . join (
397+ compilation . options . context ,
398+ nextConfig . distDir
399+ ) ;
400+ const hasStats = fs . existsSync (
401+ path . join ( distPath , "static/federated-stats.json" )
402+ ) ;
403+ const hasRemote = fs . existsSync (
404+ path . join ( distPath , "static/ssr/remoteEntry.js" )
405+ ) ;
406+ if ( hasRemote && hasStats && experiments . flushChunks ) {
407+ let remoteEntry = path . join (
408+ distPath ,
409+ 'static/ssr/remoteEntry.js'
410+ ) ;
411+
395412
396- if ( name === "federated-server" && experiments . flushChunks ) {
397- const remoteEntry = assets . find ( ( asset ) => {
398- if ( ! asset && ! asset . name ) {
399- return false ;
400- }
401- return asset . name . includes ( "remoteEntry" ) ;
402- } ) ;
403413 const statContent = require ( path . join (
404- outputPath ,
405- ".. /federated-stats.json"
414+ distPath ,
415+ "static /federated-stats.json"
406416 ) ) ;
407417
408418 let loadableContent ;
409419 if (
410420 fs . existsSync (
411- path . join (
412- compilation . options . output . path ,
413- "../../federated-loadable-manifest.json"
414- )
421+ path . join ( distPath , "federated-loadable-manifest.json" )
415422 )
416423 ) {
417424 loadableContent = require ( path . join (
418- compilation . options . output . path ,
419- "../../federated-loadable-manifest.json"
420- ) ) ;
421- } else if (
422- fs . existsSync (
423- path . join (
424- compilation . options . output . path ,
425- "../federated-loadable-manifest.json"
426- )
427- )
428- ) {
429- loadableContent = require ( path . join (
430- compilation . options . output . path ,
431- "../federated-loadable-manifest.json"
425+ distPath ,
426+ "federated-loadable-manifest.json"
432427 ) ) ;
433428 } else {
434429 log (
@@ -439,17 +434,13 @@ const withModuleFederation =
439434 statContent . loadable = loadableContent ;
440435
441436 const remoteContentPromise = new Promise ( ( resolve , reject ) => {
442- fs . readFile (
443- path . join ( outputPath , remoteEntry . name ) ,
444- "utf-8" ,
445- function ( err , data ) {
446- if ( err ) {
447- reject ( err ) ;
448- complete ( ) ;
449- }
450- resolve ( data ) ;
437+ fs . readFile ( remoteEntry , "utf-8" , function ( err , data ) {
438+ if ( err ) {
439+ reject ( err ) ;
440+ complete ( ) ;
451441 }
452- ) ;
442+ resolve ( data ) ;
443+ } ) ;
453444 } ) ;
454445
455446 const collectedChunkHash = chunks
@@ -469,21 +460,20 @@ const withModuleFederation =
469460 `init: () => (init), chunkMap: () => (${ JSON . stringify (
470461 statContent
471462 ) } ), hash: ()=>(${ JSON . stringify ( hash ) } )`
463+
472464 ) ;
465+
473466 process . nextTick ( ( ) => {
474- fs . writeFile (
475- path . join ( outputPath , remoteEntry . name ) ,
476- newSource ,
477- ( err ) => {
478- log ( "emitted!" ) ;
479- if ( typeof done === "function" ) {
480- complete ( ) ;
481- pending . then ( ( ) => {
482- done ( err ) ;
483- } ) ;
484- }
467+ fs . rmSync ( remoteEntry ) ;
468+ fs . writeFile ( remoteEntry , newSource , ( err ) => {
469+ log ( "emitted!" ) ;
470+ if ( typeof done === "function" ) {
471+ complete ( ) ;
472+ pending . then ( ( ) => {
473+ done ( err ) ;
474+ } ) ;
485475 }
486- ) ;
476+ } ) ;
487477 } ) ;
488478 } ) ;
489479 } else {
@@ -517,6 +507,14 @@ const withModuleFederation =
517507 * @returns {import("webpack").Configuration }
518508 */
519509 webpack ( config , options ) {
510+ if ( ! ssr && options . isServer ) {
511+ log ( 'SSR Disabled' )
512+ if ( typeof nextConfig . webpack === "function" ) {
513+ return nextConfig . webpack ( config , options ) ;
514+ }
515+
516+ return config ;
517+ }
520518 const { webpack } = options ;
521519 Object . assign ( config . experiments , {
522520 topLevelAwait : true ,
@@ -641,17 +639,17 @@ const withModuleFederation =
641639
642640 // in production or on server build use tapAsync to wait for the full compilation of the sidecar
643641 if ( isProd || compiler . options . mode === "production" ) {
644- // if (
645- // compiler.options.name === "server" ||
646- // compiler.options.name === "client"
647- // ) {
642+ if (
643+ compiler . options . name === "server" ||
644+ compiler . options . name === "client"
645+ ) {
648646 compiler . hooks . afterCompile . tapAsync (
649647 "NextFederation" ,
650648 ( compilation , done ) => {
651649 run ( compilation , done ) ;
652650 }
653651 ) ;
654- // }
652+ }
655653 } else {
656654 compiler . hooks . afterCompile . tap (
657655 "NextFederation" ,
0 commit comments