Skip to content

Commit fbf69d5

Browse files
authored
fix: dont resolve imports with malformed URI (#16244)
1 parent a67f9f6 commit fbf69d5

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import {
4040
joinUrlSegments,
4141
moduleListContains,
4242
normalizePath,
43-
partialEncodeURIPath,
4443
prettifyUrl,
4544
removeImportQuery,
4645
removeTimestampQuery,
@@ -594,9 +593,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
594593
rewriteDone = true
595594
}
596595
if (!rewriteDone) {
597-
const rewrittenUrl = JSON.stringify(
598-
ssr ? url : partialEncodeURIPath(url),
599-
)
596+
const rewrittenUrl = JSON.stringify(url)
600597
const s = isDynamicImport ? start : start - 1
601598
const e = isDynamicImport ? end : end + 1
602599
str().overwrite(s, e, rewrittenUrl, {

playground/assets/__tests__/assets.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,11 @@ test('?url import on css', async () => {
389389

390390
describe('unicode url', () => {
391391
test('from js import', async () => {
392-
const src = readFile('テスト-測試-white space%.js')
392+
const src = readFile('テスト-測試-white space.js')
393393
expect(await page.textContent('.unicode-url')).toMatch(
394394
isBuild
395395
? `data:text/javascript;base64,${Buffer.from(src).toString('base64')}`
396-
: encodeURI(`/foo/bar/テスト-測試-white space%.js`),
396+
: encodeURI(`/foo/bar/テスト-測試-white space.js`),
397397
)
398398
})
399399
})

playground/assets/asset/percent%.png

157 KB
Loading

playground/assets/index.html

+13-3
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,25 @@ <h2>CSS url references</h2>
135135
<h2>Unicode URL</h2>
136136
<div>
137137
<code class="unicode-url"></code>
138-
<img src="./nested/テスト-測試-white space%25.png" />
138+
<img src="./nested/テスト-測試-white space.png" />
139139
</div>
140140

141141
<h2>Filename including single quote</h2>
142142
<div>
143143
<code class="filename-including-single-quote"></code>
144144
</div>
145145

146+
<h2>Filename including percent</h2>
147+
<div>
148+
<code class="percent-url"></code>
149+
<img src="./asset/percent%25.png" />
150+
</div>
151+
146152
<h2>encodeURI for the address</h2>
147153
<div>
148154
<img
149155
class="encodeURI"
150-
src="./nested/%E3%83%86%E3%82%B9%E3%83%88-%E6%B8%AC%E8%A9%A6-white%20space%25.png"
156+
src="./nested/%E3%83%86%E3%82%B9%E3%83%88-%E6%B8%AC%E8%A9%A6-white%20space.png"
151157
/>
152158
</div>
153159

@@ -447,12 +453,16 @@ <h3>assets in noscript</h3>
447453
import fooUrl from './foo.js?url'
448454
text('.url', fooUrl)
449455

450-
import unicodeUrl from './テスト-測試-white space%.js?url'
456+
import unicodeUrl from './テスト-測試-white space.js?url'
451457
text('.unicode-url', unicodeUrl)
452458

453459
import filenameIncludingSingleQuoteUrl from "./nested/with-single'quote.png"
454460
text('.filename-including-single-quote', filenameIncludingSingleQuoteUrl)
455461

462+
// TODO: is not supported yet (https://github.com/vitejs/vite/pull/16243)
463+
// import percentUrl from './asset/percent%25.png?url'
464+
// text('.percent-url', percentUrl)
465+
456466
import cssUrl from './css/icons.css?url'
457467
text('.url-css', cssUrl)
458468

0 commit comments

Comments
 (0)