【问题标题】:Angular cli debuging with vs code source map not working使用 vscode 源映射的 Angular cli 调试不起作用
【发布时间】:2017-03-16 10:20:04
【问题描述】:

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

调试过程 - 打开终端 ng serve 而不是转到调试选项卡并在 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"
    ]
  }
}

【问题讨论】:

    标签: debugging angular visual-studio-code angular-cli


    【解决方案1】:

    我已经更新到 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:/*"
    }
    

    正如@carpinchosaurio所说,对于linux

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

    2017 年 2 月 21 日更新:

    使用新版本的 angular 和 typescript,不再需要源地图路径覆盖。

    "@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"
        }
      ]
    }
    

    【讨论】:

    • 谢谢你,但是你是怎么意识到的呢?对于 Linux "webpack:///*": "/*" 并且它可以工作,但严重的是为什么? D=
    • 我在玩它,我会把你的答案融入我的
    【解决方案2】:

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

     {
            "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
        }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2018-05-13
        • 2023-03-25
        • 2018-04-18
        • 2022-06-29
        • 1970-01-01
        • 2017-03-22
        • 1970-01-01
        • 2018-09-28
        • 1970-01-01
        相关资源
        最近更新 更多