Skip to content

Instantly share code, notes, and snippets.

@jarshwah
Created April 23, 2017 11:28
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jarshwah/389f93f2282a165563990ed60f2b6d6c to your computer and use it in GitHub Desktop.
Save jarshwah/389f93f2282a165563990ed60f2b6d6c to your computer and use it in GitHub Desktop.
Webpack Source Maps with vscode debugging
// debug config for running project under vscode debugger
{
"version": "0.2.0",
"configurations": [
{
"trace": true,
"name": "Chrome Debug",
"type": "chrome",
"request": "launch",
"url": "http://localhost:8000/",
"webRoot": "${workspaceRoot}/src/", // folder containing webpack.config.js
"userDataDir": "${workspaceRoot}/.vscode/chrome",
"sourceMaps": true,
"disableNetworkCache": true,
// we have multiple js source folders, so some source maps are still generated with webpack protocol links. Don't know why?
"sourceMapPathOverrides": { // if you override this, you MUST provide all defaults again
"webpack:///./~/*": "${webRoot}/node_modules/*", // a default
"webpack:///./*": "${webRoot}/js-src/*", // unsure how/why webpack generates ./links.js
"webpack:///../*": "${webRoot}/js-src/*", // unsure how/why webpack generates ../links.js
"webpack:///*": "*" // a default, catch everything else
}
}
]
}
// source: ./js-src/<item>
// target: ./static/js-build/<item>
// Do not use uglify plugin when debugging, did not try to get source maps working with uglify
{
resolve: {
root: [ // older webpack config
path.join(__dirname, '/js-src'), // source at ./js-src/
]
devtool: "source-map", // many options, but this one works best for me: https://webpack.js.org/configuration/devtool/
output: {
filename: '[name].js',
path: path.join(__dirname, '/static/js-build'), // compile to ./static/js-build
devtoolModuleFilenameTemplate: 'file:///[absolute-resource-path]' // map to source with absolute file path not webpack:// protocol
}
}
@lahbib
Copy link

lahbib commented Aug 2, 2019

Nice work jarshwah :)

@jmbeach
Copy link

jmbeach commented Nov 7, 2019

thanks a ton. the devtoolModuleFilenameTemplate got me what I needed.

@kvn1351
Copy link

kvn1351 commented Apr 9, 2020

+1 for devtoolModuleFilenameTemplate. Thanks.

@jeromerg
Copy link

4 years later: thank you!!
I couldn't get together: Webpack + Typescript + nodejs (nestjs) + Webpack HMR (hot module replacement) + Debugging

devtoolModuleFilenameTemplate: 'file:///[absolute-resource-path]' was the last missing stone.

@jdkuhnke
Copy link

jdkuhnke commented Feb 8, 2021

Another +1 for devtoolModuleFilenameTemplate. Thanks!

I am using Webpack + Typescript only as an extension module for our web app using non-webpack build tooling.
devtoolModuleFilenameTemplate: 'file:///[absolute-resource-path]' fixed the debug mode in VSCode.

Copy link

ghost commented Oct 18, 2022

+1, the devtoolModuleFilenameTemplate solution fixed our AngularJS+Lit+TypeScript+Webpack setup.

@Amelia9449
Copy link

+1 for even now, thanks a bunch!

using Webpack + Typescript + nodejs(nestjs) + Webpack HMR just like jeromerg above
My project was missing devtool: "source-map" and devtoolModuleFilenameTemplate: 'file:///[absolute-resource-path]'
Works like a charm!

@sebastiaanviaene
Copy link

sebastiaanviaene commented Jun 21, 2023

Lifesaver! the devtoolModuleFilenameTemplate: 'file:///[absolute-resource-path]' did the trick for me as well.
I was doing a complex set up with Nx + NestJS + Typescript + Webpack so was really hard to know where to look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment