Skip to content

Commit

Permalink
refactor(runtime): use slash
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Feb 29, 2024
1 parent 291f902 commit 369c3f7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
5 changes: 2 additions & 3 deletions packages/vite/src/runtime/moduleCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isWindows, withTrailingSlash } from '../shared/utils'
import { isWindows, slash, withTrailingSlash } from '../shared/utils'
import { SOURCEMAPPING_URL } from '../shared/constants'
import { decodeBase64 } from './utils'
import { DecodedMap } from './sourcemap/decoder'
Expand Down Expand Up @@ -180,8 +180,7 @@ function normalizeModuleId(file: string, root: string): string {
if (prefixedBuiltins.has(file)) return file

// unix style, but Windows path still starts with the drive letter to check the root
let unixFile = file
.replace(/\\/g, '/')
let unixFile = slash(file)
.replace(/^\/@fs\//, isWindows ? '' : '/')
.replace(/^node:/, '')
.replace(/^\/+/, '/')
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
cleanUrl,
isPrimitive,
isWindows,
slash,
unwrapId,
wrapId,
} from '../shared/utils'
Expand Down Expand Up @@ -164,7 +165,7 @@ export class ViteRuntime {
// 8 is the length of "file:///"
url = url.slice(isWindows ? 8 : 7)
}
url = url.replace(/\\/g, '/')
url = slash(url)
const _root = this.options.root
const root = _root[_root.length - 1] === '/' ? _root : `${_root}/`
// strip root from the URL because fetchModule prefers a public served url path
Expand Down
12 changes: 5 additions & 7 deletions packages/vite/src/runtime/sourcemap/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { OriginalMapping } from '@jridgewell/trace-mapping'
import type { ViteRuntime } from '../runtime'
import { posixDirname, posixResolve } from '../utils'
import type { ModuleCacheMap } from '../moduleCache'
import { slash } from '../../shared/utils'
import { DecodedMap, getOriginalPosition } from './decoder'

interface RetrieveFileHandler {
Expand Down Expand Up @@ -88,24 +89,21 @@ interface CachedMapEntry {
// Support URLs relative to a directory, but be careful about a protocol prefix
function supportRelativeURL(file: string, url: string) {
if (!file) return url
const dir = posixDirname(file.replace(/\\/g, '/'))
const dir = posixDirname(slash(file))
const match = /^\w+:\/\/[^/]*/.exec(dir)
let protocol = match ? match[0] : ''
const startPath = dir.slice(protocol.length)
if (protocol && /^\/\w:/.test(startPath)) {
// handle file:///C:/ paths
protocol += '/'
return (
protocol +
posixResolve(dir.slice(protocol.length), url).replace(/\\/g, '/')
)
return protocol + slash(posixResolve(startPath, url))
}
return protocol + posixResolve(dir.slice(protocol.length), url)
return protocol + posixResolve(startPath, url)
}

function getRuntimeSourceMap(position: OriginalMapping): CachedMapEntry | null {
for (const moduleCache of moduleGraphs) {
const sourceMap = moduleCache.getSourceMap(position.source as string)
const sourceMap = moduleCache.getSourceMap(position.source!)
if (sourceMap) {
return {
url: position.source,
Expand Down

0 comments on commit 369c3f7

Please sign in to comment.