/
sourcemap.ts
54 lines (49 loc) · 1.43 KB
/
sourcemap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import path from 'path'
import { promises as fs } from 'fs'
import { Logger } from '../logger'
import { createDebugger } from '../utils'
const isDebug = !!process.env.DEBUG
const debug = createDebugger('vite:sourcemap', {
onlyWhenFocused: true
})
interface SourceMapLike {
sources: string[]
sourcesContent?: (string | null)[]
sourceRoot?: string
}
export async function injectSourcesContent(
map: SourceMapLike,
file: string,
logger: Logger
): Promise<void> {
let sourceRoot: string | undefined
try {
// The source root is undefined for virtual modules and permission errors.
sourceRoot = await fs.realpath(
path.resolve(path.dirname(file), map.sourceRoot || '')
)
} catch {}
const missingSources: string[] = []
map.sourcesContent = await Promise.all(
map.sources.map((sourcePath) => {
if (sourcePath) {
sourcePath = decodeURI(sourcePath)
if (sourceRoot) {
sourcePath = path.resolve(sourceRoot, sourcePath)
}
return fs.readFile(sourcePath, 'utf-8').catch(() => {
missingSources.push(sourcePath)
return null
})
}
return null
})
)
// Use this command…
// DEBUG="vite:sourcemap" vite build
// …to log the missing sources.
if (missingSources.length) {
logger.warnOnce(`Sourcemap for "${file}" points to missing source files`)
isDebug && debug(`Missing sources:\n ` + missingSources.join(`\n `))
}
}