scramjet/src/bundle/rewriters/css.ts
2024-06-25 20:49:48 -07:00

11 lines
442 B
TypeScript

import { encodeUrl } from "./url";
export function rewriteCss(css: string, origin?: URL) {
css = css.replace(/(?<=url\("?'?)[^"'][\S]*[^"'](?="?'?\);?)/g, (match) => encodeUrl(match, origin));
//painful regex simulator
css = css.replace(/@import\s+(['"])?([^'"\);]+)\1?\s*(?:;|$)/g, (match, quote, url) => {
return `@import ${quote || ""}${encodeUrl(url.trim(), origin)}${quote || ""};`;
});
return css;
}