Skip to content

Commit

Permalink
fix: ensure source maps can be traced by debugger (#886)
Browse files Browse the repository at this point in the history
close #675

- Populate the `sourcesContent` array
- Resolve relative paths in the `sources` array

The `sourcesContent` of a source map can be null, which makes the devtools ask Vite for the original source. This can lead to Vite responding with transformed code, since Vite supports importing TypeScript directly. By populating the `sourcesContent` before sending the source map, the devtools won't need to ask Vite for the original source directly.

The `sources` array may contain relative paths, which are resolved to a /@modules/ path by the devtools. It would be better to know the actual path of the original source, so this PR resolves the relative path before sending the source map to the debugger.
  • Loading branch information
aleclarson authored Oct 23, 2020
1 parent 802aa7e commit ba7442f
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/node/utils/fsUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import fs from 'fs-extra'
import LRUCache from 'lru-cache'
import { RawSourceMap } from 'source-map'
import { Context } from '../server'
import { Readable } from 'stream'
import { seenUrls } from '../server/serverPluginServeStatic'
Expand Down Expand Up @@ -50,7 +51,27 @@ export async function cachedRead(
return cached.content
}
// #395 some file is an binary file, eg. font
const content = await fs.readFile(file)
let content = await fs.readFile(file)
// Populate the "sourcesContent" array and resolve relative paths in the
// "sources" array, so the debugger can trace back to the original source.
if (file.endsWith('.map')) {
const map: RawSourceMap = JSON.parse(content.toString('utf8'))
if (!map.sourcesContent || !map.sources.every(path.isAbsolute)) {
const sourcesContent = map.sourcesContent || []
map.sources = await Promise.all(
map.sources.map(async (source, i) => {
const originalPath = path.resolve(path.dirname(file), source)
if (!sourcesContent[i]) {
const originalCode = await cachedRead(null, originalPath)
sourcesContent[i] = originalCode.toString('utf8')
}
return originalPath
})
)
map.sourcesContent = sourcesContent
content = Buffer.from(JSON.stringify(map))
}
}
const etag = getETag(content)
fsReadCache.set(file, {
content,
Expand Down

0 comments on commit ba7442f

Please sign in to comment.