50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
type ViteManifestEntry = Readonly<{
|
|
file: string;
|
|
isEntry?: boolean;
|
|
imports?: readonly string[];
|
|
}>;
|
|
|
|
/**
|
|
* Static imports of an entry are part of initial JavaScript. Every remaining
|
|
* JavaScript output is governed by the lazy-chunk budget.
|
|
*
|
|
*/
|
|
export function classifyViteJavascript(
|
|
manifest: Readonly<Record<string, ViteManifestEntry>>,
|
|
) {
|
|
const initialFiles = new Set<string>();
|
|
const visitedKeys = new Set<string>();
|
|
const pendingKeys = Object.entries(manifest)
|
|
.filter(([, entry]) => entry.isEntry)
|
|
.map(([key]) => key);
|
|
const missingImports: string[] = [];
|
|
|
|
while (pendingKeys.length > 0) {
|
|
const key = pendingKeys.pop();
|
|
if (key === undefined) break;
|
|
if (visitedKeys.has(key)) continue;
|
|
visitedKeys.add(key);
|
|
const entry = manifest[key];
|
|
if (!entry) {
|
|
missingImports.push(key);
|
|
continue;
|
|
}
|
|
if (entry.file.endsWith(".js")) initialFiles.add(entry.file);
|
|
pendingKeys.push(...(entry.imports ?? []));
|
|
}
|
|
|
|
const allJavaScript = new Set(
|
|
Object.values(manifest)
|
|
.map((entry) => entry.file)
|
|
.filter((file) => file.endsWith(".js")),
|
|
);
|
|
const lazyFiles = [...allJavaScript].filter(
|
|
(file) => !initialFiles.has(file),
|
|
);
|
|
return Object.freeze({
|
|
initialFiles: Object.freeze([...initialFiles].sort()),
|
|
lazyFiles: Object.freeze(lazyFiles.sort()),
|
|
missingImports: Object.freeze(missingImports.sort()),
|
|
});
|
|
}
|