Skip to content

Commit

Permalink
fix css rewriter (fr this time)
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol committed Jul 13, 2024
1 parent f35903c commit 607ee1a
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/rewrite/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,34 @@ class CSS extends EventEmitter {
return this.recast(str, options, 'source');
}
recast(str, options, type) {
const regex =
/(@import\s+(?!url\())?\s*url\(\s*(['"]?)([^'")]+)\2\s*\)|@import\s+(['"])([^'"]+)\4/g

if (!str) return str;
str = new String(str).toString();
str = str.replace(/(?<=url\("?'?)[^"'][\S]*[^"'](?="?'?\);?)/gm, (match) => {
return type === "rewrite" ? this.ctx.rewriteUrl(match) : this.ctx.sourceUrl(match);
});
str = str.replace(/@import\s+(['"])?([^'"\);]+)\1?\s*(?:;|$)/gm, (match, quote, url) => {
return `@import ${quote || ""}${type === "rewrite" ? this.ctx.rewriteUrl(url) : this.ctx.sourceUrl(url)}${quote || ""};`;
return str.replace(
regex,
(
match,
importStatement,
urlQuote,
urlContent,
importQuote,
importContent
) => {
const url = urlContent || importContent
const encodedUrl = type === "rewrite" ? this.ctx.rewriteUrl(url) : this.ctx.sourceUrl(url);

if (importStatement) {
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`
}

if (importQuote) {
return `@import ${importQuote}${encodedUrl}${importQuote}`
}

return `url(${urlQuote}${encodedUrl}${urlQuote})`
});
return str;
}
}

Expand Down

0 comments on commit 607ee1a

Please sign in to comment.