As of version 1.9, VS Code automatically tries to use source maps by default, but you have to specify outFiles
if the transpiled files are not in the same folder as the source files.
As an example, here are the relevant files. In this case, babel is transpiling from the src
folder to the lib
folder.
Note: The entries in package.json
and .vscode/tasks.json
are only required if you want VS Code to transpile the files before debugging.
.vscode/launch.json
Ctrl+Shift+P, >Debug: Open launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/lib/index.js",
"cwd": "${workspaceRoot}",
"preLaunchTask": "build",
"outFiles": [
"${workspaceRoot}/lib/**.js"
]
}
]
}
Note: Only specify preLaunchTask
if you also set up the build
tasks in package.json
and .vscode/tasks.json
.
package.json
Ctrl+P, package.json
{
"scripts": {
"build": "babel src -d lib -s"
},
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-preset-env": "^1.1.10"
}
}
Note: You may use a different version of babel-cli
and different babel presets.
.vscode/tasks.json
Ctrl+Shift+P, >Tasks: Configure Task Runner
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"showOutput": "always",
"suppressTaskName": true,
"tasks": [
{
"taskName": "build",
"args": ["run", "build"],
"isBuildCommand": true
}
]
}
Official VS Code Documentation
Source Maps
The Node.js debugger of VS Code supports JavaScript Source Maps which help debugging of transpiled languages, e.g. TypeScript or minified/uglified JavaScript. With source maps, it is possible to single step through or set breakpoints in the original source. If no source map exists for the original source or if the source map is broken and cannot successfully map between the source and the generated JavaScript, then breakpoints show up as unverified (gray hollow circles).
Source maps can be generated with two kinds of inlining:
- Inlined source maps: the generated JavaScript file contains the source map as a data URI at the end (instead of referencing the source map through a file URI).
- Inlined source: the source map contains the original source (instead of referencing the source through a path).
VS Code supports both the inlined source maps and the inlined source.
The source map feature is controlled by the sourceMaps
attribute which defaults to true
starting with VS Code 1.9.0. This means that node debugging always tries to use source maps (if it can find any) and as a consequence you can even specify a source file (e.g. app.ts) with the program
attribute.
If you need to disable source maps for some reason, you can set the sourceMaps
attribute to false
.
If the generated (transpiled) JavaScript files do not live next to their source but in a separate directory, you must help the VS Code debugger locating them by setting the outFiles
attribute. This attribute takes multiple glob patterns for including and excluding files from the set of generated JavaScript files. Whenever you set a breakpoint in the original source, VS Code tries to find the generated JavaScript code in the files specified by outFiles
.
Since source maps are not automatically created, you must configure the transpiler you are using to create them. For TypeScript this can be done in the follwoing way:
tsc --sourceMap --outDir bin app.ts
This is the corresponding launch configuration for a TypeScript program:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch TypeScript",
"type": "node",
"request": "launch",
"program": "app.ts",
"outFiles": [ "bin/**/*.js" ]
}
]
}
Source