Your error monitoring reports an error from production: at r (bundled.js:10:25). Function r? You never wrote a function called r, and bundled.js holds nothing but long lines no human can read.
What translates it back is a source map, a .map file that records, for positions in a built JavaScript file, which file, which line and which column that code was originally written in. Picture a novel reprinted as one long page with no paragraphs to save paper. A reader who finds a typo can only say "page 10, letter 25". The source map is the publisher's cheat sheet that turns that position into "chapter App, line 42, letter 10" of the original manuscript.
Why a production stack trace can't be read
Before it is deployed, your code goes through a bundler (Vite, webpack, esbuild): many files are joined into one (the bundle), then minified, meaning spaces and comments are dropped and variable names are shortened. Here are two pieces of original code:
// src/App.tsx, lines 40 to 43
export function greet(user: User) {
return "Welcome " +
user.name +
", how are you?";
// src/utils.ts, lines 6 to 9
export function forEachItem<T>(list: T[], fn: (item: T) => void) {
for (const item of list) {
fn(item);
}
In bundled.js, both become one line, line 10:
const r=t=>"Welcome "+t.name+", how are you?";function o(t,n){for(const e of t)n(e)}
When forEachItem is called with a list that holds undefined, Chrome prints this stack trace (with the file's full address shortened to bundled.js):
TypeError: Cannot read properties of undefined (reading 'name')
at r (bundled.js:10:25)
at o (bundled.js:10:80)
at bundled.js:99:1
The format is file:line:column, and a stack trace counts lines and columns from 1. Column 25 on line 10 is the name in t.name, and column 80 is n(e). But r, o and bundled.js say nothing about your original files.
What's in a .map file
The bundler can also produce bundled.js.map, a JSON file:
{
"version": 3,
"file": "bundled.js",
"sources": ["src/App.tsx", "src/utils.ts"],
"sourcesContent": ["...all of App.tsx...", "...all of utils.ts..."],
"names": [],
"mappings": ";;;;;;;;;wBAyCS,uDClCL"
}
sources: the list of original files. Index 0 issrc/App.tsx, index 1 issrc/utils.ts.sourcesContent: the full content of those original files, so DevTools can show them.mappings: each;separates a line of the bundle, and each,separates a segment within one line. There are 9 semicolons beforewBAyCS, so both segments belong to line 10.- The bundle's last line holds the comment
//# sourceMappingURL=bundled.js.map, so the browser knows where the.mapfile is.
Each segment stores four numbers in Base64 VLQ (numbers written with letters to keep them short): the column in the bundle, the index of the original file, the original line and the original column. Decoded and counted from 1, they are these two mappings:
| Position in the bundle | Original file | Line | Column |
|---|---|---|---|
| line 10, column 25 | src/App.tsx |
42 | 10 |
| line 10, column 80 | src/utils.ts |
8 | 5 |
Matching a position (what the exercise does)
In the exercise, mappings is already decoded into an array like the table above. Your job is to find the original position for a bundle position:
function createSourceMapResolver(mappings) {
let unmapped = 0;
function resolve(line, column) {
const match = mappings.find((m) => m.bundled.line === line && m.bundled.column === column);
if (!match) {
unmapped++; // counted here only, once per failed lookup
return null;
}
return match.original; // { file, line, column }
}
return {
resolve,
resolveFrame(frame) {
const [file, line, column] = frame.split(':'); // "bundled.js:10:80"
const original = resolve(Number(line), Number(column));
if (!original) return frame; // no mapping: return it as it is
return original.file + ':' + original.line + ':' + original.column;
},
unmappedCount: () => unmapped,
};
}
const r = createSourceMapResolver([
{ bundled: { line: 10, column: 25 }, original: { file: 'src/App.tsx', line: 42, column: 10 } },
{ bundled: { line: 10, column: 80 }, original: { file: 'src/utils.ts', line: 8, column: 5 } },
]);
const r1 = r.resolve(10, 25);
console.log(r1.file); // src/App.tsx
console.log(r1.line); // 42
console.log(r.resolveFrame('bundled.js:10:80')); // src/utils.ts:8:5
console.log(r.resolveFrame('bundled.js:99:1')); // bundled.js:99:1
console.log(r.resolve(5, 5)); // null
console.log(r.unmappedCount()); // 2
resolvelooks for the mapping whose bundle line and column match exactly, then returnsoriginal, ornullwhen there is none.frame.split(':')splits"bundled.js:10:80"into["bundled.js", "10", "80"].Number(...)turns text into a number, because"10" === 10isfalse.bundled.js:99:1(the calling code on line 99) has no mapping, so it comes back whole, notnulland not an empty string.unmappedonly goes up insideresolve.resolveFramecallsresolve, so if you raise it in both places, one failed frame is counted twice. In this example two lookups fail:99:1and(5, 5).
As a result, the stack trace becomes at src/App.tsx:42:10, at src/utils.ts:8:5 and at bundled.js:99:1. Line 42 of App.tsx is user.name: the user is undefined.
In the exercise the match is exact. DevTools and Sentry are a little looser: they use the nearest segment to the left on the same line, so even column 30 still lands on src/App.tsx:42:10.
Source maps in production
A source map holds sourcesContent, which is all of your original code, with variable names and comments. If the .map file can be downloaded publicly, anyone can read your code. In vite.config.js, set build.sourcemap:
sourcemap: truemakes the.mapand adds thesourceMappingURLcomment. Fine for code that is public anyway.sourcemap: 'hidden'makes the.mapwithout that comment. Upload the.mapto your error tracker (Sentry has a Vite plugin andsentry-cli), and don't serve it publicly. Sentry then translates the stack traces.- In webpack, the equivalents are
devtool: 'source-map'anddevtool: 'hidden-source-map'. - Besides the comment, the server can point to the
.mapfile with the HTTP headerSourceMap: <url>. When both the header and the comment are there, the header wins (the source map standard, ECMA-426). The old nameX-SourceMapis deprecated. ignoreListinside the.maplists third-party files, such as framework code. DevTools skips those files while you step, so you don't get lost in library code.
In Chrome, source maps are used automatically when Settings → Preferences → Sources → "JavaScript source maps" is on (it is on by default) and the .map file can be fetched. The Sources panel then shows the original src/App.tsx, and you can set breakpoints there.
💡 Reading one VLQ segment
The segment wBAyCS is decoded letter by letter. Each Base64 letter is worth 0 to 63 (A = 0, a = 26, w = 48). The bit worth 32 means "the number carries on into the next letter", the other 5 bits are its content, and the lowest bit of the finished number is the minus sign.
| Letters | Values | Carries on? | Finished number |
|---|---|---|---|
w B |
48, 1 | yes, no | 16 + 1 × 32 = 48, low bit 0, so 48 / 2 = 24 |
A |
0 | no | 0 |
y C |
50, 2 | yes, no | 18 + 2 × 32 = 82, so 41 |
S |
18 | no | 18, so 9 |
The result is [24, 0, 41, 9]: column 24, file index 0, line 41, column 9, all counted from 0. Add 1 and column 25 in the bundle is src/App.tsx:42:10. The next segment, uDClCL, holds [55, 1, -34, -5]: those numbers are differences from the previous segment, so its column is 24 + 55 = 79, its file is index 1, its line is 41 - 34 = 7, and its column is 9 - 5 = 4.
⚠️ Why the position is off, or your code leaks
- A
.mapserved publicly. Your code can be read whole. Usesourcemap: 'hidden', upload to the error tracker, and don't deploy the.mapfile. - A map from another build. A source map only matches the bundle built with it. If the uploaded map comes from an old build, the positions point at the wrong lines, with no error at all.
- Counting from 0 or from 1. A stack trace counts lines and columns from 1, the
mappingscontent from 0. - In the exercise: a frame with no mapping.
resolveFrame('bundled.js:99:1')must return"bundled.js:99:1", andunmappedCount()must be2, not3: raise the counter inresolveonly.
TL;DR: A source map (.map) maps positions in a minified bundle to the original file, line and column. The stack trace bundled.js:10:25 becomes src/App.tsx:42:10 through the mapping. The mappings content is Base64 VLQ segments, split by ; per bundle line and , per segment, counted from 0. In the exercise: resolve looks for an exact match or returns null, resolveFrame splits file:line:column and returns the original frame when nothing matches, and unmappedCount counts each failed lookup once. In production use sourcemap: 'hidden' and upload to an error tracker, because a public .map means all of your original code can be read. In Chrome, turn on "JavaScript source maps".
⚡ A production stack trace can't be read
Write:
TypeError: Cannot read properties of undefined (reading 'name')
at r (bundled.js:10:25)
at o (bundled.js:10:80)
at bundled.js:99:1
Meaning: A bundler joins many files into one, then minifies it: spaces and comments are dropped, variable names are shortened. r, o and bundled.js say nothing about your original files.
⚡ A source map translates it back
Guess first: through the source map, what does bundled.js:10:25 become?
src/App.tsx:42:10, which is user.name. The user is undefined.
Meaning: A .map file records, for positions in the bundle, the original file, line and column. Chrome uses it automatically as long as the .map file can be fetched.
⚡ Matching a position
Write:
const match = mappings.find((m) => m.bundled.line === line && m.bundled.column === column);
Meaning: In the exercise, resolve looks for the mapping whose line and column match exactly, or returns null. A frame with no mapping comes back as it is.
⚡ Don't serve the .map publicly
Guess first: why is a .map file anyone can download dangerous?
It holds sourcesContent: all of your original code, with variable names and comments. Anyone can read it.
Meaning: Use sourcemap: 'hidden', upload the .map to your error tracker, and don't deploy it.