Javascript - Source Map

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > Javascript - (Debug|Diagnostic)

1 - About

A source map provides a mapping between the original and the build source code (combined/minified)

This is an important element used in a debug session in order to be able to stop at breakpoint.

The location coordinates are by line and column number in the source map.

Source maps works for styling as well. See CSS - Source Map

With source maps, it is possible to single step through or set breakpoints in the original source.

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).
Advertising

3 - Library and IDE

3.1 - VsCode

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).

See VsCode - Javascript

3.2 - Webpack

The devtool option controls if and how source maps are generated for devtool.

webpack.config.js
module.exports = {
    mode: 'development',
    entry: './js/index.js',
    devtool: 'source-map',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js'
    }
};

where the devtool value may be:

  • source-map - a full source map is emitted
  • inline-source-map extract source maps (for instance created from typescript) and include them in the final bundle
  • cheap-eval-source-map

Running webpack through:

  • The webpack cli, the source map comes into the output directory
webpack -d

where -d is shorthand for –debug –devtool source-map –output-pathinfo

  • The webpack-dev-server. The map are emitted in memory and then then you don't see them in explorer. You can see that the source map file is emitted with the dev server
Hash: db8af9ffcce7bc166a1d
Version: webpack 3.6.0
Time: 2678ms
          Asset     Size  Chunks                    Chunk Names
      bundle.js  1.07 MB       0  [emitted]  [big]  main
  bundle.js.map  1.09 MB       0  [emitted]         main

See:

Advertising

3.3 - Typescript

The sourceMap option of the compiler controls the generation of a source map.

tsconfig.json
{
  "compilerOptions": {
   ..........
    "sourceMap": true,
    ........
  }

4 - Documentation / Reference