Open History to see the last commit
1import type { DevServerFnModuleSpecifierEncoder } from '../../start-compiler/types'
2β
3export function createViteDevServerFnModuleSpecifierEncoder(
4 root: string,
5): DevServerFnModuleSpecifierEncoder {
6 const normalizedRoot = root.replace(/\\/g, '/')
7 const rootWithTrailingSlash = normalizedRoot.endsWith('/')
8 ? normalizedRoot
9 : `${normalizedRoot}/`
10β
11 return ({ extractedFilename }) => {
12 const normalizedFile = extractedFilename.replace(/\\/g, '/')
13β
14 if (normalizedFile.startsWith(rootWithTrailingSlash)) {
15 return `/${normalizedFile.slice(rootWithTrailingSlash.length)}`
16 }
17β
18 return normalizedFile.startsWith('/')
19 ? `/@fs${normalizedFile}`
20 : `/@fs/${normalizedFile}`
21 }
22}
23β
24export function decodeViteDevServerModuleSpecifier(
25 moduleSpecifier: string,
26): string {
27 let sourceFile = moduleSpecifier
28β
29 if (sourceFile.startsWith('/@id/')) {
30 sourceFile = sourceFile.slice('/@id/'.length)
31 } else if (sourceFile.startsWith('/@fs/')) {
32 sourceFile = sourceFile.slice('/@fs'.length)
33 sourceFile = sourceFile.replace(/^\/([A-Za-z]:\/)/, '$1')
34 } else if (sourceFile.startsWith('/')) {
35 sourceFile = sourceFile.slice(1)
36 }
37β
38 const queryIndex = sourceFile.indexOf('?')
39β
40 return queryIndex === -1 ? sourceFile : sourceFile.slice(0, queryIndex)
41}