Skip to content

Commit 81e44dd

Browse files
authored
fix: take in relative assets path fixes from rollup (#12695)
1 parent 7ce9b00 commit 81e44dd

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

packages/vite/src/node/build.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ function injectSsrFlag<T extends Record<string, any>>(
10331033

10341034
/*
10351035
The following functions are copied from rollup
1036-
https://github.com/rollup/rollup/blob/c5269747cd3dd14c4b306e8cea36f248d9c1aa01/src/ast/nodes/MetaProperty.ts#L189-L232
1036+
https://github.com/rollup/rollup/blob/0bcf0a672ac087ff2eb88fbba45ec62389a4f45f/src/ast/nodes/MetaProperty.ts#L145-L193
10371037
10381038
https://github.com/rollup/rollup
10391039
The MIT License (MIT)
@@ -1042,15 +1042,30 @@ function injectSsrFlag<T extends Record<string, any>>(
10421042
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10431043
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10441044
*/
1045+
const needsEscapeRegEx = /[\n\r'\\\u2028\u2029]/
1046+
const quoteNewlineRegEx = /([\n\r'\u2028\u2029])/g
1047+
const backSlashRegEx = /\\/g
1048+
1049+
function escapeId(id: string): string {
1050+
if (!needsEscapeRegEx.test(id)) return id
1051+
return id.replace(backSlashRegEx, '\\\\').replace(quoteNewlineRegEx, '\\$1')
1052+
}
1053+
10451054
const getResolveUrl = (path: string, URL = 'URL') => `new ${URL}(${path}).href`
10461055

10471056
const getRelativeUrlFromDocument = (relativePath: string, umd = false) =>
10481057
getResolveUrl(
1049-
`'${relativePath}', ${
1058+
`'${escapeId(relativePath)}', ${
10501059
umd ? `typeof document === 'undefined' ? location.href : ` : ''
10511060
}document.currentScript && document.currentScript.src || document.baseURI`,
10521061
)
10531062

1063+
const getFileUrlFromFullPath = (path: string) =>
1064+
`require('u' + 'rl').pathToFileURL(${path}).href`
1065+
1066+
const getFileUrlFromRelativePath = (path: string) =>
1067+
getFileUrlFromFullPath(`__dirname + '/${path}'`)
1068+
10541069
const relativeUrlMechanisms: Record<
10551070
InternalModuleFormat,
10561071
(relativePath: string) => string
@@ -1060,18 +1075,16 @@ const relativeUrlMechanisms: Record<
10601075
return getResolveUrl(`require.toUrl('${relativePath}'), document.baseURI`)
10611076
},
10621077
cjs: (relativePath) =>
1063-
`(typeof document === 'undefined' ? ${getResolveUrl(
1064-
`'file:' + __dirname + '/${relativePath}'`,
1065-
`(require('u' + 'rl').URL)`,
1078+
`(typeof document === 'undefined' ? ${getFileUrlFromRelativePath(
1079+
relativePath,
10661080
)} : ${getRelativeUrlFromDocument(relativePath)})`,
10671081
es: (relativePath) => getResolveUrl(`'${relativePath}', import.meta.url`),
10681082
iife: (relativePath) => getRelativeUrlFromDocument(relativePath),
10691083
// NOTE: make sure rollup generate `module` params
10701084
system: (relativePath) => getResolveUrl(`'${relativePath}', module.meta.url`),
10711085
umd: (relativePath) =>
1072-
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getResolveUrl(
1073-
`'file:' + __dirname + '/${relativePath}'`,
1074-
`(require('u' + 'rl').URL)`,
1086+
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(
1087+
relativePath,
10751088
)} : ${getRelativeUrlFromDocument(relativePath, true)})`,
10761089
}
10771090
/* end of copy */

0 commit comments

Comments
 (0)