Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inject sourcesContent into sourcemaps if missing #2283

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/vite/src/node/server/sourcemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { promises as fs } from 'fs'
import path from 'path'

export async function injectSourcesContent(
map: { sources: string[]; sourcesContent?: string[]; sourceRoot?: string },
file: string
) {
const sourceRoot = await fs.realpath(
path.resolve(path.dirname(file), map.sourceRoot || '')
)
map.sourcesContent = []
await Promise.all(
map.sources.map(async (sourcePath, i) => {
map.sourcesContent![i] = await fs.readFile(
path.resolve(sourceRoot, sourcePath),
'utf-8'
)
})
)
}
8 changes: 8 additions & 0 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '../utils'
import { checkPublicFile } from '../plugins/asset'
import { ssrTransform } from '../ssr/ssrTransform'
import { injectSourcesContent } from './sourcemap'

const debugLoad = createDebugger('vite:load')
const debugTransform = createDebugger('vite:transform')
Expand Down Expand Up @@ -135,6 +136,13 @@ export async function transformRequest(
map = transformResult.map
}

if (map && mod.file) {
map = (typeof map === 'string' ? JSON.parse(map) : map) as SourceMap
if (map.mappings && !map.sourcesContent) {
await injectSourcesContent(map, mod.file)
}
}

if (ssr) {
return (mod.ssrTransformResult = await ssrTransform(code, map as SourceMap))
} else {
Expand Down