Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

module-specifier.ts

41 lines1.3 KB

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 }
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)