@@ -8,7 +8,11 @@ import { getExternalImports as extractExternalImports } from '../utils/get-exter
8
8
import { MappedPath } from '../utils/mapped-paths' ;
9
9
import { ProjectData } from '@softarc/sheriff-core/src/lib/api/get-project-data' ;
10
10
11
- export function removeUnusedDeps ( config : NormalizedFederationConfig , main : string , workspaceRoot : string ) : NormalizedFederationConfig {
11
+ export function removeUnusedDeps (
12
+ config : NormalizedFederationConfig ,
13
+ main : string ,
14
+ workspaceRoot : string
15
+ ) : NormalizedFederationConfig {
12
16
const fileInfos = getProjectData ( main , cwd ( ) , {
13
17
includeExternalLibraries : true ,
14
18
} ) ;
@@ -17,7 +21,10 @@ export function removeUnusedDeps(config: NormalizedFederationConfig, main: strin
17
21
const usedPackageNames = usedDeps . usedPackageNames ;
18
22
const usedMappings = usedDeps . usedMappings ;
19
23
20
- const usedPackageNamesWithTransient = addTransientDeps ( usedPackageNames , workspaceRoot ) ;
24
+ const usedPackageNamesWithTransient = addTransientDeps (
25
+ usedPackageNames ,
26
+ workspaceRoot
27
+ ) ;
21
28
const filteredShared = filterShared ( config , usedPackageNamesWithTransient ) ;
22
29
23
30
return {
@@ -27,92 +34,105 @@ export function removeUnusedDeps(config: NormalizedFederationConfig, main: strin
27
34
} ;
28
35
}
29
36
30
- function filterShared ( config : NormalizedFederationConfig , usedPackageNamesWithTransient : Set < string > ) {
31
- const filteredSharedNames = Object . keys ( config . shared ) . filter (
32
- ( shared ) => usedPackageNamesWithTransient . has ( shared )
33
- ) ;
34
-
35
- const filteredShared = filteredSharedNames . reduce (
36
- ( acc , curr ) => ( { ...acc , [ curr ] : config . shared [ curr ] } ) ,
37
- { }
38
- ) ;
39
- return filteredShared ;
37
+ function filterShared (
38
+ config : NormalizedFederationConfig ,
39
+ usedPackageNamesWithTransient : Set < string >
40
+ ) {
41
+ const filteredSharedNames = Object . keys ( config . shared ) . filter ( ( shared ) =>
42
+ usedPackageNamesWithTransient . has ( shared )
43
+ ) ;
44
+
45
+ const filteredShared = filteredSharedNames . reduce (
46
+ ( acc , curr ) => ( { ...acc , [ curr ] : config . shared [ curr ] } ) ,
47
+ { }
48
+ ) ;
49
+ return filteredShared ;
40
50
}
41
51
42
- function findUsedDeps ( fileInfos : ProjectData , workspaceRoot : string , config : NormalizedFederationConfig ) {
43
- const usedPackageNames = new Set < string > ( ) ;
44
- const usedMappings = new Set < MappedPath > ( ) ;
52
+ function findUsedDeps (
53
+ fileInfos : ProjectData ,
54
+ workspaceRoot : string ,
55
+ config : NormalizedFederationConfig
56
+ ) {
57
+ const usedPackageNames = new Set < string > ( ) ;
58
+ const usedMappings = new Set < MappedPath > ( ) ;
45
59
46
- for ( const fileName of Object . keys ( fileInfos ) ) {
47
- const fileInfo = fileInfos [ fileName ] ;
60
+ for ( const fileName of Object . keys ( fileInfos ) ) {
61
+ const fileInfo = fileInfos [ fileName ] ;
48
62
49
- if ( ! fileInfo || ! fileInfo . externalLibraries ) {
50
- continue ;
51
- }
63
+ if ( ! fileInfo || ! fileInfo . externalLibraries ) {
64
+ continue ;
65
+ }
52
66
53
- for ( const pckg of fileInfo . externalLibraries ) {
54
- usedPackageNames . add ( pckg ) ;
55
- }
67
+ for ( const pckg of fileInfo . externalLibraries ) {
68
+ usedPackageNames . add ( pckg ) ;
69
+ }
56
70
57
- const fullFileName = path . join ( workspaceRoot , fileName ) ;
58
- const mappings = config . sharedMappings . filter (
59
- ( sm ) => sm . path === fullFileName
60
- ) ;
71
+ const fullFileName = path . join ( workspaceRoot , fileName ) ;
72
+ const mappings = config . sharedMappings . filter (
73
+ ( sm ) => sm . path === fullFileName
74
+ ) ;
61
75
62
- for ( const mapping of mappings ) {
63
- usedMappings . add ( mapping ) ;
64
- }
76
+ for ( const mapping of mappings ) {
77
+ usedMappings . add ( mapping ) ;
65
78
}
66
- return { usedPackageNames, usedMappings } ;
79
+ }
80
+ return { usedPackageNames, usedMappings } ;
67
81
}
68
82
69
83
function addTransientDeps ( packages : Set < string > , workspaceRoot : string ) {
70
- const packagesAndPeers = new Set < string > ( [ ...packages ] ) ;
71
- const discovered = new Set < string > ( packagesAndPeers ) ;
72
- const stack = [ ...packagesAndPeers ] ;
73
-
74
- while ( stack . length > 0 ) {
75
- const dep = stack . pop ( ) ;
76
-
77
- if ( ! dep ) {
78
- continue ;
79
- }
80
-
81
- const pInfo = getPackageInfo ( dep , workspaceRoot ) ;
82
-
83
- if ( ! pInfo ) {
84
- continue ;
85
- }
86
-
87
- const peerDeps = getExternalImports ( pInfo , workspaceRoot ) ;
88
-
89
- for ( const peerDep of peerDeps ) {
90
- if ( ! discovered . has ( peerDep ) ) {
91
- discovered . add ( peerDep ) ;
92
- stack . push ( peerDep ) ;
93
- packagesAndPeers . add ( peerDep ) ;
94
- }
95
- }
96
- }
97
- return packagesAndPeers ;
98
- }
84
+ const packagesAndPeers = new Set < string > ( [ ...packages ] ) ;
85
+ const discovered = new Set < string > ( packagesAndPeers ) ;
86
+ const stack = [ ...packagesAndPeers ] ;
99
87
100
- function getExternalImports ( pInfo : PackageInfo , workspaceRoot : string ) {
101
- const encodedPackageName = pInfo . packageName . replace ( / [ ^ A - Z a - z 0 - 9 ] / g, '_' ) ;
102
- const cacheFileName = `${ encodedPackageName } -${ pInfo . version } .deps.json` ;
103
- const cachePath = path . join ( workspaceRoot , 'node_modules/.cache/native-federation' ) ;
104
- const cacheFilePath = path . join ( cachePath , cacheFileName ) ;
88
+ while ( stack . length > 0 ) {
89
+ const dep = stack . pop ( ) ;
105
90
106
- const cacheHit = fs . existsSync ( cacheFilePath ) ;
91
+ if ( ! dep ) {
92
+ continue ;
93
+ }
94
+
95
+ const pInfo = getPackageInfo ( dep , workspaceRoot ) ;
107
96
108
- let peerDeps ;
109
- if ( cacheHit ) {
110
- peerDeps = JSON . parse ( fs . readFileSync ( cacheFilePath , 'utf-8' ) ) ;
97
+ if ( ! pInfo ) {
98
+ continue ;
111
99
}
112
- else {
113
- peerDeps = extractExternalImports ( pInfo . entryPoint ) ;
114
- fs . mkdirSync ( cachePath , { recursive : true } ) ;
115
- fs . writeFileSync ( cacheFilePath , JSON . stringify ( peerDeps , undefined , 2 ) , 'utf-8' ) ;
100
+
101
+ const peerDeps = getExternalImports ( pInfo , workspaceRoot ) ;
102
+
103
+ for ( const peerDep of peerDeps ) {
104
+ if ( ! discovered . has ( peerDep ) ) {
105
+ discovered . add ( peerDep ) ;
106
+ stack . push ( peerDep ) ;
107
+ packagesAndPeers . add ( peerDep ) ;
108
+ }
116
109
}
117
- return peerDeps ;
110
+ }
111
+ return packagesAndPeers ;
112
+ }
113
+
114
+ function getExternalImports ( pInfo : PackageInfo , workspaceRoot : string ) {
115
+ const encodedPackageName = pInfo . packageName . replace ( / [ ^ A - Z a - z 0 - 9 ] / g, '_' ) ;
116
+ const cacheFileName = `${ encodedPackageName } -${ pInfo . version } .deps.json` ;
117
+ const cachePath = path . join (
118
+ workspaceRoot ,
119
+ 'node_modules/.cache/native-federation'
120
+ ) ;
121
+ const cacheFilePath = path . join ( cachePath , cacheFileName ) ;
122
+
123
+ const cacheHit = fs . existsSync ( cacheFilePath ) ;
124
+
125
+ let peerDeps ;
126
+ if ( cacheHit ) {
127
+ peerDeps = JSON . parse ( fs . readFileSync ( cacheFilePath , 'utf-8' ) ) ;
128
+ } else {
129
+ peerDeps = extractExternalImports ( pInfo . entryPoint ) ;
130
+ fs . mkdirSync ( cachePath , { recursive : true } ) ;
131
+ fs . writeFileSync (
132
+ cacheFilePath ,
133
+ JSON . stringify ( peerDeps , undefined , 2 ) ,
134
+ 'utf-8'
135
+ ) ;
136
+ }
137
+ return peerDeps ;
118
138
}
0 commit comments