Skip to content

Commit

Permalink
fix: use relative paths in sources for esbuild
Browse files Browse the repository at this point in the history
This refines the fix from #4985 to
turn absolute paths into relative paths for the `sources` array in
source maps generated by esbuild (and therefore completely avoids the
Windows drive letter problem). In order to minimize unintended negative
side effects, we perform this step only when the source file name is
absolute.

This addresses the issue that source files show up with an absolute path
prefix in case of Vue[^1]. It doesn't change anything when using
SvelteKit (where the problem did not exist in the first place). When
using SolidStart, the problem still persists, likely because it doesn't
use the `transformWithEsbuild` function, and therefore needs a follow up
fix.

Bug: https://crbug.com/1411596
Ref: #4964
Ref: #4912

[^1]: https://goo.gle/devtools-vite-interoperability
  • Loading branch information
bmeurer committed Feb 16, 2023
1 parent 081c27f commit b81a561
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
11 changes: 8 additions & 3 deletions packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -18,7 +18,6 @@ import {
createFilter,
ensureWatchedFile,
generateCodeFrame,
toUpperCaseDriveLetter,
} from '../utils'
import type { ResolvedConfig, ViteDevServer } from '..'
import type { Plugin } from '../plugin'
Expand Down Expand Up @@ -192,8 +191,14 @@ export async function transformWithEsbuild(
? JSON.parse(result.map)
: { mappings: '' }
}
if (Array.isArray(map.sources)) {
map.sources = map.sources.map((it) => toUpperCaseDriveLetter(it))
if (Array.isArray(map.sources) && path.isAbsolute(filename)) {
const sourceFolder = path.dirname(filename)
map.sources = map.sources.map((source) => {
if (path.isAbsolute(source)) {
source = path.relative(sourceFolder, source)
}
return source
})
}
return {
...result,
Expand Down
4 changes: 0 additions & 4 deletions packages/vite/src/node/utils.ts
Expand Up @@ -909,10 +909,6 @@ export function arraify<T>(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
Expand Down

0 comments on commit b81a561

Please sign in to comment.