diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index c85665f1aadffa..24323a6a0b7355 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -18,7 +18,6 @@ import { createFilter, ensureWatchedFile, generateCodeFrame, - toUpperCaseDriveLetter, } from '../utils' import type { ResolvedConfig, ViteDevServer } from '..' import type { Plugin } from '../plugin' @@ -192,9 +191,6 @@ export async function transformWithEsbuild( ? JSON.parse(result.map) : { mappings: '' } } - if (Array.isArray(map.sources)) { - map.sources = map.sources.map((it) => toUpperCaseDriveLetter(it)) - } return { ...result, map, diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index a75d78c0b6af5e..0caf823a74dd17 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -266,11 +266,25 @@ async function loadAndTransform( map = transformResult.map } - if (map && mod.file) { + if (map) { map = (typeof map === 'string' ? JSON.parse(map) : map) as SourceMap - if (map.mappings && !map.sourcesContent) { + if (mod.file && map.mappings && !map.sourcesContent) { await injectSourcesContent(map, mod.file, logger) } + // Rewrite sources to relative paths to give debuggers the chance + // to resolve and display them in a meaningful way (rather than + // with absolute paths). + if (Array.isArray(map.sources)) { + const file = map.file ?? mod.file + if (file && path.isAbsolute(file)) { + const sourceFolder = path.dirname(file) + map.sources = map.sources.map((source) => + path.isAbsolute(source) + ? path.relative(sourceFolder, source) + : source, + ) + } + } } const result = diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 13ef1c8c91326e..738e323f8c8739 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -909,10 +909,6 @@ export function arraify(target: T | T[]): T[] { return Array.isArray(target) ? target : [target] } -export function toUpperCaseDriveLetter(pathName: string): string { - return pathName.replace(/^\w:/, (letter) => letter.toUpperCase()) -} - // Taken from https://stackoverflow.com/a/36328890 export const multilineCommentsRE = /\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\//g export const singlelineCommentsRE = /\/\/.*/g diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 5a6d37dd421ca5..692de4c8ed2c6e 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -24,7 +24,7 @@ if (!isBuild) { { "mappings": "AAAO,aAAM,MAAM;", "sources": [ - "/root/bar.ts", + "bar.ts", ], "sourcesContent": [ "export const bar = 'bar'