MCPcopy
hub / github.com/vitejs/vite / posixPathToFileHref

Function posixPathToFileHref

packages/vite/src/module-runner/utils.ts:41–62  ·  view source on GitHub ↗
(posixPath: string)

Source from the content-addressed store, hash-verified

39export const posixResolve: (...paths: string[]) => string = pathe.resolve
40
41export function posixPathToFileHref(posixPath: string): string {
42 let resolved = posixResolve(posixPath)
43 // path.resolve strips trailing slashes so we must add them back
44 const filePathLast = posixPath.charCodeAt(posixPath.length - 1)
45 if (
46 (filePathLast === CHAR_FORWARD_SLASH ||
47 (isWindows && filePathLast === CHAR_BACKWARD_SLASH)) &&
48 resolved[resolved.length - 1] !== '/'
49 )
50 resolved += '/'
51
52 // Call encodePathChars first to avoid encoding % again for ? and #.
53 resolved = encodePathChars(resolved)
54
55 // Question and hash character should be included in pathname.
56 // Therefore, encoding is required to eliminate parsing them in different states.
57 // This is done as an optimization to not creating a URL instance and
58 // later triggering pathname setter, which impacts performance
59 if (resolved.includes('?')) resolved = resolved.replace(questionRegex, '%3F')
60 if (resolved.includes('#')) resolved = resolved.replace(hashRegex, '%23')
61 return new URL(`file://${resolved}`).href
62}
63
64export function toWindowsPath(path: string): string {
65 return path.replace(/\//g, '\\')

Callers 2

createDefaultImportMetaFunction · 0.90
directRequestMethod · 0.90

Calls 1

encodePathCharsFunction · 0.85

Tested by

no test coverage detected