vscode 调试 TypeScript

安装 typescript 依赖

npm install typescript --save-dev

目录结构:

 

添加 tsconfig.json

主要是将 sourceMap 设置为true

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5", "noImplicitAny": true, "outDir": "./dist", "sourceMap": true }, "include": [ "src/**/*" ] }

配置自动编译

利用 vscode 的 tasks 自动将 ts 编译为 js。也可以使用别的方式编译,如:gulp,webpack 等。
添加文件: /.vscode/tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for thedocumentation about the tasks.json format
   "version": "0.1.0",
   "command": "tsc", "isShellCommand": true, //-p 指定目录;-w watch,检测文件改变自动编译 "args": ["-p", ".","-w"], "showOutput": "always", "problemMatcher": "$tsc" } 

使用快捷键 Ctrl + Shift + B 开启自动编译。

配置调试

调试时,需要配置 vscode 的 launch.json 文件。这个文件记录启动选项。
添加或编辑文件 /.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "launch", "type": "node", "request": "launch", "program": "${workspaceRoot}/dist/main.js", "args": [], "cwd": "${workspaceRoot}", "protocol": "inspector" } ] }

注意 : program 需设置为你要调试的 ts 生成的对应的 js。 
假如需要调试 /src/main.ts,则此处为 ${workspaceRoot}/dist/main.js

调试

打开 main.ts,在左侧添加断点,进行调试。

 

使用 ts-node 调试 ts 文件

源码:github
来自:Debugging TypeScript in VS Code without compiling, using ts-node
ts-node 调试 ts 文件时,不会显式生成 js。假如你不想编译为 js 后 在调试,可以考虑这种方式。

安装 npm 依赖包

npm install typescript --save-dev
npm install ts-node --save-dev

配置 tsconfig.json

主要是将 sourceMap 设置为true

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5", "noImplicitAny": true, "outDir": "./dist", "sourceMap": true }, "include": [ "src/**/*" ] }

配置 launch.json

打开 DEBUG 界面,添加 配置
或者编辑 /.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Current TS File", "type": "node", "request": "launch", "program": "${workspaceRoot}/node_modules/ts-node/dist/_bin.js", "args": [ "${relativeFile}" ], "cwd": "${workspaceRoot}", "protocol": "inspector" } ] }

调试

  1. 打开要调试的 ts 文件,添加debugger

  2. 打开 debug 界面。

  3. DEBUG后 选择 launch.json 中对应的配置,此处为Current TS File

  4. 点击运行按键或者按 F5 运行。

 

转载于:https://www.cnblogs.com/jiangzhaowei/p/10298086.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值