Skip to content

Commit

Permalink
fix: escape single quote when relative base is used (#16060)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Mar 5, 2024
1 parent 2af1ae8 commit 8f74ce4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/vite/src/node/build.ts
Expand Up @@ -1070,7 +1070,7 @@ function injectSsrFlag<T extends Record<string, any>>(

/*
The following functions are copied from rollup
https://github.com/rollup/rollup/blob/0bcf0a672ac087ff2eb88fbba45ec62389a4f45f/src/ast/nodes/MetaProperty.ts#L145-L193
https://github.com/rollup/rollup/blob/ce6cb93098850a46fa242e37b74a919e99a5de28/src/ast/nodes/MetaProperty.ts#L155-L203
https://github.com/rollup/rollup
The MIT License (MIT)
Expand Down Expand Up @@ -1101,24 +1101,28 @@ const getFileUrlFromFullPath = (path: string) =>
`require('u' + 'rl').pathToFileURL(${path}).href`

const getFileUrlFromRelativePath = (path: string) =>
getFileUrlFromFullPath(`__dirname + '/${path}'`)
getFileUrlFromFullPath(`__dirname + '/${escapeId(path)}'`)

const relativeUrlMechanisms: Record<
InternalModuleFormat,
(relativePath: string) => string
> = {
amd: (relativePath) => {
if (relativePath[0] !== '.') relativePath = './' + relativePath
return getResolveUrl(`require.toUrl('${relativePath}'), document.baseURI`)
return getResolveUrl(
`require.toUrl('${escapeId(relativePath)}'), document.baseURI`,
)
},
cjs: (relativePath) =>
`(typeof document === 'undefined' ? ${getFileUrlFromRelativePath(
relativePath,
)} : ${getRelativeUrlFromDocument(relativePath)})`,
es: (relativePath) => getResolveUrl(`'${relativePath}', import.meta.url`),
es: (relativePath) =>
getResolveUrl(`'${escapeId(relativePath)}', import.meta.url`),
iife: (relativePath) => getRelativeUrlFromDocument(relativePath),
// NOTE: make sure rollup generate `module` params
system: (relativePath) => getResolveUrl(`'${relativePath}', module.meta.url`),
system: (relativePath) =>
getResolveUrl(`'${escapeId(relativePath)}', module.meta.url`),
umd: (relativePath) =>
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(
relativePath,
Expand All @@ -1129,7 +1133,7 @@ const relativeUrlMechanisms: Record<
const customRelativeUrlMechanisms = {
...relativeUrlMechanisms,
'worker-iife': (relativePath) =>
getResolveUrl(`'${relativePath}', self.location.href`),
getResolveUrl(`'${escapeId(relativePath)}', self.location.href`),
} as const satisfies Record<string, (relativePath: string) => string>

export type RenderBuiltAssetUrl = (
Expand Down
8 changes: 8 additions & 0 deletions playground/assets/index.html
Expand Up @@ -138,6 +138,11 @@ <h2>Unicode URL</h2>
<img src="./nested/テスト-測試-white space.png" />
</div>

<h2>Filename including single quote</h2>
<div>
<code class="filename-including-single-quote"></code>
</div>

<h2>encodeURI for the address</h2>
<div>
<img
Expand Down Expand Up @@ -440,6 +445,9 @@ <h3>assets in noscript</h3>
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)

import cssUrl from './css/icons.css?url'
text('.url-css', cssUrl)

Expand Down
Binary file added playground/assets/nested/with-single'quote.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8f74ce4

Please sign in to comment.