Skip to content

Commit

Permalink
fix: dont resolve imports with malformed URI
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Mar 23, 2024
1 parent dad7f4f commit 6c8c7b8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
5 changes: 1 addition & 4 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -40,7 +40,6 @@ import {
joinUrlSegments,
moduleListContains,
normalizePath,
partialEncodeURIPath,
prettifyUrl,
removeImportQuery,
removeTimestampQuery,
Expand Down Expand Up @@ -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, {
Expand Down
4 changes: 2 additions & 2 deletions playground/assets/__tests__/assets.spec.ts
Expand Up @@ -369,11 +369,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`),
)
})
})
Expand Down
Binary file added playground/assets/asset/percent%.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 13 additions & 3 deletions playground/assets/index.html
Expand Up @@ -135,19 +135,25 @@ <h2>CSS url references</h2>
<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>
<div>
<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>

Expand Down Expand Up @@ -442,12 +448,16 @@ <h3>assets in noscript</h3>
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)

Expand Down

0 comments on commit 6c8c7b8

Please sign in to comment.