Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip encode if is data uri #16233

Merged
merged 1 commit into from Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/vite/src/node/plugins/asset.ts
Expand Up @@ -207,9 +207,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
}

return {
code: `export default ${JSON.stringify(
url.startsWith('data:') ? url : encodeURIPath(url),
)}`,
code: `export default ${JSON.stringify(encodeURIPath(url))}`,
// Force rollup to keep this module from being shared between other entry points if it's an entrypoint.
// If the resulting chunk is empty, it will be removed in generateBundle.
moduleSideEffects:
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -1421,6 +1421,7 @@ export function displayTime(time: number): string {
* Encodes the URI path portion (ignores part after ? or #)
*/
export function encodeURIPath(uri: string): string {
if (uri.startsWith('data:')) return uri
const filePath = cleanUrl(uri)
const postfix = filePath !== uri ? uri.slice(filePath.length) : ''
return encodeURI(filePath) + postfix
Expand All @@ -1431,6 +1432,7 @@ export function encodeURIPath(uri: string): string {
* that can handle un-encoded URIs, where `%` is the only ambiguous character.
*/
export function partialEncodeURIPath(uri: string): string {
if (uri.startsWith('data:')) return uri
const filePath = cleanUrl(uri)
const postfix = filePath !== uri ? uri.slice(filePath.length) : ''
return filePath.replaceAll('%', '%25') + postfix
Expand Down
20 changes: 20 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Expand Up @@ -282,6 +282,26 @@ describe('css url() references', () => {
})

describe('image', () => {
test('src', async () => {
const img = await page.$('.img-src')
const src = await img.getAttribute('src')
expect(src).toMatch(
isBuild
? /\/foo\/bar\/assets\/html-only-asset-[-\w]{8}\.jpg/
: /\/foo\/bar\/nested\/html-only-asset.jpg/,
)
})

test('src inline', async () => {
const img = await page.$('.img-src-inline')
const src = await img.getAttribute('src')
expect(src).toMatch(
isBuild
? /^data:image\/svg\+xml,%3csvg/
: /\/foo\/bar\/nested\/inlined.svg/,
)
})

test('srcset', async () => {
const img = await page.$('.img-src-set')
const srcset = await img.getAttribute('srcset')
Expand Down
7 changes: 6 additions & 1 deletion playground/assets/index.html
Expand Up @@ -175,7 +175,12 @@ <h2>Image Src Set</h2>

<h2>HTML only asset</h2>
<div>
<img src="./nested/html-only-asset.jpg" alt="" />
<img class="img-src" src="./nested/html-only-asset.jpg" alt="" />
</div>

<h2>HTML inline asset</h2>
<div>
<img class="img-src-inline" src="./nested/inlined.svg" alt="" />
</div>

<h2>SVG Fragments</h2>
Expand Down
12 changes: 12 additions & 0 deletions playground/assets/nested/inlined.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.