Skip to content

Commit

Permalink
fix(less): handles rewriting relative paths passed Less's data-uri
Browse files Browse the repository at this point in the history
…function. (#7400)
  • Loading branch information
bbotto-pdga committed Apr 5, 2022
1 parent ace6e93 commit 08e39b7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/playground/css/__tests__/css.spec.ts
Expand Up @@ -388,3 +388,11 @@ test('import css in less', async () => {
expect(await getColor('.css-in-less')).toBe('yellow')
expect(await getColor('.css-in-less-2')).toBe('blue')
})

test("relative path rewritten in Less's data-uri", async () => {
// relative path passed to Less's data-uri is rewritten to absolute,
// the Less inlines it
expect(await getBg('.form-box-data-uri')).toMatch(
/^url\("data:image\/svg\+xml,%3Csvg/
)
})
4 changes: 4 additions & 0 deletions packages/playground/css/index.html
Expand Up @@ -49,6 +49,10 @@ <h1>CSS</h1>
<p>Imported Less string:</p>
<pre class="imported-less"></pre>

<div class="form-box-data-uri">
tests Less's `data-uri()` function with relative image paths
</div>

<p class="stylus">Stylus: This should be blue</p>
<p class="stylus-additional-data">
Stylus additionalData: This should be orange
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/css/less.less
@@ -1,6 +1,9 @@
@import '@/nested/nested';
@import './nested/css-in-less.less';

// Test data-uri calls with relative images.
@import './less/components/form.less';

@color: blue;

.less {
Expand Down
4 changes: 4 additions & 0 deletions packages/playground/css/less/components/form.less
@@ -0,0 +1,4 @@
.form-box-data-uri {
// data-uri() calls with relative paths should be replaced just like urls.
background-image: data-uri('../images/backgrounds/form-select.svg');
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 22 additions & 3 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -965,6 +965,8 @@ type CssUrlReplacer = (
// https://drafts.csswg.org/css-syntax-3/#identifier-code-point
export const cssUrlRE =
/(?<=^|[^\w\-\u0080-\uffff])url\(\s*('[^']+'|"[^"]+"|[^'")]+)\s*\)/
export const cssDataUriRE =
/(?<=^|[^\w\-\u0080-\uffff])data-uri\(\s*('[^']+'|"[^"]+"|[^'")]+)\s*\)/
export const importCssRE = /@import ('[^']+\.css'|"[^"]+\.css"|[^'")]+\.css)/
const cssImageSetRE = /image-set\(([^)]+)\)/

Expand Down Expand Up @@ -1015,6 +1017,16 @@ function rewriteCssUrls(
})
}

function rewriteCssDataUris(
css: string,
replacer: CssUrlReplacer
): Promise<string> {
return asyncReplace(css, cssDataUriRE, async (match) => {
const [matched, rawUrl] = match
return await doUrlReplace(rawUrl, matched, replacer, 'data-uri')
})
}

function rewriteImportCss(
css: string,
replacer: CssUrlReplacer
Expand All @@ -1040,7 +1052,8 @@ function rewriteCssImageSet(
async function doUrlReplace(
rawUrl: string,
matched: string,
replacer: CssUrlReplacer
replacer: CssUrlReplacer,
funcName: string = 'url'
) {
let wrap = ''
const first = rawUrl[0]
Expand All @@ -1057,7 +1070,7 @@ async function doUrlReplace(
// The new url might need wrapping even if the original did not have it, e.g. if a space was added during replacement
wrap = "'"
}
return `url(${wrap}${newUrl}${wrap})`
return `${funcName}(${wrap}${newUrl}${wrap})`
}

async function doImportCSSReplace(
Expand Down Expand Up @@ -1307,10 +1320,12 @@ async function rebaseUrls(
const content = fs.readFileSync(file, 'utf-8')
// no url()
const hasUrls = cssUrlRE.test(content)
// data-uri() calls
const hasDataUris = cssDataUriRE.test(content)
// no @import xxx.css
const hasImportCss = importCssRE.test(content)

if (!hasUrls && !hasImportCss) {
if (!hasUrls && !hasDataUris && !hasImportCss) {
return { file }
}

Expand Down Expand Up @@ -1339,6 +1354,10 @@ async function rebaseUrls(
rebased = await rewriteCssUrls(rebased || content, rebaseFn)
}

if (hasDataUris) {
rebased = await rewriteCssDataUris(rebased || content, rebaseFn)
}

return {
file,
contents: rebased
Expand Down

0 comments on commit 08e39b7

Please sign in to comment.