浏览器错误消息使用outFile引用正确的typescript文件

时间:2015-12-04 13:43:19

标签: debugging browser console source-maps typescript1.6

我使用outFile中的tsconfig.json选项将我的打字稿文件编译成一个.js文件。
我正在生成源地图 错误消息是引用了typescript文件。

示例:

script.ts:throw 'Error'
tsconfig.json:

{
  "compilerOptions": {
    "sourceMap": true
  }
}

编译(tsc 1.6.2)将生成:
script.js:throw 'Error
script.js.map:{"version":3,"file":"script.js","sourceRoot":"","sources":["script.ts"],"names":[],"mappings":"AACA,MAAM,QAAQ,CAAC"}

浏览器的控制台(Chrome 47.0.2526.73 m)将显示:Uncaught Error. script.ts:1

那很好。

script.ts:throw 'Error'
tsconfig.json:

{
  "compilerOptions": {
    "sourceMap": true,
    "outFile": "app.js"
  }
}

编译(tsc 1.6.2)将生成:
app.js:throw 'Error
app.js.map:{"version":3,"file":"app.js","sourceRoot":"","sources":["script.ts"],"names":[],"mappings":"AACA,MAAM,QAAQ,CAAC"}

浏览器的控制台(Chrome 47.0.2526.73 m)将显示:Uncaught Error. app.js:1

app.js 应该 app.ts

如何让浏览器将错误消息引用到我的打字稿文件?

1 个答案:

答案 0 :(得分:1)

我使用的是相同版本的Chrome浏览器。但是ts编译器版本是1.7.3

开启Dev工具控制台。

1例:

  • ts.config

    { "compilerOptions": { "sourceMap": true } }

  • app.ts

    throw 'some error in my typescript';

  • 开发工具控制台:

    app.ts:5Uncaught some error in my typescript(anonymous function) @ app.ts:5

2个案例:

  • ts.config

    { "compilerOptions": { "sourceMap": true, "outFile": "src.js" } }

  • app.ts

    throw 'some error in my typescript';

  • 开发工具控制台:

    app.ts:5Uncaught some error in my typescript(anonymous function) @ app.ts:5

如果关闭开发工具控制台,页面会刷新,然后您将打开开发工具控制台,然后您可以看到以下内容:

src.js:4 Uncaught some error in my typescript

但如果您使用打开的开发工具控制台再次刷新页面,则可以将ts文件视为错误来源。