Skip to content

Commit 0b2e40b

Browse files
authoredMar 20, 2024··
fix: encode path uri only (#16212)
1 parent e41d8cf commit 0b2e40b

File tree

7 files changed

+51
-27
lines changed

7 files changed

+51
-27
lines changed
 

‎packages/vite/src/node/build.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
emptyDir,
4141
joinUrlSegments,
4242
normalizePath,
43-
partialEncodeURI,
43+
partialEncodeURIPath,
4444
requireResolveFromRootWithFallback,
4545
} from './utils'
4646
import { manifestPlugin } from './plugins/manifest'
@@ -1093,7 +1093,7 @@ const getResolveUrl = (path: string, URL = 'URL') => `new ${URL}(${path}).href`
10931093

10941094
const getRelativeUrlFromDocument = (relativePath: string, umd = false) =>
10951095
getResolveUrl(
1096-
`'${escapeId(partialEncodeURI(relativePath))}', ${
1096+
`'${escapeId(partialEncodeURIPath(relativePath))}', ${
10971097
umd ? `typeof document === 'undefined' ? location.href : ` : ''
10981098
}document.currentScript && document.currentScript.src || document.baseURI`,
10991099
)
@@ -1120,13 +1120,13 @@ const relativeUrlMechanisms: Record<
11201120
)} : ${getRelativeUrlFromDocument(relativePath)})`,
11211121
es: (relativePath) =>
11221122
getResolveUrl(
1123-
`'${escapeId(partialEncodeURI(relativePath))}', import.meta.url`,
1123+
`'${escapeId(partialEncodeURIPath(relativePath))}', import.meta.url`,
11241124
),
11251125
iife: (relativePath) => getRelativeUrlFromDocument(relativePath),
11261126
// NOTE: make sure rollup generate `module` params
11271127
system: (relativePath) =>
11281128
getResolveUrl(
1129-
`'${escapeId(partialEncodeURI(relativePath))}', module.meta.url`,
1129+
`'${escapeId(partialEncodeURIPath(relativePath))}', module.meta.url`,
11301130
),
11311131
umd: (relativePath) =>
11321132
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(
@@ -1139,7 +1139,7 @@ const customRelativeUrlMechanisms = {
11391139
...relativeUrlMechanisms,
11401140
'worker-iife': (relativePath) =>
11411141
getResolveUrl(
1142-
`'${escapeId(partialEncodeURI(relativePath))}', self.location.href`,
1142+
`'${escapeId(partialEncodeURIPath(relativePath))}', self.location.href`,
11431143
),
11441144
} as const satisfies Record<string, (relativePath: string) => string>
11451145

‎packages/vite/src/node/plugins/asset.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { Plugin } from '../plugin'
1818
import type { ResolvedConfig } from '../config'
1919
import { checkPublicFile } from '../publicDir'
2020
import {
21+
encodeURIPath,
2122
getHash,
2223
injectQuery,
2324
joinUrlSegments,
@@ -100,7 +101,7 @@ export function renderAssetUrlInJS(
100101
)
101102
const replacementString =
102103
typeof replacement === 'string'
103-
? JSON.stringify(encodeURI(replacement)).slice(1, -1)
104+
? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
104105
: `"+${replacement.runtime}+"`
105106
s.update(match.index, match.index + full.length, replacementString)
106107
}
@@ -123,7 +124,7 @@ export function renderAssetUrlInJS(
123124
)
124125
const replacementString =
125126
typeof replacement === 'string'
126-
? JSON.stringify(encodeURI(replacement)).slice(1, -1)
127+
? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
127128
: `"+${replacement.runtime}+"`
128129
s.update(match.index, match.index + full.length, replacementString)
129130
}
@@ -207,7 +208,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
207208

208209
return {
209210
code: `export default ${JSON.stringify(
210-
url.startsWith('data:') ? url : encodeURI(url),
211+
url.startsWith('data:') ? url : encodeURIPath(url),
211212
)}`,
212213
// Force rollup to keep this module from being shared between other entry points if it's an entrypoint.
213214
// If the resulting chunk is empty, it will be removed in generateBundle.

‎packages/vite/src/node/plugins/css.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
combineSourcemaps,
5050
createSerialPromiseQueue,
5151
emptyCssComments,
52+
encodeURIPath,
5253
generateCodeFrame,
5354
getHash,
5455
getPackageManagerCommand,
@@ -593,7 +594,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
593594
chunkCSS = chunkCSS.replace(assetUrlRE, (_, fileHash, postfix = '') => {
594595
const filename = this.getFileName(fileHash) + postfix
595596
chunk.viteMetadata!.importedAssets.add(cleanUrl(filename))
596-
return encodeURI(
597+
return encodeURIPath(
597598
toOutputFilePathInCss(
598599
filename,
599600
'asset',
@@ -612,7 +613,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
612613
)
613614
chunkCSS = chunkCSS.replace(publicAssetUrlRE, (_, hash) => {
614615
const publicUrl = publicAssetUrlMap.get(hash)!.slice(1)
615-
return encodeURI(
616+
return encodeURIPath(
616617
toOutputFilePathInCss(
617618
publicUrl,
618619
'public',
@@ -715,7 +716,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
715716
)
716717
const replacementString =
717718
typeof replacement === 'string'
718-
? JSON.stringify(encodeURI(replacement)).slice(1, -1)
719+
? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
719720
: `"+${replacement.runtime}+"`
720721
s.update(start, end, replacementString)
721722
}

‎packages/vite/src/node/plugins/html.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import { stripLiteral } from 'strip-literal'
1313
import type { Plugin } from '../plugin'
1414
import type { ViteDevServer } from '../server'
1515
import {
16+
encodeURIPath,
1617
generateCodeFrame,
1718
getHash,
1819
isDataUrl,
1920
isExternalUrl,
2021
normalizePath,
21-
partialEncodeURI,
22+
partialEncodeURIPath,
2223
processSrcSet,
2324
removeLeadingSlash,
2425
urlCanParse,
@@ -439,7 +440,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
439440
overwriteAttrValue(
440441
s,
441442
sourceCodeLocation!,
442-
partialEncodeURI(toOutputPublicFilePath(url)),
443+
partialEncodeURIPath(toOutputPublicFilePath(url)),
443444
)
444445
}
445446

@@ -498,7 +499,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
498499
if (!isExcludedUrl(decodedUrl)) {
499500
const result = await processAssetUrl(url)
500501
return result !== decodedUrl
501-
? encodeURI(result)
502+
? encodeURIPath(result)
502503
: url
503504
}
504505
return url
@@ -519,7 +520,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
519520
overwriteAttrValue(
520521
s,
521522
getAttrSourceCodeLocation(node, attrKey),
522-
partialEncodeURI(toOutputPublicFilePath(url)),
523+
partialEncodeURIPath(toOutputPublicFilePath(url)),
523524
)
524525
} else if (!isExcludedUrl(url)) {
525526
if (
@@ -563,7 +564,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
563564
overwriteAttrValue(
564565
s,
565566
getAttrSourceCodeLocation(node, attrKey),
566-
partialEncodeURI(processedUrl),
567+
partialEncodeURIPath(processedUrl),
567568
)
568569
}
569570
})(),
@@ -636,12 +637,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
636637
// emit <script>import("./aaa")</script> asset
637638
for (const { start, end, url } of scriptUrls) {
638639
if (checkPublicFile(url, config)) {
639-
s.update(start, end, partialEncodeURI(toOutputPublicFilePath(url)))
640+
s.update(
641+
start,
642+
end,
643+
partialEncodeURIPath(toOutputPublicFilePath(url)),
644+
)
640645
} else if (!isExcludedUrl(url)) {
641646
s.update(
642647
start,
643648
end,
644-
partialEncodeURI(await urlToBuiltUrl(url, id, config, this)),
649+
partialEncodeURIPath(await urlToBuiltUrl(url, id, config, this)),
645650
)
646651
}
647652
}
@@ -904,15 +909,15 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
904909
if (chunk) {
905910
chunk.viteMetadata!.importedAssets.add(cleanUrl(file))
906911
}
907-
return encodeURI(toOutputAssetFilePath(file)) + postfix
912+
return encodeURIPath(toOutputAssetFilePath(file)) + postfix
908913
})
909914

910915
result = result.replace(publicAssetUrlRE, (_, fileHash) => {
911916
const publicAssetPath = toOutputPublicAssetFilePath(
912917
getPublicAssetFilename(fileHash, config)!,
913918
)
914919

915-
return encodeURI(
920+
return encodeURIPath(
916921
urlCanParse(publicAssetPath)
917922
? publicAssetPath
918923
: normalizePath(publicAssetPath),

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
joinUrlSegments,
4141
moduleListContains,
4242
normalizePath,
43-
partialEncodeURI,
43+
partialEncodeURIPath,
4444
prettifyUrl,
4545
removeImportQuery,
4646
removeTimestampQuery,
@@ -594,7 +594,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
594594
rewriteDone = true
595595
}
596596
if (!rewriteDone) {
597-
const rewrittenUrl = JSON.stringify(partialEncodeURI(url))
597+
const rewrittenUrl = JSON.stringify(partialEncodeURIPath(url))
598598
const s = isDynamicImport ? start : start - 1
599599
const e = isDynamicImport ? end : end + 1
600600
str().overwrite(s, e, rewrittenUrl, {

‎packages/vite/src/node/plugins/worker.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import type { ResolvedConfig } from '../config'
55
import type { Plugin } from '../plugin'
66
import type { ViteDevServer } from '../server'
77
import { ENV_ENTRY, ENV_PUBLIC_PATH } from '../constants'
8-
import { getHash, injectQuery, prettifyUrl, urlRE } from '../utils'
8+
import {
9+
encodeURIPath,
10+
getHash,
11+
injectQuery,
12+
prettifyUrl,
13+
urlRE,
14+
} from '../utils'
915
import {
1016
createToImportMetaURLBasedRelativeRuntime,
1117
onRollupWarning,
@@ -411,7 +417,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
411417
)
412418
const replacementString =
413419
typeof replacement === 'string'
414-
? JSON.stringify(encodeURI(replacement)).slice(1, -1)
420+
? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
415421
: `"+${replacement.runtime}+"`
416422
s.update(match.index, match.index + full.length, replacementString)
417423
}

‎packages/vite/src/node/utils.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1418,9 +1418,20 @@ export function displayTime(time: number): string {
14181418
}
14191419

14201420
/**
1421-
* Like `encodeURI`, but only replacing `%` as `%25`. This is useful for environments
1421+
* Encodes the URI path portion (ignores part after ? or #)
1422+
*/
1423+
export function encodeURIPath(uri: string): string {
1424+
const filePath = cleanUrl(uri)
1425+
const postfix = filePath !== uri ? uri.slice(filePath.length) : ''
1426+
return encodeURI(filePath) + postfix
1427+
}
1428+
1429+
/**
1430+
* Like `encodeURIPath`, but only replacing `%` as `%25`. This is useful for environments
14221431
* that can handle un-encoded URIs, where `%` is the only ambiguous character.
14231432
*/
1424-
export function partialEncodeURI(uri: string): string {
1425-
return uri.replaceAll('%', '%25')
1433+
export function partialEncodeURIPath(uri: string): string {
1434+
const filePath = cleanUrl(uri)
1435+
const postfix = filePath !== uri ? uri.slice(filePath.length) : ''
1436+
return filePath.replaceAll('%', '%25') + postfix
14261437
}

0 commit comments

Comments
 (0)
Please sign in to comment.