Skip to content

Commit

Permalink
fix: better approach for windows hack
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Mar 4, 2022
1 parent 13c0bb6 commit 3e54315
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
45 changes: 32 additions & 13 deletions packages/vite/src/node/utils.ts
Expand Up @@ -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: [],
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e54315

Please sign in to comment.