From 81b3bd09cff926534ea667edfa5417b944cdf01c Mon Sep 17 00:00:00 2001 From: Murad Khafizov <101127600+murad-khafizov@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:15:29 +0100 Subject: [PATCH] fix: srcSet with optional descriptor (#15905) Co-authored-by: patak --- packages/vite/src/node/__tests__/utils.spec.ts | 7 +++++++ packages/vite/src/node/utils.ts | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 3efdbdd77c2409..35fdd3d3f97249 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -338,6 +338,13 @@ describe('processSrcSetSync', () => { 'data:image/avif;base64,aA+/0= 400w, data:image/avif;base64,bB+/9= 800w' expect(processSrcSetSync(base64, ({ url }) => url)).toBe(base64) }) + + test('should not break a regular URL in srcSet', async () => { + const source = 'https://anydomain/image.jpg' + expect( + processSrcSetSync('https://anydomain/image.jpg', ({ url }) => url), + ).toBe(source) + }) }) describe('flattenId', () => { diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 6f8004566cb6f9..bc85a3b38d37b4 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -767,7 +767,9 @@ interface ImageCandidate { const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g const imageSetUrlRE = /^(?:[\w\-]+\(.*?\)|'.*?'|".*?"|\S*)/ function joinSrcset(ret: ImageCandidate[]) { - return ret.map(({ url, descriptor }) => `${url} ${descriptor}`).join(', ') + return ret + .map(({ url, descriptor }) => url + (descriptor ? ` ${descriptor}` : '')) + .join(', ') } function splitSrcSetDescriptor(srcs: string): ImageCandidate[] {