Debug & Run Angular2 Typescript with Visual Studio Code?

后端 未结 10 1107
情歌与酒
情歌与酒 2020-11-27 10:08

Debug & Run Angular2 Typescript with Visual Studio Code?

I am trying to debug Angular2 typescript application with VS code https://angular.io/gu

10条回答
  •  [愿得一人]
    2020-11-27 10:41

    I was having a similar issue but my project also included webpack that caused the above solutions to fail. After traversing the Internet I found a solution in a thread on github:

    https://github.com/AngularClass/angular2-webpack-starter/issues/144#issuecomment-218721972

    Anyway, this is what was done.

    Note:- Before you start you must check whether you have the latest version of visual studio code and also have installed the extension called 'Debugger for Chrome' within VS Code.

    Firstly, in './config/webpack.dev.js'

    • use => devtool: 'source-map'
    • instead of => devtool: 'cheap-module-source-map'

    Then install and use the write-file-webpack-plugin:

    • npm install --save write-file-webpack-plugin

    Add the plugin to './config/webpack.dev.js' by adding:

    • const WriteFilePlugin = require('write-file-webpack-plugin');

    under the Webpack Plugins at the top. Continue to add:

    • new WriteFilePlugin()

    to the list of plugins after new new DefinePlugin(), i.e

    plugins:[
        new DefinePlugin({....}),
        new WriteFilePlugin(),
        ....
    ]
    

    This ensures that the source maps are written to disk

    Finally, my launch.json is given below.

    {
        "version": "0.2.0",
        "configurations": [{
            "name": "Launch Chrome against localhost, with sourcemaps",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:3000/",
            "runtimeArgs": [
               "--user-data-dir",
               "--remote-debugging-port=9222"
            ],
            "sourceMaps": true,
            "diagnosticLogging": true,
            "webRoot": "${workspaceRoot}",
            "userDataDir": "${workspaceRoot}/.vscode/chrome"
        },
        {
            "name": "Attach to Chrome, with sourcemaps",
            "type": "chrome",
            "request": "attach",
            "url": "http://localhost:3000/",
            "port": 9222,
            "sourceMaps": true,
            "diagnosticLogging": true,
            "webRoot": "${workspaceRoot}"
        }]
    }
    

    notice the absence of '/dist/' in the webroot. with this config, source-maps are in ./dist/, but they point to ./src/. vscode prepends the absolute root to the workspace, and correctly resolves the file.

提交回复
热议问题