feat: add source map support

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-02-19 19:00:26 +08:00
parent 8d81d6938e
commit b506d06173

View File

@ -86,17 +86,7 @@ export class MiaoScriptConsole implements Console {
ex(ex: Error) { ex(ex: Error) {
this.stack(ex).forEach(line => this.console(line)) this.stack(ex).forEach(line => this.console(line))
} }
stack(ex: Error): string[] { readSourceMap(fileName: string, lineNumber: number) {
var stack = ex.getStackTrace();
var cache = ['§4' + ex];
//@ts-ignore
if (stack.class) {
stack = Arrays.asList(stack)
}
stack.forEach(trace => {
if (trace.className.startsWith('<')) {
var fileName = trace.fileName;
var lineNumber = trace.lineNumber;
try { try {
if (fileName.endsWith('js')) { if (fileName.endsWith('js')) {
var file = Paths.get(fileName + '.map'); var file = Paths.get(fileName + '.map');
@ -108,20 +98,38 @@ export class MiaoScriptConsole implements Console {
} }
if (this.sourceMaps[fileName]) { if (this.sourceMaps[fileName]) {
var sourceMapping = this.sourceMaps[fileName].getSource(lineNumber, lineNumber); var sourceMapping = this.sourceMaps[fileName].getSource(lineNumber, lineNumber);
if (sourceMapping) {
fileName = fileName.replace(".js", ".ts"); fileName = fileName.replace(".js", ".ts");
lineNumber = sourceMapping.mapping.sourceLine; lineNumber = sourceMapping.mapping.sourceLine;
} }
} }
} catch (error) {
console.debug('search source map error:', error)
} }
} catch (error) {
console.debug('search source map', fileName, 'line', lineNumber, 'error:', error)
}
return {
fileName,
lineNumber
}
}
stack(ex: Error): string[] {
var stack = ex.getStackTrace();
var cache = ['§4' + ex];
//@ts-ignore
if (stack.class) {
stack = Arrays.asList(stack)
}
stack.forEach(trace => {
if (trace.className.startsWith('<')) {
var { fileName, lineNumber } = this.readSourceMap(trace.fileName, trace.lineNumber)
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] } if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }
cache.push(` §e->§c ${fileName} => §4${trace.methodName}:${lineNumber}`) cache.push(` §e->§c ${fileName} => §4${trace.methodName}:${lineNumber}`)
} else { } else {
var className = trace.className; var className = trace.className;
var fileName = trace.fileName; var fileName = trace.fileName as string;
if (className.startsWith('jdk.nashorn.internal.scripts')) { if (className.startsWith('jdk.nashorn.internal.scripts')) {
className = className.substr(className.lastIndexOf('$') + 1) className = className.substr(className.lastIndexOf('$') + 1)
var { fileName, lineNumber } = this.readSourceMap(trace.fileName, trace.lineNumber)
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] } if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }
} else { } else {
for (var prefix in ignoreLogPrefix) { for (var prefix in ignoreLogPrefix) {