fix: dont resolve imports with malformed URI (#16244)

This commit is contained in:
翠 / green 2024-03-23 18:56:58 +09:00 committed by GitHub
parent a67f9f6945
commit fbf69d5f6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 16 additions and 9 deletions

View File

@ -40,7 +40,6 @@ import {
joinUrlSegments,
moduleListContains,
normalizePath,
partialEncodeURIPath,
prettifyUrl,
removeImportQuery,
removeTimestampQuery,
@ -594,9 +593,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
rewriteDone = true
}
if (!rewriteDone) {
const rewrittenUrl = JSON.stringify(
ssr ? url : partialEncodeURIPath(url),
)
const rewrittenUrl = JSON.stringify(url)
const s = isDynamicImport ? start : start - 1
const e = isDynamicImport ? end : end + 1
str().overwrite(s, e, rewrittenUrl, {

View File

@ -389,11 +389,11 @@ test('?url import on css', async () => {
describe('unicode url', () => {
test('from js import', async () => {
const src = readFile('テスト-測試-white space%.js')
const src = readFile('テスト-測試-white space.js')
expect(await page.textContent('.unicode-url')).toMatch(
isBuild
? `data:text/javascript;base64,${Buffer.from(src).toString('base64')}`
: encodeURI(`/foo/bar/テスト-測試-white space%.js`),
: encodeURI(`/foo/bar/テスト-測試-white space.js`),
)
})
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 KiB

View File

@ -135,7 +135,7 @@
<h2>Unicode URL</h2>
<div>
<code class="unicode-url"></code>
<img src="./nested/テスト-測試-white space%25.png" />
<img src="./nested/テスト-測試-white space.png" />
</div>
<h2>Filename including single quote</h2>
@ -143,11 +143,17 @@
<code class="filename-including-single-quote"></code>
</div>
<h2>Filename including percent</h2>
<div>
<code class="percent-url"></code>
<img src="./asset/percent%25.png" />
</div>
<h2>encodeURI for the address</h2>
<div>
<img
class="encodeURI"
src="./nested/%E3%83%86%E3%82%B9%E3%83%88-%E6%B8%AC%E8%A9%A6-white%20space%25.png"
src="./nested/%E3%83%86%E3%82%B9%E3%83%88-%E6%B8%AC%E8%A9%A6-white%20space.png"
/>
</div>
@ -447,12 +453,16 @@
import fooUrl from './foo.js?url'
text('.url', fooUrl)
import unicodeUrl from './テスト-測試-white space%.js?url'
import unicodeUrl from './テスト-測試-white space.js?url'
text('.unicode-url', unicodeUrl)
import filenameIncludingSingleQuoteUrl from "./nested/with-single'quote.png"
text('.filename-including-single-quote', filenameIncludingSingleQuoteUrl)
// TODO: is not supported yet (https://github.com/vitejs/vite/pull/16243)
// import percentUrl from './asset/percent%25.png?url'
// text('.percent-url', percentUrl)
import cssUrl from './css/icons.css?url'
text('.url-css', cssUrl)

View File

Before

Width:  |  Height:  |  Size: 4.9 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB