From 9b5b3526de3288fc5d0e0a40fd80378db24674a1 Mon Sep 17 00:00:00 2001 From: underfin Date: Thu, 7 Jan 2021 23:10:05 +0800 Subject: [PATCH] fix(build): inline quotes css url to base64 (#1412) fix(build/css): fix base64 inlined css urls with quotes fix #1409, fix #1413 --- packages/playground/assets/__tests__/assets.spec.ts | 1 + packages/playground/assets/css/css-url.css | 6 +++++- packages/playground/assets/index.html | 3 +++ packages/vite/src/node/plugins/css.ts | 5 +---- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/playground/assets/__tests__/assets.spec.ts b/packages/playground/assets/__tests__/assets.spec.ts index 5359c18d8956d8..c7250c43b8c23d 100644 --- a/packages/playground/assets/__tests__/assets.spec.ts +++ b/packages/playground/assets/__tests__/assets.spec.ts @@ -68,6 +68,7 @@ describe('css url() references', () => { test('base64 inline', async () => { const match = isBuild ? `data:image/png;base64` : `/icon.png` expect(await getBg('.css-url-base64-inline')).toMatch(match) + expect(await getBg('.css-url-quotes-base64-inline')).toMatch(match) }) if (isBuild) { diff --git a/packages/playground/assets/css/css-url.css b/packages/playground/assets/css/css-url.css index decc4bd687abd1..74a9ad90d8cb85 100644 --- a/packages/playground/assets/css/css-url.css +++ b/packages/playground/assets/css/css-url.css @@ -23,6 +23,10 @@ background-size: 10px; } +.css-url-quotes-base64-inline { + background: url('../nested/icon.png'); + background-size: 10px; +} /* urls inside comments should be ignored @@ -30,4 +34,4 @@ urls inside comments should be ignored background: url(../nested/non-existent.png); background-size: 10px; } -*/ \ No newline at end of file +*/ diff --git a/packages/playground/assets/index.html b/packages/playground/assets/index.html index 6fe1fab89ea90b..bcdee697f0a3b7 100644 --- a/packages/playground/assets/index.html +++ b/packages/playground/assets/index.html @@ -37,6 +37,9 @@

CSS url references

CSS background (base64 inline in prod)
+
+ CSS background (base64 inline in prod) +

SVG Fragments

diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 9e78c3b5cf7f2a..b2c882649df8a3 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -119,11 +119,8 @@ export function cssPlugin(config: ResolvedConfig): Plugin { css = css.replace(/\/\*[\s\S]*?\*\//gm, '') if (cssUrlRE.test(css)) { css = await rewriteCssUrls(css, async (url) => { - if (isExternalUrl(url) || isDataUrl(url)) { - return url - } url = await urlToBuiltUrl(url, id, config, this) - return JSON.stringify(url) + return isDataUrl(url) ? url : JSON.stringify(url) }) } }