[Vuejs]-How to debug vue.js SFC (Single file component) in vscode, with TypeScript and webpack 4?

0👍

I used the version 3 vue-cli template with TypeScript in Vue.js Single-File-Components rather than the guide you have linked, but I had a similar problem.

The vue-cli v.3 template ends up outputting TypeScript components’ sourcemaps into a ‘.’ folder (but all under webpack://) while the JavaScript components’ sourcemaps end up in a ‘src’ folder. This resulted in the default sourceMapPaths working for the JavaScript SFCs but not the TypeScript SFCs. Therefore, I could set breakpoints in Chrome debugger directly, but not in the original files in VSCode for TypeScript SFCs.

My solution was to correct the mapping via the sourceMapPathOverrides configuration (alternatively, it could be corrected by modifying the build process but this seemed like the simple approach).

In .vscode/launch.json, you can set the appropriate mappings. The config I used ended up looking similar to the following (but you may have to adjust based on your exact setup):

{
  "type": "chrome",
  "request": "launch",
  "name": "ChromeDebug",
  "url": "http://localhost:8080",
  "webRoot": "${workspaceFolder}",
  "breakOnLoad": true,
  "sourceMaps": true,
  "disableNetworkCache": true,
  "sourceMapPathOverrides": {
    "webpack:///*": "${webRoot}/*",
    "webpack:///./*": "${webRoot}/*",
    "webpack:///src/*": "${webRoot}/src/*"
  }
}

(The last sourceMapPathOverrides entry is probably redundant. Your link seems to be using a ‘.://’ root output path rather than my ‘webpack://’ root and you may also need to adjust the webRoot path to wherever your source files are. Don’t forget to change the url/port as well, if it differs from my example.)

And the following is probably not related to OP’s issue, but may be helpful for someone with a similar problem:

When using TypeScript, ensure that you have “sourceMap”: true in your tsconfig.json.

If using vue-cli v.3, you may also need to add a vue.config.js file in the project root, such as the following, to change the devtool value from the default:

module.exports = { configureWebpack: { devtool: "source-map" } };
👤Josh

Leave a comment