diff --git a/packages/vite/src/runtime/moduleCache.ts b/packages/vite/src/runtime/moduleCache.ts index 0b9aecd858bb19..3681e3db1ed78d 100644 --- a/packages/vite/src/runtime/moduleCache.ts +++ b/packages/vite/src/runtime/moduleCache.ts @@ -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' @@ -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(/^\/+/, '/') diff --git a/packages/vite/src/runtime/runtime.ts b/packages/vite/src/runtime/runtime.ts index 45132bf536b8b5..5feff38352617f 100644 --- a/packages/vite/src/runtime/runtime.ts +++ b/packages/vite/src/runtime/runtime.ts @@ -4,6 +4,7 @@ import { cleanUrl, isPrimitive, isWindows, + slash, unwrapId, wrapId, } from '../shared/utils' @@ -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 diff --git a/packages/vite/src/runtime/sourcemap/interceptor.ts b/packages/vite/src/runtime/sourcemap/interceptor.ts index 5175c2d40a9a53..58d324e79b943c 100644 --- a/packages/vite/src/runtime/sourcemap/interceptor.ts +++ b/packages/vite/src/runtime/sourcemap/interceptor.ts @@ -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 { @@ -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,