add asset integrity key to file

This commit is contained in:
ChaosExAnima 2025-04-01 12:51:45 +02:00
parent b19966e529
commit a0eb0b9ee1
No known key found for this signature in database
GPG Key ID: 8F2B333100FB6117

View File

@ -18,6 +18,7 @@ export interface Options {
declare module 'vite' { declare module 'vite' {
interface ManifestChunk { interface ManifestChunk {
integrity: string; integrity: string;
assetIntegrity: Record<string, string>;
} }
} }
@ -46,17 +47,30 @@ async function augmentManifest(
.readFile(manifestPath, 'utf-8') .readFile(manifestPath, 'utf-8')
.then((file) => JSON.parse(file) as Manifest); .then((file) => JSON.parse(file) as Manifest);
if (manifest) { if (!manifest) {
await Promise.all( throw new Error(`Manifest file not found at ${manifestPath}`);
Object.values(manifest).map(async (chunk) => {
chunk.integrity = integrityForAsset(
await fs.readFile(resolveInOutDir(chunk.file)),
algorithms,
);
}),
);
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2));
} }
await Promise.all(
Object.values(manifest).map(async (chunk) => {
chunk.integrity = integrityForAsset(
await fs.readFile(resolveInOutDir(chunk.file)),
algorithms,
);
if (!chunk.assets && !chunk.css) {
return;
}
chunk.assetIntegrity = {};
await Promise.all(
(chunk.assets ?? []).concat(chunk.css ?? []).map(async (asset) => {
chunk.assetIntegrity[asset] = integrityForAsset(
await fs.readFile(resolveInOutDir(asset)),
algorithms,
);
}),
);
}),
);
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2));
} }
function integrityForAsset(source: Buffer, algorithms: string[]) { function integrityForAsset(source: Buffer, algorithms: string[]) {