Angular 使用 vscode 源映射的 cli 调试不工作

Angular cli debuging with vs code source map not working

您好,我已经使用 angular-cli 构建了一个应用程序,我正在尝试使用 vs 代码和 chrome 扩展调试器对其进行调试。过了一会儿,我能够让它工作,很好。发生的事情是我可以在我的打字稿中设置一个断点 class 但它被放置在错误的行号上,例如源映射不正确。

调试过程 - 打开终端 ng 服务然后转到调试选项卡并在 vscode

中单击 F5

我有以下内容: 我使用 LaunchChrome 配置

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "LaunchChrome",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:4200",
            "sourceMaps": true,
            "webRoot": "${workspaceRoot}",
            "diagnosticLogging": true,
            "userDataDir": "${workspaceRoot}/.vscode/chrome",
             "sourceMapPathOverrides": {
            "webpack:///C:*": "c:/*"
        }
        },
        {
            "name": "AttachChrome",
            "type": "chrome",
            "request": "attach",
            "port": 9222,
            "sourceMaps": true,
            "webRoot": "${workspaceRoot}",
            "diagnosticLogging": true,
            "sourceMapPathOverrides": {
                "webpack:///*": "/*"
            }
        }
    ]
}

angular-cli.json

{
  "project": {
    "version": "1.0.0-beta.18",
    "name": "frontend"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "./dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "styles.css",
        "../semantic/dist/packaged/semantic.css"
      ],
      "scripts": [
          "../node_modules/jquery/dist/jquery.js",
          "../semantic/dist/packaged/semantic.js",
          "../node_modules/chart.js/dist/Chart.bundle.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false,
    "inline": {
      "style": false,
      "template": false
    },
    "spec": {
      "class": false,
      "component": true,
      "directive": true,
      "module": false,
      "pipe": true,
      "service": true
    }
  }
}

tsconfig.json

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ]
  }
}

我已经更新到 angular-cli-beta19-3 和 typescript 2.0.6 并清除了 chrome 中的缓存,现在可以使用了。

更新:现在使用 angular 2.4.1

有趣的是它不适用于

"sourceMapPathOverrides": {
    "webpack:///*": "${webRoot}/*"
}

此处定义https://github.com/Microsoft/vscode-chrome-debug

但它适用于

"sourceMapPathOverrides": {
    "webpack:///C:*": "c:/*"
}

和 linux 正如@carpinchosaurio 所说

"webpack:///*": "/*"

2017 年 2 月 21 日更新:

使用新版本的 angular 和打字稿,不再需要源映射路径覆盖。

"@angular/compiler-cli": "2.4.8",
"@angular/cli": "1.0.0-beta.32.3",
"typescript": "2.1.6"
angular version 2.4.8

工作设置:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "LaunchChrome",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:4200",
      "sourceMaps": true,
      "webRoot": "${workspaceRoot}",
      "userDataDir": "${workspaceRoot}/.vscode/chrome"
    }
  ]
}

对于仍然感兴趣的任何人,这对我有用 -

 {
        "name": "Launch localhost with sourcemaps",
        "type": "chrome",
        "request": "launch",
        "url": "http://localhost:4200",
        "sourceMaps": true,
        "webRoot": "${workspaceRoot}/src",          
        "userDataDir": "${workspaceRoot}/.vscode/chrome",
        "sourceMapPathOverrides": {              
             "webpack:///./~/*": "${workspaceRoot}/node_modules/*",
             "webpack:///./src/*": "${workspaceRoot}/src/*"
        }
        // Uncomment this to get diagnostic logs in the console
        // "diagnosticLogging": true
    }

只是为了更加强调更新后的 :目前不需要 sourceMapPathOverrides 属性 在您的 launch.json 中。如果您从旧 Angular 更新项目,只需删除 属性 即可开始调试。