From 0696cdde1495e60c2109831ac1eb812261b74fe8 Mon Sep 17 00:00:00 2001 From: 10Derozan Date: Wed, 20 Dec 2023 11:31:09 +0800 Subject: [PATCH] chore(module-tools): simplify logic in rewrite css url (#5086) --- .changeset/purple-bottles-dream.md | 6 ++++ .../builder/feature/style/postcssUrlPlugin.ts | 7 ++-- .../src/builder/feature/style/utils.ts | 35 ++++++------------- 3 files changed, 21 insertions(+), 27 deletions(-) create mode 100644 .changeset/purple-bottles-dream.md diff --git a/.changeset/purple-bottles-dream.md b/.changeset/purple-bottles-dream.md new file mode 100644 index 000000000000..0c41ede2b608 --- /dev/null +++ b/.changeset/purple-bottles-dream.md @@ -0,0 +1,6 @@ +--- +'@modern-js/module-tools': patch +--- + +chore(module-tools): simplify logic in rewrite css url +chore(module-tools): 简化更新 css url 的逻辑 diff --git a/packages/solutions/module-tools/src/builder/feature/style/postcssUrlPlugin.ts b/packages/solutions/module-tools/src/builder/feature/style/postcssUrlPlugin.ts index 95ad2624f44b..a93b4eefeb04 100644 --- a/packages/solutions/module-tools/src/builder/feature/style/postcssUrlPlugin.ts +++ b/packages/solutions/module-tools/src/builder/feature/style/postcssUrlPlugin.ts @@ -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, @@ -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; diff --git a/packages/solutions/module-tools/src/builder/feature/style/utils.ts b/packages/solutions/module-tools/src/builder/feature/style/utils.ts index f44f68d14fb3..785d08810449 100644 --- a/packages/solutions/module-tools/src/builder/feature/style/utils.ts +++ b/packages/solutions/module-tools/src/builder/feature/style/utils.ts @@ -87,12 +87,17 @@ export async function rebaseUrls( } } -export function rewriteCssUrls( +export async function rewriteCssUrls( css: string, type: false | string, replacer: CssUrlReplacer, ): Promise { - 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 = ''; @@ -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, -): Promise { - 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;