-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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: sanitize asset filenames #9737
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions
27
playground/assets-sanitize/__tests__/assets-sanitize.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { expect, test } from 'vitest' | ||
import { getBg, isBuild, page, readManifest } from '~utils' | ||
|
||
if (!isBuild) { | ||
test('importing asset with special char in filename works in dev', async () => { | ||
expect(await getBg('.plus-circle')).toContain('+circle.svg') | ||
expect(await page.textContent('.plus-circle')).toMatch('+circle.svg') | ||
expect(await getBg('.underscore-circle')).toContain('_circle.svg') | ||
expect(await page.textContent('.underscore-circle')).toMatch('_circle.svg') | ||
}) | ||
} else { | ||
test('importing asset with special char in filename works in build', async () => { | ||
const manifest = readManifest() | ||
const plusCircleAsset = manifest['+circle.svg'].file | ||
const underscoreCircleAsset = manifest['_circle.svg'].file | ||
expect(await getBg('.plus-circle')).toMatch(plusCircleAsset) | ||
expect(await page.textContent('.plus-circle')).toMatch(plusCircleAsset) | ||
expect(await getBg('.underscore-circle')).toMatch(underscoreCircleAsset) | ||
expect(await page.textContent('.underscore-circle')).toMatch( | ||
underscoreCircleAsset | ||
) | ||
expect(plusCircleAsset).toMatch('/_circle') | ||
expect(underscoreCircleAsset).toMatch('/_circle') | ||
expect(plusCircleAsset).not.toEqual(underscoreCircleAsset) | ||
expect(Object.keys(manifest).length).toBe(3) // 2 svg, 1 index.js | ||
}) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script type="module" src="./index.js"></script> | ||
<style> | ||
.test-el { | ||
background-repeat: no-repeat; | ||
padding-left: 2rem; | ||
margin-bottom: 1rem; | ||
} | ||
</style> | ||
<h1>test elements below should show circles and their url</h1> | ||
<div class="test-el plus-circle"></div> | ||
<div class="test-el underscore-circle"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import plusCircle from './+circle.svg' | ||
import underscoreCircle from './_circle.svg' | ||
function setData(classname, file) { | ||
const el = document.body.querySelector(classname) | ||
el.style.backgroundImage = `url(${file})` | ||
el.textContent = file | ||
} | ||
setData('.plus-circle', plusCircle) | ||
setData('.underscore-circle', underscoreCircle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "test-assets-sanitize", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"debug": "node --inspect-brk ../../packages/vite/bin/vite", | ||
"preview": "vite preview" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { defineConfig } = require('vite') | ||
|
||
module.exports = defineConfig({ | ||
build: { | ||
//speed up build | ||
minify: false, | ||
target: 'esnext', | ||
assetsInlineLimit: 0, | ||
manifest: true | ||
} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we use
output.sanitizeFileName
here to support custom functions by the user? (and also avoid the need to replicate the default in our code)https://rollupjs.org/guide/en/#outputsanitizefilename
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussing with @dominikg in Vite Land, we don't have access to Rollup's default. We can also leave support for custom sanitize functions for another PR. I think we can merge this PR and then work on extending support later.