import { Transform } from "node:stream"; import { createHash } from "node:crypto"; import File from "vinyl"; function fileHash(buffer: Buffer) { const hash = createHash("sha256"); hash.update(buffer.toString()); return hash.digest("hex"); } export default function (manifestName: string) { const mappings: { [path: string]: string } = {}; return new Transform({ readableObjectMode: true, writableObjectMode: true, transform(chunk: File, _, callback) { const hash = fileHash(chunk.contents as Buffer); const newName = `${chunk.basename.substring(0, chunk.basename.length - chunk.extname.length)}.${hash.substring(0, 8)}${chunk.extname}`; mappings[chunk.basename] = newName; chunk.basename = newName; callback(null, chunk); }, final(callback) { const mappingFile = new File({ path: manifestName, contents: Buffer.from(JSON.stringify(mappings)), }); this.push(mappingFile); callback(null); }, }); }