@@ -17,47 +17,38 @@ import {
1717 isSourceFile ,
1818 rewriteChunkImports ,
1919} from '../utils/rewrite-chunk-imports' ;
20- import {
21- copyCacheToDist ,
22- getCachedMetadata ,
23- getChecksum ,
24- purgeCacheFolder ,
25- storeCachedMetadata ,
26- } from './get-cache' ;
20+ import { cacheEntry , getChecksum , getFilename } from './bundle-caching' ;
2721
2822export async function bundleShared (
2923 sharedBundles : Record < string , NormalizedSharedConfig > ,
3024 config : NormalizedFederationConfig ,
3125 fedOptions : FederationOptions ,
3226 externals : string [ ] ,
3327 platform : 'browser' | 'node' = 'browser' ,
34- cache : { pathToCache : string ; metaDataFile : string }
28+ cacheOptions : { pathToCache : string ; bundleName : string }
3529) : Promise < Array < SharedInfo > > {
3630 const checksum = getChecksum ( sharedBundles ) ;
3731 const folder = fedOptions . packageJson
3832 ? path . dirname ( fedOptions . packageJson )
3933 : fedOptions . workspaceRoot ;
4034
41- const sharedPackageInfoCache = getCachedMetadata (
42- cache . pathToCache ,
43- cache . metaDataFile ,
44- checksum
35+ const bundleCache = cacheEntry (
36+ cacheOptions . pathToCache ,
37+ getFilename ( checksum , cacheOptions . bundleName )
4538 ) ;
4639
47- if ( sharedPackageInfoCache ) {
40+ const cacheMetadata = bundleCache . getMetadata ( checksum ) ;
41+ if ( cacheMetadata ) {
4842 logger . info (
49- `Checksum of ${ cache . metaDataFile } matched, Skipped artifact bundling`
43+ `Checksum of ${ cacheOptions . bundleName } matched, Skipped artifact bundling`
5044 ) ;
51- copyCacheToDist (
52- cache . pathToCache ,
53- cache . metaDataFile ,
45+ bundleCache . copyFiles (
5446 path . join ( fedOptions . workspaceRoot , fedOptions . outputPath )
5547 ) ;
56- return sharedPackageInfoCache ;
48+ return cacheMetadata . externals ;
5749 }
5850
59- purgeCacheFolder ( cache . pathToCache , cache . metaDataFile ) ;
60- fs . mkdirSync ( cache . pathToCache , { recursive : true } ) ;
51+ bundleCache . clear ( ) ;
6152
6253 const inferredPackageInfos = Object . keys ( sharedBundles )
6354 . filter ( ( packageName ) => ! sharedBundles [ packageName ] . packageInfo )
@@ -93,7 +84,7 @@ export async function bundleShared(
9384 path . join ( fullOutputPath , ep . outName )
9485 ) ;
9586 const entryPoints = allEntryPoints . filter (
96- ( ep ) => ! fs . existsSync ( path . join ( cache . pathToCache , ep . outName ) )
87+ ( ep ) => ! fs . existsSync ( path . join ( cacheOptions . pathToCache , ep . outName ) )
9788 ) ;
9889
9990 if ( entryPoints . length > 0 ) {
@@ -120,7 +111,7 @@ export async function bundleShared(
120111 entryPoints,
121112 tsConfigPath : fedOptions . tsConfig ,
122113 external : [ ...additionalExternals , ...externals ] ,
123- outdir : cache . pathToCache ,
114+ outdir : cacheOptions . pathToCache ,
124115 mappedPaths : config . sharedMappings ,
125116 dev : fedOptions . dev ,
126117 kind : 'shared-package' ,
@@ -130,7 +121,7 @@ export async function bundleShared(
130121 } ) ;
131122
132123 const cachedFiles = bundleResult . map ( ( br ) => path . basename ( br . fileName ) ) ;
133- rewriteImports ( cachedFiles , cache . pathToCache ) ;
124+ rewriteImports ( cachedFiles , cacheOptions . pathToCache ) ;
134125 } catch ( e ) {
135126 logger . error ( 'Error bundling shared npm package ' ) ;
136127 if ( e instanceof Error ) {
@@ -165,12 +156,7 @@ export async function bundleShared(
165156
166157 const outFileNames = [ ...expectedResults ] ;
167158
168- const result = buildResult (
169- packageInfos ,
170- sharedBundles ,
171- outFileNames ,
172- fedOptions
173- ) ;
159+ const result = buildResult ( packageInfos , sharedBundles , outFileNames ) ;
174160
175161 // TODO: Decide whether/when to add .map files
176162 const chunks = bundleResult . filter (
@@ -181,15 +167,13 @@ export async function bundleShared(
181167
182168 addChunksToResult ( chunks , result , fedOptions . dev ) ;
183169
184- storeCachedMetadata ( cache . pathToCache , cache . metaDataFile , {
170+ bundleCache . persist ( {
185171 checksum,
186172 externals : result ,
187173 files : bundleResult . map ( ( r ) => r . fileName . split ( '/' ) . pop ( ) ?? r . fileName ) ,
188174 } ) ;
189175
190- copyCacheToDist (
191- cache . pathToCache ,
192- cache . metaDataFile ,
176+ bundleCache . copyFiles (
193177 path . join ( fedOptions . workspaceRoot , fedOptions . outputPath )
194178 ) ;
195179
@@ -220,28 +204,10 @@ function createOutName(
220204 return outName ;
221205}
222206
223- function createCacheFileName (
224- configState : string ,
225- sharedBundles : Record < string , NormalizedSharedConfig > ,
226- fedOptions : FederationOptions ,
227- cachePath : string ,
228- platform : string
229- ) {
230- const resultCacheState = configState + JSON . stringify ( sharedBundles ) ;
231- const resultHash = calcHash ( resultCacheState ) ;
232- const dev = fedOptions . dev ? '-dev' : '' ;
233- const resultCacheFile = path . join (
234- cachePath ,
235- 'result-' + resultHash + '-' + platform + dev + '.json'
236- ) ;
237- return resultCacheFile ;
238- }
239-
240207function buildResult (
241208 packageInfos : PackageInfo [ ] ,
242209 sharedBundles : Record < string , NormalizedSharedConfig > ,
243- outFileNames : string [ ] ,
244- fedOptions : FederationOptions
210+ outFileNames : string [ ]
245211) {
246212 return packageInfos . map ( ( pi ) => {
247213 const shared = sharedBundles [ pi . packageName ] ;
0 commit comments