Skip to content

Commit

Permalink
chore(module-tools): simplify logic in rewrite css url (#5086)
Browse files Browse the repository at this point in the history
  • Loading branch information
10Derozan committed Dec 20, 2023
1 parent 83415e3 commit 0696cdd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .changeset/purple-bottles-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/module-tools': patch
---

chore(module-tools): simplify logic in rewrite css url
chore(module-tools): 简化更新 css url 的逻辑
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export const postcssUrlPlugin = (options: {
postcssPlugin: 'postcss-url',
async Declaration(decl) {
const isProcessed = (decl as any)[Processed];

if (isProcessed) {
return;
}
decl.value = await rewriteCssUrls(
decl.value,
false,
Expand All @@ -26,8 +28,7 @@ export const postcssUrlPlugin = (options: {
URL &&
!HTTP_PATTERNS.test(URL) &&
!HASH_PATTERNS.test(URL) &&
!DATAURL_PATTERNS.test(URL) &&
!isProcessed
!DATAURL_PATTERNS.test(URL)
) {
let filePath = URL;
const { outDir, sourceDir, buildType } = options.compilation.config;
Expand Down
35 changes: 11 additions & 24 deletions packages/solutions/module-tools/src/builder/feature/style/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@ export async function rebaseUrls(
}
}

export function rewriteCssUrls(
export async function rewriteCssUrls(
css: string,
type: false | string,
replacer: CssUrlReplacer,
): Promise<string> {
return asyncReplace(css, cssUrlRE, async match => {
let match: RegExpExecArray | null;
let remaining = css;
let rewritten = '';
// eslint-disable-next-line no-cond-assign
while ((match = cssUrlRE.exec(remaining))) {
rewritten += remaining.slice(0, match.index);
const matched = match[0];
let rawUrl = match[1];
let wrap = '';
Expand All @@ -101,33 +106,15 @@ export function rewriteCssUrls(
wrap = first;
rawUrl = rawUrl.slice(1, -1);
}
if (
const result =
(type === 'less' && rawUrl.startsWith('@')) ||
((type === 'sass' || type === 'scss') && rawUrl.startsWith('$')) ||
isExternalUrl(rawUrl) ||
isDataUrl(rawUrl) ||
rawUrl.startsWith('#')
) {
// do not rewrite
return matched;
}

return `url(${wrap}${normalizeSlashes(await replacer(rawUrl))}${wrap})`;
});
}

async function asyncReplace(
input: string,
re: RegExp,
replacer: (match: RegExpExecArray) => string | Promise<string>,
): Promise<string> {
let match: RegExpExecArray | null;
let remaining = input;
let rewritten = '';
// eslint-disable-next-line no-cond-assign
while ((match = re.exec(remaining))) {
rewritten += remaining.slice(0, match.index);
rewritten += await replacer(match);
? matched
: `url(${wrap}${normalizeSlashes(await replacer(rawUrl))}${wrap})`;
rewritten += result;
remaining = remaining.slice(match.index + match[0].length);
}
rewritten += remaining;
Expand Down

0 comments on commit 0696cdd

Please sign in to comment.