44 RUNTIME_006 ,
55 runtimeDescMap ,
66} from '@module-federation/error-codes' ;
7+ import { TreeshakeStatus , isDebugMode } from '@module-federation/sdk' ;
78import { Federation } from '../global' ;
89import {
910 Options ,
@@ -16,6 +17,7 @@ import {
1617 InitScope ,
1718 InitTokens ,
1819 CallFrom ,
20+ NoMatchedUsedExportsItem ,
1921} from '../type' ;
2022import { ModuleFederation } from '../core' ;
2123import {
@@ -100,6 +102,19 @@ export class SharedHandler {
100102 shared . scope . forEach ( ( scope ) => {
101103 if ( ! this . shareScopeMap [ scope ] ?. [ sharedKey ] ?. [ shared . version ] ) {
102104 registered = false ;
105+ } else if ( sharedVal . usedExports ) {
106+ const registeredShared =
107+ this . shareScopeMap [ scope ] [ sharedKey ] [ shared . version ] ;
108+ if (
109+ registeredShared . treeshakeStatus === TreeshakeStatus . UNKNOWN &&
110+ registeredShared . usedExports &&
111+ sharedVal . usedExports . some (
112+ ( exportName ) =>
113+ ! registeredShared . usedExports ?. includes ( exportName ) ,
114+ )
115+ ) {
116+ registeredShared . treeshakeStatus = TreeshakeStatus . NO_USE ;
117+ }
103118 }
104119 } ) ;
105120
@@ -469,6 +484,34 @@ export class SharedHandler {
469484 extraOptions : { hostShareScopeMap ?: ShareScopeMap } = { } ,
470485 ) : void {
471486 const { host } = this ;
487+ const existedShareScope = this . shareScopeMap [ scopeName ] ;
488+ Object . entries ( shareScope ) . forEach ( ( [ pkgName , newVersions ] ) => {
489+ const existedShareMap = existedShareScope [ pkgName ] ;
490+ if ( ! existedShareMap ) {
491+ return ;
492+ }
493+ Object . entries ( existedShareMap ) . forEach ( ( [ version , existedShared ] ) => {
494+ const newShared = newVersions [ version ] ;
495+ if (
496+ newShared &&
497+ newShared . treeshakeStatus === TreeshakeStatus . UNKNOWN &&
498+ newShared . usedExports &&
499+ existedShared . usedExports &&
500+ existedShared . usedExports . some (
501+ ( exportName ) => ! newShared . usedExports ?. includes ( exportName ) ,
502+ )
503+ ) {
504+ newShared . treeshakeStatus = TreeshakeStatus . NO_USE ;
505+ newShared . _noMatchedUsedExports =
506+ existedShared . _noMatchedUsedExports || [ ] ;
507+ const item : NoMatchedUsedExportsItem = [ existedShared . from ] ;
508+ if ( isDebugMode ( ) && existedShared . usedExports ) {
509+ item . push ( existedShared . usedExports ) ;
510+ }
511+ newShared . _noMatchedUsedExports . push ( item ) ;
512+ }
513+ } ) ;
514+ } ) ;
472515 this . shareScopeMap [ scopeName ] = shareScope ;
473516 this . hooks . lifecycle . initContainerShareScopeMap . emit ( {
474517 shareScope,
0 commit comments