blog/lib/postcss/hashes.ts

23 lines
626 B
TypeScript
Raw Normal View History

2024-06-21 21:03:37 +00:00
import Postcss from "postcss";
import { readAssetManifest } from "../hash.js";
export default (opts: any = {}): Postcss.Plugin => {
return {
postcssPlugin: "postcss-hashes",
Once() {
opts._mappings = readAssetManifest();
},
Declaration(decl: Postcss.Declaration) {
decl.value = decl.value.replace(/url\("([^"]+)"\)/, (v, url) => {
if (opts._mappings.hasOwnProperty(url)) {
return v.replace(url, opts._mappings[url]);
}
return v;
});
},
};
};
export const postcss = true;