Skip to content

Commit

Permalink
fix: improve css url rewriting
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 24, 2020
1 parent cebe2e9 commit 7305c5b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/node/utils/cssUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SFCStyleCompileResults
} from '@vue/compiler-sfc'

export const urlRE = /(url\(\s*['"]?)([^"']+)(["']?\s*\))/
export const urlRE = /url\(\s*('[^']+'|"[^"]+"|[^'")]+)\s*\)/
export const cssPreprocessLangRE = /(.+)\.(less|sass|scss|styl|stylus|postcss)$/

export const isCSSRequest = (file: string) =>
Expand All @@ -32,15 +32,21 @@ export function rewriteCssUrls(
}

return asyncReplace(css, urlRE, async (match) => {
const [matched, before, rawUrl, after] = match
let [matched, rawUrl] = match
let wrap = ''
const first = rawUrl[0]
if (first === `"` || first === `'`) {
wrap = first
rawUrl = rawUrl.slice(1, -1)
}
if (
isExternalUrl(rawUrl) ||
rawUrl.startsWith('data:') ||
rawUrl.startsWith('#')
) {
return matched
}
return before + (await replacer(rawUrl)) + after
return `url(${wrap}${await replacer(rawUrl)}${wrap})`
})
}

Expand Down

0 comments on commit 7305c5b

Please sign in to comment.