From 133bdc4305de0d9d5a24aefde7ac16e32d5526ec Mon Sep 17 00:00:00 2001 From: Wick Date: Sun, 23 Feb 2025 20:07:00 +0800 Subject: [PATCH 1/2] feat(assets): ensure ?no-inline is not included in the asset url in the production environment --- packages/vite/src/node/plugins/asset.ts | 10 +++++++++- playground/assets/__tests__/assets.spec.ts | 10 +++++++++- playground/assets/index.html | 6 ++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index b9447d9cf17f68..d91fe82088acd9 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -381,7 +381,7 @@ async function fileToBuiltUrl( return cached } - const { file, postfix } = splitFileAndPostfix(id) + let { file, postfix } = splitFileAndPostfix(id) const content = await fsp.readFile(file) let url: string @@ -401,6 +401,14 @@ async function fileToBuiltUrl( originalFileName, source: content, }) + + if (environment.config.command === 'build' && noInlineRE.test(postfix)) { + postfix = postfix.replace(noInlineRE, '') + if (postfix.startsWith('&')) { + postfix = postfix.replace(/^&/, '?') + } + } + url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}` } diff --git a/playground/assets/__tests__/assets.spec.ts b/playground/assets/__tests__/assets.spec.ts index d1a1c477229609..ba519377715c43 100644 --- a/playground/assets/__tests__/assets.spec.ts +++ b/playground/assets/__tests__/assets.spec.ts @@ -473,11 +473,19 @@ test('?raw import', async () => { test('?no-inline svg import', async () => { expect(await page.textContent('.no-inline-svg')).toMatch( isBuild - ? /\/foo\/bar\/assets\/fragment-[-\w]{8}\.svg\?no-inline/ + ? /\/foo\/bar\/assets\/fragment-[-\w]{8}\.svg/ : '/foo/bar/nested/fragment.svg?no-inline', ) }) +test('?no-inline svg import -- multiple postfix', async () => { + expect(await page.textContent('.no-inline-svg-mp')).toMatch( + isBuild + ? /\/foo\/bar\/assets\/fragment-[-\w]{8}\.svg\?foo=bar/ + : '/foo/bar/nested/fragment.svg?no-inline&foo=bar', + ) +}) + test('?inline png import', async () => { expect(await page.textContent('.inline-png')).toMatch( /^data:image\/png;base64,/, diff --git a/playground/assets/index.html b/playground/assets/index.html index bf0b4b5a4315cf..3a5451dccd1b91 100644 --- a/playground/assets/index.html +++ b/playground/assets/index.html @@ -269,6 +269,9 @@

?raw import

?no-inline svg import

+

?no-inline svg import -- multiple postfix

+ +

?inline png import

@@ -546,6 +549,9 @@

assets in template

import noInlineSvg from './nested/fragment.svg?no-inline' text('.no-inline-svg', noInlineSvg) + import noInlineSvgMP from './nested/fragment.svg?no-inline&foo=bar' + text('.no-inline-svg-mp', noInlineSvgMP) + import inlinePng from './nested/asset.png?inline' text('.inline-png', inlinePng) From b57c6a45c94e5ade66fc284382f3b1ab65fad761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Wed, 26 Feb 2025 15:39:49 +0900 Subject: [PATCH 2/2] chore: merge lines --- packages/vite/src/node/plugins/asset.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index d91fe82088acd9..484b6d27a007fc 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -403,10 +403,7 @@ async function fileToBuiltUrl( }) if (environment.config.command === 'build' && noInlineRE.test(postfix)) { - postfix = postfix.replace(noInlineRE, '') - if (postfix.startsWith('&')) { - postfix = postfix.replace(/^&/, '?') - } + postfix = postfix.replace(noInlineRE, '').replace(/^&/, '?') } url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`