diff --git a/packages/enhanced/src/lib/container/RemoteRuntimeModule.ts b/packages/enhanced/src/lib/container/RemoteRuntimeModule.ts index 14005344c1..2671e97e12 100644 --- a/packages/enhanced/src/lib/container/RemoteRuntimeModule.ts +++ b/packages/enhanced/src/lib/container/RemoteRuntimeModule.ts @@ -10,6 +10,7 @@ import type ExternalModule from 'webpack/lib/ExternalModule'; import type FallbackModule from './FallbackModule'; import type { RemotesOptions } from '@module-federation/webpack-bundler-runtime'; import { FEDERATION_SUPPORTED_TYPES } from '@module-federation/webpack-bundler-runtime/constant'; +import ContainerEntryModule from './ContainerEntryModule'; const extractUrlAndGlobal = require( normalizeWebpackPath('webpack/lib/util/extractUrlAndGlobal'), @@ -44,12 +45,22 @@ class RemoteRuntimeModule extends RuntimeModule { // chunkReferences = this.chunk.getAllAsyncChunks(); // } // } + let chunkReferences; + if (this.chunkGraph && this.chunk) { + const entryMods = Array.from( + this.chunkGraph.getChunkEntryModulesIterable(this.chunk), + ); + const isRemoteEntry = entryMods.some( + (m) => m instanceof ContainerEntryModule, + ); + chunkReferences = isRemoteEntry + ? this.chunk.getAllAsyncChunks() + : this.chunk.getAllReferencedChunks(); + } else { + chunkReferences = this.chunk?.getAllReferencedChunks() || []; + } - const allChunks = [ - ...Array.from(this.chunk?.getAllReferencedChunks() || []), - ]; - - for (const chunk of allChunks) { + for (const chunk of chunkReferences) { const modules = chunkGraph?.getChunkModulesIterableBySourceType( chunk, 'remote', diff --git a/packages/enhanced/src/lib/sharing/ConsumeSharedRuntimeModule.ts b/packages/enhanced/src/lib/sharing/ConsumeSharedRuntimeModule.ts index 0ed0842722..500173556d 100644 --- a/packages/enhanced/src/lib/sharing/ConsumeSharedRuntimeModule.ts +++ b/packages/enhanced/src/lib/sharing/ConsumeSharedRuntimeModule.ts @@ -7,6 +7,7 @@ import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-p import type { Module, ChunkGraph, Compilation, Chunk } from 'webpack'; import ConsumeSharedModule from './ConsumeSharedModule'; import { getFederationGlobalScope } from '../container/runtime/utils'; +import ContainerEntryModule from '../container/ContainerEntryModule'; const { Template, RuntimeGlobals, RuntimeModule } = require( normalizeWebpackPath('webpack'), @@ -92,15 +93,21 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { moduleIdToSourceMapping.set(id, sharedInfoAndHandlerStr); } }; - // const chunkReferences = this._runtimeRequirements.has( - // 'federation-entry-startup', - // ) - // ? this.chunk?.getAllReferencedChunks() - // : this.chunk?.getAllAsyncChunks(); - // - // const allChunks = chunkReferences || []; - const allChunks = [...(this.chunk?.getAllReferencedChunks() || [])]; - for (const chunk of allChunks) { + let chunkReferences; + if (this.chunkGraph && this.chunk) { + const entryMods = Array.from( + this.chunkGraph.getChunkEntryModulesIterable(this.chunk), + ); + const isRemoteEntry = entryMods.some( + (m) => m instanceof ContainerEntryModule, + ); + chunkReferences = isRemoteEntry + ? this.chunk.getAllAsyncChunks() + : this.chunk.getAllReferencedChunks(); + } else { + chunkReferences = this.chunk?.getAllReferencedChunks() || []; + } + for (const chunk of chunkReferences) { const modules = chunkGraph.getChunkModulesIterableBySourceType( chunk, 'consume-shared', @@ -116,7 +123,7 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { (chunkToModuleMapping[chunk.id.toString()] = []), ); } - for (const chunk of [...(this.chunk?.getAllInitialChunks() || [])]) { + for (const chunk of this.chunk?.getAllInitialChunks() || []) { const modules = chunkGraph.getChunkModulesIterableBySourceType( chunk, 'consume-shared',