1

我想为异常堆栈跟踪实现我自己的格式,我知道Error.prepareStackTrace这就像一个魅力。

当我通过运行程序使用咖啡脚本时出现问题coffee,而不保存中间 *.js 结果及其源映射文件。我想从 .coffee 文件中显示正确的行号,而不是来自已编译的 .js

是否可以通过 require 访问在运行时动态加载的文件的咖啡脚本的内部运行时源映射(或获取相同的值)?sourceMaps = {}我知道里面有coffee-script.coffee咖啡脚本编译器本身使用的局部变量,但是在模块闭包之外无法访问它。

任何想法?

4

2 回答 2

0

是的,您可以通过 require "source-map" 使用 sourceMap。

然后您应该在 stackTrace 中获取文件路径、行和列信息以进行转换。

Kevin 编写了 coffeestack 库。

npm install coffeestack

用法:

{convertStackTrace} = require 'coffeestack'

try
  throw new Error('this is an error')
catch error
  console.error(convertStackTrace(error.stack))

更多细节参见CoffeeStack包。

于 2014-12-23T00:42:50.283 回答
0

这是我运行时得到的堆栈跟踪,simplenested.coffee其中包含一个require 'simpleerror.coffee'. 至少在 中1.6.3,两个文件的 sourceMaps 都被使用了。中间是loadFile. 如果任一文件是预先编译的,相关的框架行将引用 Javascript 代码。

2011:~/myjs$ coffee simplenested.coffee 
ReferenceError: x is not defined
  at foo (/.../simpleerror.coffee:4:7)
  at Object.<anonymous> (/.../simpleerror.coffee:7:1)
  at Object.<anonymous> (/.../simpleerror.coffee:2:1)
  at Module._compile (module.js:456:26)
  at Object.loadFile (/.../coffee-script/lib/coffee-script/coffee-script.js:182:19)
  at Module.load (/.../coffee-script/lib/coffee-script/coffee-script.js:211:36)
  at Function.Module._load (module.js:312:12)
  at Module.require (module.js:364:17)
  at require (module.js:380:17)
  at Object.<anonymous> (/.../simplenested.coffee:2:8)
  at Object.<anonymous> (/.../simplenested.coffee:1:1)
  at Module._compile (module.js:456:26)

simplenest.coffee

require 'coffee-script'
nested=require './simpleerror'

simpleerror.coffee

foo = () ->
  y = 'testing'
  y = x # expect ReferenceError
  return y

foo()
于 2014-01-11T20:11:46.073 回答