diff --git a/packages/playground/assets/__tests__/assets.spec.ts b/packages/playground/assets/__tests__/assets.spec.ts index 694d91709701d0..04ba14afa17272 100644 --- a/packages/playground/assets/__tests__/assets.spec.ts +++ b/packages/playground/assets/__tests__/assets.spec.ts @@ -99,6 +99,12 @@ describe('css url() references', () => { expect(await getBg('.css-url-quotes-base64-inline')).toMatch(match) }) + test('multiple urls on the same line', async () => { + const bg = await getBg('.css-url-same-line') + expect(bg).toMatch(assetMatch) + expect(bg).toMatch(iconMatch) + }) + if (isBuild) { test('preserve postfix query/hash', () => { expect(findAssetFile(/\.css$/, 'foo')).toMatch(`woff2?#iefix`) diff --git a/packages/playground/assets/css/css-url.css b/packages/playground/assets/css/css-url.css index 5caf72cf8be2e3..683305fa687b95 100644 --- a/packages/playground/assets/css/css-url.css +++ b/packages/playground/assets/css/css-url.css @@ -29,6 +29,11 @@ background: url('../nested/icon.png'); background-size: 10px; } + +.css-url-same-line { + background: url('/nested/asset.png') top right / 10px no-repeat, url('/icon.png') bottom right / 10px no-repeat; +} + /* urls inside comments should be ignored diff --git a/packages/playground/assets/index.html b/packages/playground/assets/index.html index 808802ce3740fc..eeed653201796c 100644 --- a/packages/playground/assets/index.html +++ b/packages/playground/assets/index.html @@ -47,6 +47,11 @@

CSS url references

CSS background (base64 inline in prod)
+
+ CSS background (multiple urls on same line) +

SVG Fragments

diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index c621427e3ae5f2..7d368627aec5bb 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -10,11 +10,11 @@ import { PluginContext, RenderedChunk } from 'rollup' import MagicString from 'magic-string' import { createHash } from 'crypto' -export const assetUrlRE = /__VITE_ASSET__([a-z\d]{8})__(?:(.*?)__)?/g +export const assetUrlRE = /__VITE_ASSET__([a-z\d]{8})__(?:\$_(.*?)__)?/g // urls in JS must be quoted as strings, so when replacing them we need // a different regex -const assetUrlQuotedRE = /"__VITE_ASSET__([a-z\d]{8})__(?:(.*?)__)?"/g +const assetUrlQuotedRE = /"__VITE_ASSET__([a-z\d]{8})__(?:\$_(.*?)__)?"/g const rawRE = /(\?|&)raw(?:&|$)/ const urlRE = /(\?|&)url(?:&|$)/ @@ -234,7 +234,7 @@ async function fileToBuiltUrl( }) } - url = `__VITE_ASSET__${contentHash}__${postfix ? `${postfix}__` : ``}` + url = `__VITE_ASSET__${contentHash}__${postfix ? `$_${postfix}__` : ``}` } cache.set(id, url)