mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
11 lines
442 B
TypeScript
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;
|
|
|
|
}
|