From 81e44dda57e815c71ea3b2dcdd3dfbd05ef35e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sun, 2 Apr 2023 17:56:54 +0900 Subject: [PATCH] fix: take in relative assets path fixes from rollup (#12695) --- packages/vite/src/node/build.ts | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 912129b6f70fb9..8216b1a3294971 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -1033,7 +1033,7 @@ function injectSsrFlag>( /* The following functions are copied from rollup - https://github.com/rollup/rollup/blob/c5269747cd3dd14c4b306e8cea36f248d9c1aa01/src/ast/nodes/MetaProperty.ts#L189-L232 + https://github.com/rollup/rollup/blob/0bcf0a672ac087ff2eb88fbba45ec62389a4f45f/src/ast/nodes/MetaProperty.ts#L145-L193 https://github.com/rollup/rollup The MIT License (MIT) @@ -1042,15 +1042,30 @@ function injectSsrFlag>( The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. */ +const needsEscapeRegEx = /[\n\r'\\\u2028\u2029]/ +const quoteNewlineRegEx = /([\n\r'\u2028\u2029])/g +const backSlashRegEx = /\\/g + +function escapeId(id: string): string { + if (!needsEscapeRegEx.test(id)) return id + return id.replace(backSlashRegEx, '\\\\').replace(quoteNewlineRegEx, '\\$1') +} + const getResolveUrl = (path: string, URL = 'URL') => `new ${URL}(${path}).href` const getRelativeUrlFromDocument = (relativePath: string, umd = false) => getResolveUrl( - `'${relativePath}', ${ + `'${escapeId(relativePath)}', ${ umd ? `typeof document === 'undefined' ? location.href : ` : '' }document.currentScript && document.currentScript.src || document.baseURI`, ) +const getFileUrlFromFullPath = (path: string) => + `require('u' + 'rl').pathToFileURL(${path}).href` + +const getFileUrlFromRelativePath = (path: string) => + getFileUrlFromFullPath(`__dirname + '/${path}'`) + const relativeUrlMechanisms: Record< InternalModuleFormat, (relativePath: string) => string @@ -1060,18 +1075,16 @@ const relativeUrlMechanisms: Record< return getResolveUrl(`require.toUrl('${relativePath}'), document.baseURI`) }, cjs: (relativePath) => - `(typeof document === 'undefined' ? ${getResolveUrl( - `'file:' + __dirname + '/${relativePath}'`, - `(require('u' + 'rl').URL)`, + `(typeof document === 'undefined' ? ${getFileUrlFromRelativePath( + relativePath, )} : ${getRelativeUrlFromDocument(relativePath)})`, es: (relativePath) => getResolveUrl(`'${relativePath}', import.meta.url`), iife: (relativePath) => getRelativeUrlFromDocument(relativePath), // NOTE: make sure rollup generate `module` params system: (relativePath) => getResolveUrl(`'${relativePath}', module.meta.url`), umd: (relativePath) => - `(typeof document === 'undefined' && typeof location === 'undefined' ? ${getResolveUrl( - `'file:' + __dirname + '/${relativePath}'`, - `(require('u' + 'rl').URL)`, + `(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath( + relativePath, )} : ${getRelativeUrlFromDocument(relativePath, true)})`, } /* end of copy */