Skip to content

Commit 16a73c0

Browse files
OnlyWicksapphi-red
andauthored
fix(assets): ensure ?no-inline is not included in the asset url in the production environment (#19496)
Co-authored-by: 翠 / green <[email protected]>
1 parent 9399cda commit 16a73c0

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

packages/vite/src/node/plugins/asset.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ async function fileToBuiltUrl(
403403
return cached
404404
}
405405

406-
const { file, postfix } = splitFileAndPostfix(id)
406+
let { file, postfix } = splitFileAndPostfix(id)
407407
const content = await fsp.readFile(file)
408408

409409
let url: string
@@ -423,6 +423,11 @@ async function fileToBuiltUrl(
423423
originalFileName,
424424
source: content,
425425
})
426+
427+
if (environment.config.command === 'build' && noInlineRE.test(postfix)) {
428+
postfix = postfix.replace(noInlineRE, '').replace(/^&/, '?')
429+
}
430+
426431
url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`
427432
}
428433

playground/assets/__tests__/assets.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,11 +472,19 @@ test('?raw import', async () => {
472472
test('?no-inline svg import', async () => {
473473
expect(await page.textContent('.no-inline-svg')).toMatch(
474474
isBuild
475-
? /\/foo\/bar\/assets\/fragment-[-\w]{8}\.svg\?no-inline/
475+
? /\/foo\/bar\/assets\/fragment-[-\w]{8}\.svg/
476476
: '/foo/bar/nested/fragment.svg?no-inline',
477477
)
478478
})
479479

480+
test('?no-inline svg import -- multiple postfix', async () => {
481+
expect(await page.textContent('.no-inline-svg-mp')).toMatch(
482+
isBuild
483+
? /\/foo\/bar\/assets\/fragment-[-\w]{8}\.svg\?foo=bar/
484+
: '/foo/bar/nested/fragment.svg?no-inline&foo=bar',
485+
)
486+
})
487+
480488
test('?inline png import', async () => {
481489
expect(await page.textContent('.inline-png')).toMatch(
482490
/^data:image\/png;base64,/,

playground/assets/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ <h2>?raw import</h2>
269269
<h2>?no-inline svg import</h2>
270270
<code class="no-inline-svg"></code>
271271

272+
<h2>?no-inline svg import -- multiple postfix</h2>
273+
<code class="no-inline-svg-mp"></code>
274+
272275
<h2>?inline png import</h2>
273276
<code class="inline-png"></code>
274277

@@ -546,6 +549,9 @@ <h3>assets in template</h3>
546549
import noInlineSvg from './nested/fragment.svg?no-inline'
547550
text('.no-inline-svg', noInlineSvg)
548551

552+
import noInlineSvgMP from './nested/fragment.svg?no-inline&foo=bar'
553+
text('.no-inline-svg-mp', noInlineSvgMP)
554+
549555
import inlinePng from './nested/asset.png?inline'
550556
text('.inline-png', inlinePng)
551557

0 commit comments

Comments
 (0)