From 9eb83de99cd4429720fb9089a68fb468f7313dfc Mon Sep 17 00:00:00 2001 From: SegaraRai Date: Thu, 25 Jul 2024 00:15:45 +0900 Subject: [PATCH] fix: improve virtual module id for non-slash delimiters --- src/frameworks/vite/id.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/frameworks/vite/id.ts b/src/frameworks/vite/id.ts index 9c8855c..16375d3 100644 --- a/src/frameworks/vite/id.ts +++ b/src/frameworks/vite/id.ts @@ -21,11 +21,11 @@ export const DEFAULT_VITE_ID_OPTIONS = { function stringifySourceId(id: string, root: string): string { const normalizedId = normalizePath(id); - if (normalizedId === root) { - return RELATIVE_PREFIX.slice(0, -1); + if (normalizedId === root || normalizedId === root + "/") { + return RELATIVE_PREFIX; } - const shortened = normalizePath(id).startsWith(root + "/") + const shortened = normalizedId.startsWith(root + "/") ? (RELATIVE_PREFIX + id.slice(root.length + 1)).replace(/\/$/, "") : id; return shortened.replaceAll("\0", "__x00__"); @@ -74,7 +74,11 @@ export function stringifyId( return `${PREFIX}${filenamePrefix}${name}`; } - return `${PREFIX}${stringifySourceId(sourceId, root)}${delimiter}${filenamePrefix}${name}`; + const stringifiedSourceId = stringifySourceId(sourceId, root); + const normalizedDelimiter = stringifiedSourceId.endsWith("/") + ? delimiter.replace(/^\//, "") + : delimiter; + return `${PREFIX}${stringifiedSourceId}${normalizedDelimiter}${filenamePrefix}${name}`; } export function createIdFunctions(