Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
Something went wrong. Try again.
1.1 kB · 40 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041import { existsSync, readFileSync } from 'node:fs'import path from 'node:path'import convertSourceMap from 'convert-source-map'
interface ExtractedSourceMap { map: any}
// based on vite// https://github.com/vitejs/vite/blob/84079a84ad94de4c1ef4f1bdb2ab448ff2c01196/packages/vite/src/node/server/sourcemap.ts#L149export function extractSourcemapFromFile( code: string, filePath: string,): ExtractedSourceMap | undefined { try { const map = ( convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource( code, createConvertSourceMapReadMap(filePath), ) )?.toObject() return map ? { map } : undefined } catch { return undefined }}
function createConvertSourceMapReadMap(originalFileName: string) { return (filename: string) => { // convertSourceMap can detect invalid filename from comments. // fallback to empty source map to avoid errors. const targetPath = path.resolve(path.dirname(originalFileName), filename) if (existsSync(targetPath)) { return readFileSync(targetPath, 'utf-8') } return '{}' }}