-
-
Save isaacs/ba57fca9ec16152256cba4b9b544750c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
Object.defineProperty(exports, "__esModule", { value: true }); | |
const mkdirp_1 = require("mkdirp"); | |
const node_fs_1 = require("node:fs"); | |
mkdirp_1.mkdirp; | |
const node_module_1 = require("node:module"); | |
const node_path_1 = require("node:path"); | |
const node_url_1 = require("node:url"); | |
const rel = (p) => (0, node_path_1.relative)(process.cwd(), p); | |
const getLines = (f) => (0, node_fs_1.readFileSync)(f, 'utf8').split('\n'); | |
const highlight = (lines, l, c) => { | |
const line = lines[l - 1]; | |
const before = line.slice(0, c - 1); | |
const after = line.slice(c); | |
return l + ':' + c + ' | ' + before + ' >' + line.charAt(c - 1) + '< ' + after; | |
}; | |
const fn = () => { | |
bar(); | |
}; | |
const bar = () => { | |
baz(); | |
}; | |
const baz = () => { | |
const e = new Error('trace'); | |
Error.prepareStackTrace = (e, c) => { | |
return c.map((c) => { | |
const res = {}; | |
const fileName = res.fileName = c.getFileName(); | |
const lineNumber = res.lineNumber = c.getLineNumber(); | |
const columnNumber = res.columnNumber = c.getColumnNumber(); | |
if (fileName && lineNumber !== null && columnNumber !== null) { | |
const sm = (0, node_module_1.findSourceMap)(fileName, e); | |
if (sm) { | |
const payload = sm.findEntry(lineNumber, columnNumber); | |
if (payload) { | |
Object.assign(res, payload); | |
res.originalSource = (0, node_url_1.fileURLToPath)(res.originalSource); | |
const lines = getLines(res.fileName); | |
res.fromError = highlight(lines, res.lineNumber, res.columnNumber); | |
res.generated = highlight(lines, res.generatedLine, res.generatedColumn); | |
const oglines = getLines(res.originalSource); | |
res.origin = highlight(oglines, res.originalLine, res.originalColumn); | |
res.originalSource = rel(res.originalSource); | |
res.fileName = rel(res.fileName); | |
} | |
} | |
} | |
return res; | |
}); | |
}; | |
Error.captureStackTrace(e); | |
//@ts-ignore | |
console.log(e.stack.filter(c => c.originalSource)); | |
}; | |
fn(); | |
//# sourceMappingURL=node-source-map-example.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"version":3,"file":"node-source-map-example.js","sourceRoot":"","sources":["node-source-map-example.ts"],"names":[],"mappings":";;AAAA,mCAA+B;AAC/B,qCAAoC;AACpC,eAAM,CAAA;AACN,6CAA2C;AAC3C,yCAAkC;AAClC,uCAAsC;AAEtC,MAAM,GAAG,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,IAAA,oBAAQ,EAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAA;AAErD,MAAM,QAAQ,GAAG,CAAC,CAAS,EAAE,EAAE,CAC7B,IAAA,sBAAY,EAAC,CAAC,EAAE,MAAM,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;AAErC,MAAM,SAAS,GAAG,CAAC,KAAe,EAAE,CAAS,EAAE,CAAS,EAAE,EAAE;IAC1D,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;IACzB,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;IAC3B,OAAO,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,KAAK,GAAG,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,GAAG,KAAK,CAAA;AAChF,CAAC,CAAA;AAUD,MAAM,EAAE,GAAG,GAAG,EAAE;IACd,GAAG,EAAE,CAAA;AACP,CAAC,CAAA;AAED,MAAM,GAAG,GAAG,GAAG,EAAE;IACf,GAAG,EAAE,CAAA;AACP,CAAC,CAAA;AAED,MAAM,GAAG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CAAC,GAAG,CAAC,cAAc,CAAC,CAAA;wBAC5C,GAAG,CAAC,MAAM,GAAG,SAAS,CAAC,OAAO,EAAE,GAAG,CAAC,YAAY,EAAE,GAAG,CAAC,cAAc,CAAC,CAAA;wBACrE,GAAG,CAAC,cAAc,GAAG,GAAG,CAAC,GAAG,CAAC,cAAc,CAAC,CAAA;wBAC5C,GAAG,CAAC,QAAQ,GAAG,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;qBACjC;iBACF;aACF;YACD,OAAO,GAAG,CAAA;QACZ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;IACD,KAAK,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAA;IAC1B,YAAY;IACZ,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAA;AACpD,CAAC,CAAA;AAED,EAAE,EAAE,CAAA","sourcesContent":["import { mkdirp } from 'mkdirp'\nimport {readFileSync} from 'node:fs'\nmkdirp\nimport { findSourceMap } from 'node:module'\nimport {relative} from 'node:path'\nimport {fileURLToPath} from 'node:url'\n\nconst rel = (p: string) => relative(process.cwd(), p)\n\nconst getLines = (f: string) =>\n readFileSync(f, 'utf8').split('\\n')\n\nconst highlight = (lines: string[], l: number, c: number) => {\n const line = lines[l - 1]\n const before = line.slice(0, c - 1)\n const after = line.slice(c)\n return l + ':' + c + ' | ' + before + ' >' + line.charAt(c - 1) + '< ' + after\n}\n\n// just some stuff to be filtered out so line numbers are off more\nexport interface Something {\n foo: {\n bar: true\n baz: number\n }\n}\n\nconst fn = () => {\n bar()\n}\n\nconst bar = () => {\n baz()\n}\n\nconst baz = () => {\n const e = new Error('trace')\n Error.prepareStackTrace = (e: Error, c: NodeJS.CallSite[]) => {\n return c.map((c):Record<string, any> => {\n const res: Record<string, any> = {}\n const fileName = res.fileName = c.getFileName()\n const lineNumber = res.lineNumber = c.getLineNumber()\n const columnNumber = res.columnNumber = c.getColumnNumber()\n if (fileName && lineNumber !== null && columnNumber !== null) {\n const sm = findSourceMap(fileName, e)\n if (sm) {\n const payload = sm.findEntry(lineNumber, columnNumber)\n if (payload) {\n Object.assign(res, payload)\n res.originalSource = fileURLToPath(res.originalSource)\n const lines = getLines(res.fileName)\n res.fromError = highlight(lines, res.lineNumber, res.columnNumber)\n res.generated = highlight(lines, res.generatedLine, res.generatedColumn)\n const oglines = getLines(res.originalSource)\n res.origin = highlight(oglines, res.originalLine, res.originalColumn)\n res.originalSource = rel(res.originalSource)\n res.fileName = rel(res.fileName)\n }\n }\n }\n return res\n })\n }\n Error.captureStackTrace(e)\n //@ts-ignore\n console.log(e.stack.filter(c => c.originalSource))\n}\n\nfn()\n"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { mkdirp } from 'mkdirp' | |
import {readFileSync} from 'node:fs' | |
mkdirp | |
import { findSourceMap } from 'node:module' | |
import {relative} from 'node:path' | |
import {fileURLToPath} from 'node:url' | |
const rel = (p: string) => relative(process.cwd(), p) | |
const getLines = (f: string) => | |
readFileSync(f, 'utf8').split('\n') | |
const highlight = (lines: string[], l: number, c: number) => { | |
const line = lines[l - 1] | |
const before = line.slice(0, c - 1) | |
const after = line.slice(c) | |
return l + ':' + c + ' | ' + before + ' >' + line.charAt(c - 1) + '< ' + after | |
} | |
// just some stuff to be filtered out so line numbers are off more | |
export interface Something { | |
foo: { | |
bar: true | |
baz: number | |
} | |
} | |
const fn = () => { | |
bar() | |
} | |
const bar = () => { | |
baz() | |
} | |
const baz = () => { | |
const e = new Error('trace') | |
Error.prepareStackTrace = (e: Error, c: NodeJS.CallSite[]) => { | |
return c.map((c):Record<string, any> => { | |
const res: Record<string, any> = {} | |
const fileName = res.fileName = c.getFileName() | |
const lineNumber = res.lineNumber = c.getLineNumber() | |
const columnNumber = res.columnNumber = c.getColumnNumber() | |
if (fileName && lineNumber !== null && columnNumber !== null) { | |
const sm = findSourceMap(fileName, e) | |
if (sm) { | |
const payload = sm.findEntry(lineNumber, columnNumber) | |
if (payload) { | |
Object.assign(res, payload) | |
res.originalSource = fileURLToPath(res.originalSource) | |
const lines = getLines(res.fileName) | |
res.fromError = highlight(lines, res.lineNumber, res.columnNumber) | |
res.generated = highlight(lines, res.generatedLine, res.generatedColumn) | |
const oglines = getLines(res.originalSource) | |
res.origin = highlight(oglines, res.originalLine, res.originalColumn) | |
res.originalSource = rel(res.originalSource) | |
res.fileName = rel(res.fileName) | |
} | |
} | |
} | |
return res | |
}) | |
} | |
Error.captureStackTrace(e) | |
//@ts-ignore | |
console.log(e.stack.filter(c => c.originalSource)) | |
} | |
fn() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: