Introduction
A source map links code that a build tool has generated back to the source it came
from. Web code is often compiled, bundled and minified before it is served: TypeScript
becomes JavaScript, many modules become one file, and names are shortened. A source map,
usually named after the output with .map added, such as
app.js.map or styles.css.map, lets browser developer tools
and error trackers show the original files, lines and names instead.
The generated file points to its map with a comment on its last line, such as
//# sourceMappingURL=app.js.map, or with a SourceMap HTTP
header. The .map extension is also used for
linker map files and for other unrelated
formats.
Structure
A source map is a JSON object. version is 3; file names the
generated file; sources lists the original files, optionally relative to
sourceRoot; sourcesContent can hold their text so that the
originals need not be served; and names lists the original identifiers.
The mappings string holds the actual map.
In mappings, a semicolon starts each new line of the generated file and
commas separate segments within a line. Each segment is one, four or five numbers in
Base64 VLQ encoding: the column in the generated line, then the index of the source
file, the line and column in it, and the index of the name. Each number is stored as
the difference from the previous one, which keeps the string short. Every key, the
mappings and VLQ encoding with worked examples, and index maps are on the
keys and mappings page.
{
"version": 3,
"file": "app.js",
"sources": ["src/app.ts"],
"names": ["greet", "name"],
"mappings": "AAAA,SAASA,MAAMC"
}
Not described
Known, not yet covered
- Reference: the decoding algorithm and its error handling (ECMA-426 sections 9.1 and 9.2): which faults are thrown (map not an object,
mappingsnot a string,sourcesnot an array, sectionoffsetormapnot an object) and which are only optionally reported with a fallback (wrongversion; a non-stringsourcesentry becomes null and a non-stringnamesentry the empty string; invalidignoreListentries are skipped; an unparsablemappingsstring gives no mappings; a negative generated column drops the segment; an out-of-range source, name, line or column index gives a null original position or name;sourcesContentshorter thansources; the decoded mappings sorted by generated position). Not on the format page. - Reference: the unit of a column: UTF-16 code units for JavaScript and CSS maps, byte indexes into the binary (one line) for WebAssembly maps (ECMA-426 section 5). Not on the format page.
- Reference: where mappings may point and which tokens get names in generated JavaScript (ECMA-426 sections 9.2.3 and 9.2.4: the token and comment kinds a mapping may target, and the named-mapping rules for declarations, functions, arrow functions and methods).
- Reference: how an index map is decoded (section 10.1): sources of all sections merged without duplicates, the offset added to the column only on a section’s first generated line and to every line, an invalid section skipped, and the optional order and overlap checks. Only the key table is on the format page.
- Reference: finding the map URL (section 11.1): the JavaScript extraction methods (through parsing and without parsing, scanning from the end of the file, the quote and
*/rejection rule, the^[@#]\s*sourceMappingURL=(\S*?)\s*$match), unambiguous linking, data URIs and percent-encoding, the source origin rules (scriptsrc, inline script,eval,//# sourceURL), and the WebAssembly custom section being a WebAssembly name. The format page gives only the comment forms. - Reference: the GetOriginalPositions lookup (section 12.1), the fetch failure rule of FetchSourceMap (a failed fetch, UTF-8 or JSON error rejects), and the informative annexes (naming convention,
//# sourceURLfor eval code, multi-level mapping). - Viewer: there is no viewer for source maps yet, so mappings are not decoded or shown.
Also used for
- Linker map file – A linker’s text report of sections, addresses and symbols in a program.
Links
Software
- Chrome DevTools – the browser’s developer tools, which read source maps; Firefox, Safari and Edge do the same (Windows, macOS, Linux)
- TypeScript – compiler that writes source maps with the
sourceMapoption (Windows, macOS, Linux) - esbuild – fast bundler and minifier that writes source maps (Windows, macOS, Linux)
- webpack – JavaScript bundler that writes source maps (Windows, macOS, Linux)
- Sass – CSS preprocessor that writes CSS source maps (Windows, macOS, Linux)
Developers
- source-map – Mozilla’s library that reads and writes source maps (JavaScript)
Reference
- Source map format specification – the ECMA-426 standard