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: dont resolve imports with malformed URI #16244

Merged
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
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function testDynamicImportMetaUrlWithQuery(name, i) {
// prettier-ignore
const metaUrl = new URL(`./nested/${name}.png?abc`, import.meta.url,)
text(`.dynamic-import-meta-url-${i}-query`, metaUrl)
document.querySelector(`.dynamic-import-meta-url-img-${i}-query`).src =
metaUrl
}
testDynamicImportMetaUrlWithQuery('icon', 1)
testDynamicImportMetaUrlWithQuery('asset', 2)
function testDynamicImportMetaUrlWithTernaryOperator(name, i) {
// prettier-ignore
const metaUrl = new URL(`./nested/${1 === 0 ? 'failed' : name}.png?abc`, import.meta.url,)
text(`.dynamic-import-meta-url-${i}-ternary`, metaUrl)
document.querySelector(`.dynamic-import-meta-url-img-${i}-ternary`).src =
metaUrl
}

These new URLs is transformed to import.meta.glob and then it is transformed to import url from './nested/テスト-測試-white space%.png'. These should be import url from './nested/テスト-測試-white space%25.png', but that change opens up a can of worms (#16243), so I simply renamed back these files and put a different file in a different directory.

File renamed without changes