Skip to content

Commit 1ee5676

Browse files
author
Heiner Pöpping
committed
Refactor CSS chunk collection
Refactors the CSS chunk collection logic to use the manifest cache within the module federation runtime.
1 parent 5e238b3 commit 1ee5676

1 file changed

Lines changed: 8 additions & 43 deletions

File tree

src/util/collectCssChunks.ts

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,21 @@
11
import { ModuleContextValueType } from '../constants';
22

3-
type RemoteInfo = {
4-
exposes: {
5-
path: string,
6-
assets?: {
7-
css?: {
8-
sync?: string[],
9-
async?: string[]
10-
}
11-
}
12-
}[]
13-
}
14-
15-
const remoteInfoCache: Record<string, RemoteInfo | null> = {};
16-
17-
const loadRemoteInfo = async (url: string) => {
18-
if (!url.endsWith('/mf-manifest.json')) {
19-
return null;
20-
}
21-
22-
if (remoteInfoCache[url]) {
23-
return remoteInfoCache[url];
24-
}
25-
const res = await fetch(url);
26-
if (res.status === 200) {
27-
const info: RemoteInfo = await res.json();
28-
remoteInfoCache[url] = info;
29-
return info;
30-
}
31-
if (res.status === 404) {
32-
remoteInfoCache[url] = null;
33-
return null;
34-
}
35-
throw new Error(`Could not load remote info from ${url}`);
36-
}
37-
383
export const collectCssChunks = async (modules: ModuleContextValueType) => {
39-
const p = Object.values(modules).map(async (module) => {
40-
const info = await loadRemoteInfo(module.url);
4+
const instance = globalThis.moduleFederationRuntime.getInstance();
5+
const p = Object.values(modules).map((module) => {
6+
const info = instance.snapshotHandler.manifestCache.get(module.url);
417

428
const chunks: string[] = [];
439
info?.exposes.forEach((exposes) => {
44-
if (module.modules.has(exposes.path)) {
10+
if (exposes.path && module.modules.has(exposes.path)) {
4511
const { sync = [], async = [] } = exposes.assets?.css ?? {};
4612
[...sync, ...async].forEach((chunk) => {
4713
const url = new URL(chunk, module.url);
48-
chunks.push(`<link rel="stylesheet" href="${url}">`)
14+
chunks.push(`<link rel="stylesheet" href="${url}">`);
4915
});
5016
}
51-
})
17+
});
5218
return chunks;
53-
})
54-
const r = await Promise.allSettled(p);
55-
return r.flatMap((v) => v.status === 'fulfilled' ? v.value : []).join('')
19+
});
20+
return p.flat().join('');
5621
};

0 commit comments

Comments
 (0)