From b1964f869541eb29ca252e97525a0af5e82e65a4 Mon Sep 17 00:00:00 2001 From: Dev-MV6 <138727393+Dev-MV6@users.noreply.github.com> Date: Thu, 11 Apr 2024 22:15:19 -0500 Subject: [PATCH] fix: encode filename only --- packages/vite/src/node/plugins/asset.ts | 10 ++++--- packages/vite/src/node/plugins/css.ts | 36 +++++++++++------------- packages/vite/src/node/plugins/html.ts | 18 ++++++------ packages/vite/src/node/plugins/worker.ts | 4 +-- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 2ff5b101529982..72563cd94448ec 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -92,17 +92,19 @@ export function renderAssetUrlInJS( chunk.viteMetadata!.importedAssets.add(cleanUrl(file)) const filename = file + postfix const replacement = toOutputFilePathInJS( - filename, + encodeURIPath(filename), 'asset', chunk.fileName, 'js', config, toRelativeRuntime, ) + const replacementString = typeof replacement === 'string' - ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) + ? JSON.stringify(replacement).slice(1, -1) : `"+${replacement.runtime}+"` + s.update(match.index, match.index + full.length, replacementString) } @@ -115,7 +117,7 @@ export function renderAssetUrlInJS( const [full, hash] = match const publicUrl = publicAssetUrlMap.get(hash)!.slice(1) const replacement = toOutputFilePathInJS( - publicUrl, + encodeURIPath(publicUrl), 'public', chunk.fileName, 'js', @@ -124,7 +126,7 @@ export function renderAssetUrlInJS( ) const replacementString = typeof replacement === 'string' - ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) + ? JSON.stringify(replacement).slice(1, -1) : `"+${replacement.runtime}+"` s.update(match.index, match.index + full.length, replacementString) } diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 0e97c247cf01f8..452d374b09463a 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -594,15 +594,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { chunkCSS = chunkCSS.replace(assetUrlRE, (_, fileHash, postfix = '') => { const filename = this.getFileName(fileHash) + postfix chunk.viteMetadata!.importedAssets.add(cleanUrl(filename)) - return encodeURIPath( - toOutputFilePathInCss( - filename, - 'asset', - cssAssetName, - 'css', - config, - toRelative, - ), + return toOutputFilePathInCss( + encodeURIPath(filename), + 'asset', + cssAssetName, + 'css', + config, + toRelative, ) }) // resolve public URL from CSS paths @@ -613,15 +611,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { ) chunkCSS = chunkCSS.replace(publicAssetUrlRE, (_, hash) => { const publicUrl = publicAssetUrlMap.get(hash)!.slice(1) - return encodeURIPath( - toOutputFilePathInCss( - publicUrl, - 'public', - cssAssetName, - 'css', - config, - () => `${relativePathToPublicFromCSS}/${publicUrl}`, - ), + return toOutputFilePathInCss( + encodeURIPath(publicUrl), + 'public', + cssAssetName, + 'css', + config, + () => `${relativePathToPublicFromCSS}/${publicUrl}`, ) }) } @@ -707,7 +703,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { .set(referenceId, { originalName: originalFilename }) const replacement = toOutputFilePathInJS( - this.getFileName(referenceId), + encodeURIPath(this.getFileName(referenceId)), 'asset', chunk.fileName, 'js', @@ -716,7 +712,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { ) const replacementString = typeof replacement === 'string' - ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) + ? JSON.stringify(replacement).slice(1, -1) : `"+${replacement.runtime}+"` s.update(start, end, replacementString) } diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 232f9cc0e037a3..22221ec7b51a8a 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -440,7 +440,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { overwriteAttrValue( s, sourceCodeLocation!, - partialEncodeURIPath(toOutputPublicFilePath(url)), + toOutputPublicFilePath(partialEncodeURIPath(url)), ) } @@ -520,7 +520,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { overwriteAttrValue( s, getAttrSourceCodeLocation(node, attrKey), - partialEncodeURIPath(toOutputPublicFilePath(url)), + toOutputPublicFilePath(partialEncodeURIPath(url)), ) } else if (!isExcludedUrl(url)) { if ( @@ -640,7 +640,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { s.update( start, end, - partialEncodeURIPath(toOutputPublicFilePath(url)), + toOutputPublicFilePath(partialEncodeURIPath(url)), ) } else if (!isExcludedUrl(url)) { s.update( @@ -909,19 +909,17 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { if (chunk) { chunk.viteMetadata!.importedAssets.add(cleanUrl(file)) } - return encodeURIPath(toOutputAssetFilePath(file)) + postfix + return toOutputAssetFilePath(encodeURIPath(file)) + postfix }) result = result.replace(publicAssetUrlRE, (_, fileHash) => { const publicAssetPath = toOutputPublicAssetFilePath( - getPublicAssetFilename(fileHash, config)!, + encodeURIPath(getPublicAssetFilename(fileHash, config)!), ) - return encodeURIPath( - urlCanParse(publicAssetPath) - ? publicAssetPath - : normalizePath(publicAssetPath), - ) + return urlCanParse(publicAssetPath) + ? publicAssetPath + : normalizePath(publicAssetPath) }) if (chunk && canInlineEntry) { diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 4094b581a52b63..47086f889f0f90 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -408,7 +408,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { const [full, hash] = match const filename = fileNameHash.get(hash)! const replacement = toOutputFilePathInJS( - filename, + encodeURIPath(filename), 'asset', chunk.fileName, 'js', @@ -417,7 +417,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { ) const replacementString = typeof replacement === 'string' - ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) + ? JSON.stringify(replacement).slice(1, -1) : `"+${replacement.runtime}+"` s.update(match.index, match.index + full.length, replacementString) }