Skip to content

Commit 8617e76

Browse files
authoredMar 22, 2024··
fix: skip encode if is data uri (#16233)
1 parent f184c80 commit 8617e76

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed
 

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
207207
}
208208

209209
return {
210-
code: `export default ${JSON.stringify(
211-
url.startsWith('data:') ? url : encodeURIPath(url),
212-
)}`,
210+
code: `export default ${JSON.stringify(encodeURIPath(url))}`,
213211
// Force rollup to keep this module from being shared between other entry points if it's an entrypoint.
214212
// If the resulting chunk is empty, it will be removed in generateBundle.
215213
moduleSideEffects:

‎packages/vite/src/node/utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,7 @@ export function displayTime(time: number): string {
14211421
* Encodes the URI path portion (ignores part after ? or #)
14221422
*/
14231423
export function encodeURIPath(uri: string): string {
1424+
if (uri.startsWith('data:')) return uri
14241425
const filePath = cleanUrl(uri)
14251426
const postfix = filePath !== uri ? uri.slice(filePath.length) : ''
14261427
return encodeURI(filePath) + postfix
@@ -1431,6 +1432,7 @@ export function encodeURIPath(uri: string): string {
14311432
* that can handle un-encoded URIs, where `%` is the only ambiguous character.
14321433
*/
14331434
export function partialEncodeURIPath(uri: string): string {
1435+
if (uri.startsWith('data:')) return uri
14341436
const filePath = cleanUrl(uri)
14351437
const postfix = filePath !== uri ? uri.slice(filePath.length) : ''
14361438
return filePath.replaceAll('%', '%25') + postfix

‎playground/assets/__tests__/assets.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,26 @@ describe('css url() references', () => {
282282
})
283283

284284
describe('image', () => {
285+
test('src', async () => {
286+
const img = await page.$('.img-src')
287+
const src = await img.getAttribute('src')
288+
expect(src).toMatch(
289+
isBuild
290+
? /\/foo\/bar\/assets\/html-only-asset-[-\w]{8}\.jpg/
291+
: /\/foo\/bar\/nested\/html-only-asset.jpg/,
292+
)
293+
})
294+
295+
test('src inline', async () => {
296+
const img = await page.$('.img-src-inline')
297+
const src = await img.getAttribute('src')
298+
expect(src).toMatch(
299+
isBuild
300+
? /^data:image\/svg\+xml,%3csvg/
301+
: /\/foo\/bar\/nested\/inlined.svg/,
302+
)
303+
})
304+
285305
test('srcset', async () => {
286306
const img = await page.$('.img-src-set')
287307
const srcset = await img.getAttribute('srcset')

‎playground/assets/index.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,12 @@ <h2>Image Src Set</h2>
175175

176176
<h2>HTML only asset</h2>
177177
<div>
178-
<img src="./nested/html-only-asset.jpg" alt="" />
178+
<img class="img-src" src="./nested/html-only-asset.jpg" alt="" />
179+
</div>
180+
181+
<h2>HTML inline asset</h2>
182+
<div>
183+
<img class="img-src-inline" src="./nested/inlined.svg" alt="" />
179184
</div>
180185

181186
<h2>SVG Fragments</h2>

‎playground/assets/nested/inlined.svg

+12
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.