1 of 28

Debugging with source maps

@bmeurer - AgentConf’23

​

goo.gle/debugging-with-source-maps

2 of 28

Benedikt Meurer

Engineering Lead�@ChromeDevTools

3 of 28

Who debugs code?

4 of 28

Who debugs using source maps?

5 of 28

Who develops with React, Vue, Svelte, Angular, or the like?

6 of 28

7 of 28

Source code

8 of 28

Project tree

9 of 28

Call Stack

10 of 28

Ctrl + Shift + P

Cmd + Shift + P

11 of 28

12 of 28

🤨

😭

🤔

13 of 28

>95%

of y’all are (or in the past have been) debugging with source maps

14 of 28

Source maps are an essential part of web dev now!

15 of 28

👨‍💻

🖥️

HTML, JS, CSS

16 of 28

👨‍💻

🖥️

HTML, JS, CSS

👨‍💻

🖥️

HTML, JS, CSS

Closure, GSS

17 of 28

👨‍💻

🖥️

HTML, JS, CSS

👨‍💻

🖥️

HTML, JS, CSS

Closure, GSS

👨‍💻

🖥️

HTML, JS, CSS

SASS, TS, Svelte

18 of 28

What are source maps anyways?

A source map is a file that maps from the transformed source to the original source. It is a mapping between the generated/transpiled/minified JavaScript file and one or more original source files. The main purpose of Sourcemaps is to aid debugging. If there is an error in the generated code file, the map can tell you the original source file location. That's it. It is pretty powerful in practice!

Benefits of using source maps:

  • Debugging is easier. When you have a source map, you can easily see where an error occurred in the original source code. This can save you a lot of time and frustration.
  • Minified code is easier to read. Minified code is compressed and optimized to make it smaller. However, this can make it difficult to read and understand. Source maps can help you make sense of minified code by mapping it back to the original source code.
  • You can use source maps with any build tool. Source maps are not tied to any particular build tool. You can use them with any build tool that generates JavaScript files.

If you are developing JavaScript applications, I recommend using source maps. They can make your life much easier!

​

19 of 28

Source maps provide point wise mappings

20 of 28

What are source maps, anyways?

const hello = (name) => {

return `Hello ${name}!`;

};

const hello=l=>`Hello ${l}!`;

//# sourceMappingURL=hello.min.js.map

hello.min.js

hello.js

npx terser

21 of 28

What are source maps, anyways?

const hello = (name) => {

return `Hello ${name}!`;

};

const hello=l=>`Hello ${l}!`;

//# sourceMappingURL=hello.min.js.map

hello.min.js

hello.js

npx terser

{� "version":3,

"sources":["hello.js"],

"mappings":"AAAA,MAAM,MAAS,GACN,SAAS"

}

hello.min.js.map

22 of 28

What are source maps, anyways?

  • list of source files

{� "version":3,

"sources":["hello.js"],

"mappings":"AAAA,MAAM,MAAS,GACN,SAAS"

}

hello.min.js.map

23 of 28

What are source maps, anyways?

  • list of source files
  • tuples of Base64-encoded VLQs (Variable-length quantities)
    • encoding optimized for size

{� "version":3,

"sources":["hello.js"],

"mappings":"AAAA,MAAM,MAAS,GACN,SAAS"

}

hello.min.js.map

24 of 28

What are source maps, anyways?

  • list of source files
  • tuples of Base64-encoded VLQs (Variable-length quantities)
    • encoding optimized for size
  • sourcemap-codec to the rescue!

{� "version":3,

"sources":["hello.js"],

"mappings":[[0,0,0,0],[6,0,0,6], [12,0,0,15],[15,0,1,9], [24,0,1,18]]

}

hello.min.js.map

import {decode}

from '@jridgewell/sourcemap-codec'

decode("AAAA,MAAM,MAAS,GACN,SAAS")

25 of 28

Mappings, explained!

const hello = (name) => {

return `Hello ${name}!`;

};

const hello=l=>`Hello ${l}!`;

//# sourceMappingURL=hello.min.js.map

hello.min.js

hello.js

{� "version":3,

"sources":["hello.js"],

"mappings":[[0,0,0,0],[6,0,0,6], [12,0,0,15],[15,0,1,9], [24,0,1,18]]

}

hello.min.js.map

26 of 28

Extending point-wise mappings to ranges

const hello = (name) => {

return `Hello ${name}!`;

};

const hello=l=>`Hello ${l}!`;

//# sourceMappingURL=hello.min.js.map

hello.min.js

hello.js

{� "version":3,

"sources":["hello.js"],

"mappings":[[0,0,0,0],[6,0,0,6], [12,0,0,15],[15,0,1,9], [24,0,1,18]]

}

hello.min.js.map

27 of 28

Limitations and future directions

  • Syntax of source maps is specified, but semantics are mostly undefined (implementation dependent)
    • how to resolve "sources" entries to URLs?
    • how to expand point-wise mappings to ranges?
    • …
  • Source maps cannot express many things
    • scope information
    • inlining information
    • type information
    • …
  • Chrome DevTools team is looking to properly standardize source maps

28 of 28

Bringing the ecosystem along…