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: escape single quote when relative base is used #16060

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
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`

sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.