Skip to content

Commit 8b32d0c

Browse files
committed
fix(nf): Removed checksum from cache to allow consistent removal if checksum doesnt match
1 parent 9a64d7e commit 8b32d0c

3 files changed

Lines changed: 31 additions & 27 deletions

File tree

libs/native-federation-core/src/lib/core/build-for-federation.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function buildForFederation(
3535
config: NormalizedFederationConfig,
3636
fedOptions: FederationOptions,
3737
externals: string[],
38-
buildParams = defaultBuildParams,
38+
buildParams = defaultBuildParams
3939
): Promise<FederationInfo> {
4040
const signal = buildParams.signal;
4141

@@ -47,16 +47,16 @@ export async function buildForFederation(
4747
config,
4848
fedOptions,
4949
externals,
50-
signal,
50+
signal
5151
);
5252
logger.measure(
5353
start,
54-
'[build artifacts] - To bundle all mappings and exposed.',
54+
'[build artifacts] - To bundle all mappings and exposed.'
5555
);
5656

5757
if (signal?.aborted)
5858
throw new AbortedError(
59-
'[buildForFederation] After exposed-and-mappings bundle',
59+
'[buildForFederation] After exposed-and-mappings bundle'
6060
);
6161
}
6262

@@ -67,13 +67,13 @@ export async function buildForFederation(
6767
const cacheProjectFolder = normalizePackageName(config.name);
6868
if (cacheProjectFolder.length < 1) {
6969
logger.warn(
70-
"Project name in 'federation.config.js' is empty, defaulting to root cache folder.",
70+
"Project name in 'federation.config.js' is empty, defaulting to root cache folder (could collide with other projects in the workspace)."
7171
);
7272
}
7373

7474
const pathToCache = getCachePath(
7575
fedOptions.workspaceRoot,
76-
normalizePackageName(config.name),
76+
cacheProjectFolder
7777
);
7878

7979
if (!buildParams.skipShared && sharedPackageInfoCache.length > 0) {
@@ -92,19 +92,19 @@ export async function buildForFederation(
9292
fedOptions,
9393
externals,
9494
'browser',
95-
{ pathToCache, bundleName: 'browser-shared' },
95+
{ pathToCache, bundleName: 'browser-shared' }
9696
);
9797

9898
logger.measure(
9999
start,
100-
'[build artifacts] - To bundle all shared browser externals',
100+
'[build artifacts] - To bundle all shared browser externals'
101101
);
102102

103103
sharedPackageInfoCache.push(...sharedPackageInfoBrowser);
104104

105105
if (signal?.aborted)
106106
throw new AbortedError(
107-
'[buildForFederation] After shared-browser bundle',
107+
'[buildForFederation] After shared-browser bundle'
108108
);
109109
}
110110

@@ -116,11 +116,11 @@ export async function buildForFederation(
116116
fedOptions,
117117
externals,
118118
'node',
119-
{ pathToCache, bundleName: 'node-shared' },
119+
{ pathToCache, bundleName: 'node-shared' }
120120
);
121121
logger.measure(
122122
start,
123-
'[build artifacts] - To bundle all shared node externals',
123+
'[build artifacts] - To bundle all shared node externals'
124124
);
125125
sharedPackageInfoCache.push(...sharedPackageInfoServer);
126126

@@ -136,17 +136,17 @@ export async function buildForFederation(
136136
config,
137137
fedOptions,
138138
'browser',
139-
pathToCache,
139+
pathToCache
140140
);
141141
logger.measure(
142142
start,
143-
'[build artifacts] - To bundle all separate browser externals',
143+
'[build artifacts] - To bundle all separate browser externals'
144144
);
145145
sharedPackageInfoCache.push(...separatePackageInfoBrowser);
146146

147147
if (signal?.aborted)
148148
throw new AbortedError(
149-
'[buildForFederation] After separate-browser bundle',
149+
'[buildForFederation] After separate-browser bundle'
150150
);
151151
}
152152

@@ -158,11 +158,11 @@ export async function buildForFederation(
158158
config,
159159
fedOptions,
160160
'node',
161-
pathToCache,
161+
pathToCache
162162
);
163163
logger.measure(
164164
start,
165-
'[build artifacts] - To bundle all separate node externals',
165+
'[build artifacts] - To bundle all separate node externals'
166166
);
167167
sharedPackageInfoCache.push(...separatePackageInfoServer);
168168
}
@@ -214,13 +214,13 @@ async function bundleSeparate(
214214
config: NormalizedFederationConfig,
215215
fedOptions: FederationOptions,
216216
platform: 'node' | 'browser',
217-
pathToCache: string,
217+
pathToCache: string
218218
) {
219219
const bundlePromises = Object.entries(separateBrowser).map(
220220
async ([key, shared]) => {
221221
const packageName = inferPackageFromSecondary(key);
222222
const filteredExternals = externals.filter(
223-
(e) => !e.startsWith(packageName),
223+
(e) => !e.startsWith(packageName)
224224
);
225225
return bundleShared(
226226
{ [key]: shared },
@@ -231,17 +231,17 @@ async function bundleSeparate(
231231
{
232232
pathToCache,
233233
bundleName: `${platform}-${normalizePackageName(key)}`,
234-
},
234+
}
235235
);
236-
},
236+
}
237237
);
238238

239239
const buildResults = await Promise.all(bundlePromises);
240240
return buildResults.flat();
241241
}
242242

243243
function splitShared(
244-
shared: Record<string, NormalizedSharedConfig>,
244+
shared: Record<string, NormalizedSharedConfig>
245245
): SplitSharedResult {
246246
const sharedServer: Record<string, NormalizedSharedConfig> = {};
247247
const sharedBrowser: Record<string, NormalizedSharedConfig> = {};

libs/native-federation-core/src/lib/core/bundle-caching.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { logger } from '../utils/logger';
88
export const getCachePath = (workspaceRoot: string, project: string) =>
99
path.join(workspaceRoot, 'node_modules/.cache/native-federation', project);
1010

11-
export const getFilename = (checksum: string, title: string) => {
12-
return `${title}-meta-${checksum.substring(0, 10)}.json`;
11+
export const getFilename = (title: string) => {
12+
return `${title}.meta.json`;
1313
};
1414

1515
export const getChecksum = (
@@ -89,11 +89,12 @@ export const cacheEntry = (pathToCache: string, fileName: string) => ({
8989
const metadataFile = path.join(pathToCache, fileName);
9090
if (!fs.existsSync(pathToCache)) {
9191
fs.mkdirSync(pathToCache, { recursive: true });
92+
logger.debug(`Creating cache folder '${pathToCache}' for '${fileName}'.`);
9293
return;
9394
}
9495
if (!fs.existsSync(metadataFile)) {
95-
logger.warn(
96-
`Could not purge cache, metadata file '${fileName}' could not be found.`,
96+
logger.debug(
97+
`Could not purge cached bundle, metadata file '${metadataFile}' does not exist.`,
9798
);
9899
return;
99100
}
@@ -108,5 +109,7 @@ export const cacheEntry = (pathToCache: string, fileName: string) => ({
108109
const cachedFile = path.join(pathToCache, file);
109110
if (fs.existsSync(cachedFile)) fs.unlinkSync(cachedFile);
110111
});
112+
113+
fs.unlinkSync(metadataFile);
111114
},
112115
});

libs/native-federation-core/src/lib/core/bundle-shared.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ export async function bundleShared(
3434

3535
const bundleCache = cacheEntry(
3636
cacheOptions.pathToCache,
37-
getFilename(checksum, cacheOptions.bundleName),
37+
getFilename(cacheOptions.bundleName),
3838
);
3939

4040
const cacheMetadata = bundleCache.getMetadata(checksum);
4141
if (cacheMetadata) {
42-
logger.info(
42+
logger.debug(
4343
`Checksum of ${cacheOptions.bundleName} matched, Skipped artifact bundling`,
4444
);
4545
bundleCache.copyFiles(
@@ -48,6 +48,7 @@ export async function bundleShared(
4848
return cacheMetadata.externals;
4949
}
5050

51+
// Delete older packages if checksum didnt match
5152
bundleCache.clear();
5253

5354
const inferredPackageInfos = Object.keys(sharedBundles)

0 commit comments

Comments
 (0)