diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 2fb2d9aeed1589..6f5b785002b406 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -524,6 +524,26 @@ export async function processSrcSet( }, '') } +function escapeToLinuxLikePath(path: string) { + if (/^[A-Z]:/.test(path)) { + return path.replace(/^([A-Z]):\//, '/windows/$1/') + } + if (/^\/[^/]/.test(path)) { + return `/linux/${path}` + } + return path +} + +function unescapeToLinuxLikePath(path: string) { + if (path.startsWith('/linux/')) { + return path.slice('/linux'.length) + } + if (path.startsWith('/windows/')) { + return path.replace(/^\/windows\/([A-Z])\//, '$1:/') + } + return path +} + // based on https://github.com/sveltejs/svelte/blob/abf11bb02b2afbd3e4cac509a0f70e318c306364/src/compiler/utils/mapped_code.ts#L221 const nullSourceMap: RawSourceMap = { names: [], @@ -542,19 +562,17 @@ export function combineSourcemaps( return { ...nullSourceMap } } - // FIXME: hack for parse broken with normalized absolute paths on windows (C:/path/to/something) - const base = normalizePath(path.dirname(filename)) + // hack for parse broken with normalized absolute paths on windows (C:/path/to/something). + // escape them to linux like paths sourcemapList.forEach((sourcemap) => { - sourcemap.sources = sourcemap.sources.map((source) => { - if (!source) return null - if (sourcemap.sourceRoot) { - source = path.resolve(sourcemap.sourceRoot, source) - } - return normalizePath(path.relative(base, source)) - }) - sourcemap.sourceRoot = undefined + sourcemap.sources = sourcemap.sources.map((source) => + source ? escapeToLinuxLikePath(source) : null + ) + if (sourcemap.sourceRoot) { + sourcemap.sourceRoot = escapeToLinuxLikePath(sourcemap.sourceRoot) + } }) - const baseFilename = path.basename(filename) + const escapedFilename = escapeToLinuxLikePath(filename) // We don't declare type here so we can convert/fake/map as RawSourceMap let map //: SourceMap @@ -567,7 +585,7 @@ export function combineSourcemaps( map = remapping( sourcemapList[0], function loader(sourcefile) { - if (sourcefile === baseFilename && sourcemapList[mapIndex]) { + if (sourcefile === escapedFilename && sourcemapList[mapIndex]) { return sourcemapList[mapIndex++] } else { return null @@ -580,8 +598,9 @@ export function combineSourcemaps( delete map.file } + // unescape the previous hack map.sources = map.sources.map((source) => - source ? normalizePath(path.resolve(base, source)) : null + source ? unescapeToLinuxLikePath(source) : source ) map.file = filename diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ab2b4a7833c686..1801bb5bb2a141 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1976,9 +1976,9 @@ packages: chalk: 4.1.2 dev: true - /@jridgewell/resolve-uri/3.0.3: - resolution: {integrity: sha512-fuIOnc81C5iRNevb/XPiM8Khp9bVjreydRQ37rt0C/dY0PAW1DRvEM3WrKX/5rStS5lbgwS0FCgqSndh9tvK5w==} - engines: {node: '>=10.0.0'} + /@jridgewell/resolve-uri/3.0.5: + resolution: {integrity: sha512-VPeQ7+wH0itvQxnG+lIzWgkysKIr3L9sslimFW55rHMdGu/qCQ5z5h9zq4gI8uBtqkpHhsF4Z/OwExufUCThew==} + engines: {node: '>=6.0.0'} /@jridgewell/sourcemap-codec/1.4.10: resolution: {integrity: sha512-Ht8wIW5v165atIX1p+JvKR5ONzUyF4Ac8DZIQ5kZs9zrb6M8SJNXpx1zn04rn65VjBMygRoMXcyYwNK0fT7bEg==} @@ -1986,7 +1986,7 @@ packages: /@jridgewell/trace-mapping/0.3.2: resolution: {integrity: sha512-9KzzH4kMjA2XmBRHfqG2/Vtl7s92l6uNDd0wW7frDE+EUvQFGqNXhWp0UGJjSkt3v2AYjzOZn1QO9XaTNJIt1Q==} dependencies: - '@jridgewell/resolve-uri': 3.0.3 + '@jridgewell/resolve-uri': 3.0.5 '@jridgewell/sourcemap-codec': 1.4.10 /@mapbox/node-pre-gyp/1.0.8: @@ -3103,7 +3103,7 @@ packages: resolution: {integrity: sha512-oagLNqpfNv7CvmyMoexMDNyVDSiq1rya0AEUgcLlNHdHgNl6U/hi8xY370n5y+ZIFEXOx0J4B1qF2NDjMRxklA==} engines: {node: '>=6.0.0'} dependencies: - pvutils: 1.0.17 + pvutils: 1.1.3 dev: true /assert-never/1.2.1: @@ -7690,8 +7690,8 @@ packages: tslib: 2.3.1 dev: true - /pvutils/1.0.17: - resolution: {integrity: sha512-wLHYUQxWaXVQvKnwIDWFVKDJku9XDCvyhhxoq8dc5MFdIlRenyPI9eSfEtcvgHgD7FlvCyGAlWgOzRnZD99GZQ==} + /pvutils/1.1.3: + resolution: {integrity: sha512-pMpnA0qRdFp32b1sJl1wOJNxZLQ2cbQx+k6tjNtZ8CpvVhNqEPRgivZ2WOUev2YMajecdH7ctUPDvEe87nariQ==} engines: {node: '>=6.0.0'} dev: true