Source Maps: What Are Those?
Introduction to source maps, why they matter, how they help debugging, and how source maps are configured in Vite and Webpack.
What Are Source Maps?
Source maps are files that connect your production code back to your original source code.
When you build a modern frontend app, your code usually goes through a lot of changes:
- TypeScript becomes JavaScript
- JSX becomes JavaScript
- Sass or PostCSS becomes CSS
- Files get bundled together
- Code gets minified
- Variable names get shortened
The browser does not run your clean Button.tsx or checkout.ts file directly. It usually runs something like this:
function n(t) {
return t && t.__esModule ? t : { default: t };
}
var r = Object.defineProperty;That is great for performance, but terrible when you need to debug an error.
A source map is the translation table. It tells DevTools:
"This weird minified line in assets/index-a8f3.js came from line 42 in src/components/Button.tsx."
That is the whole magic.
Why Source Maps Exist
Without source maps, production debugging feels like reading a random string.
You might get an error like this:
TypeError: Cannot read properties of undefined
at n (index-BR7s9kLm.js:1:43821)That tells you almost nothing. Which file? Which function? Which component? Good luck.
With source maps, the same error can point back to something readable:
TypeError: Cannot read properties of undefined
at ProductCard (src/components/ProductCard.tsx:28:12)Now you have somewhere to start. That is the benefit. Source maps turn "production is broken somewhere" into "this exact line is suspicious."
What Is Inside a Source Map?
A source map is usually a .map file. It is JSON, but not JSON you usually want to read by hand.
A simplified version looks like this:
{
"version": 3,
"file": "index.js",
"sources": ["src/main.ts", "src/App.tsx"],
"sourcesContent": ["import React from 'react'", "export function App() {}"],
"names": ["React", "App"],
"mappings": "AAAA,SAASA..."
}The important parts:
fileis the generated filesourceslists the original filessourcesContentmay contain the original source codenamesstores original symbol namesmappingsconnects generated line and column positions to original positions
You do not need to memorize this structure. Just remember this: if sourcesContent exists, the map may contain your original code.
That matters a lot in production.
How Browsers Find Source Maps
Usually, the generated JavaScript file has a comment at the bottom:
//# sourceMappingURL=index.js.mapWhen DevTools is open and source maps are enabled, the browser can use that URL to fetch the .map file.
There is also an HTTP SourceMap header that can point DevTools to a map file. Most frontend developers mostly see the comment version, but the header exists and can be useful when you want to avoid putting the source map URL directly inside the built file.
Benefits of Source Maps
1. Better Debugging
This is the obvious one. Source maps let you debug the code you actually wrote, not the code your build tool generated.
You can set breakpoints in your original files, step through TypeScript or JSX, and inspect stack traces that make sense.
2. Better Production Error Reports
Tools like Sentry, Datadog, Rollbar, Bugsnag, and similar monitoring tools can use source maps to turn minified stack traces into readable stack traces.
Without source maps:
at o (app.8af2.js:1:18442)With source maps:
at submitPayment (src/features/payment/submitPayment.ts:74:10)That difference can save hours.
3. Easier Debugging for CSS and Preprocessors
Source maps are not only for JavaScript.
If you use Sass, PostCSS, CSS modules, or another CSS build step, source maps can help DevTools show the original source file instead of only the generated CSS.
4. Better Developer Experience
Source maps make debugging feel normal again.
When they work well, you do not think about them. You open DevTools, click the file you recognize, set a breakpoint, and move on with your life.
That is the best kind of tooling: invisible until you need it.
The Trade Offs
Source maps are useful, but they are not free.
Bigger Build Output
Map files can be large. Sometimes they are larger than the minified bundle itself because they contain mapping data and maybe original source code.
Slower Builds
Generating high quality source maps can slow down build time. This is especially noticeable in large apps.
Possible Source Exposure
If you publish .map files publicly and those maps include sourcesContent, users can inspect a version of your original source code.
That does not automatically mean your app is insecure. Frontend code is already shipped to the browser in some form. But source maps can make it much easier to read.
Do not put secrets in frontend code. Source maps are not the real problem if secrets are already bundled into client code.
Vite Source Maps
Vite keeps the production source map setting simple.
In vite.config.ts:
import { defineConfig } from "vite";
export default defineConfig({
build: {
sourcemap: true,
},
});Vite's build.sourcemap supports these values:
false: do not generate production source mapstrue: generate separate.mapfiles"inline": put the source map inside the output file as a data URI"hidden": generate source maps, but do not add the source map comment to the bundle
For example:
import { defineConfig } from "vite";
export default defineConfig({
build: {
sourcemap: true,
},
});import { defineConfig } from "vite";
export default defineConfig({
build: {
sourcemap: "hidden",
},
});The "hidden" option is useful when you want source maps for error monitoring, but you do not want browsers to automatically discover them through sourceMappingURL.
In development, Vite is already close to your source files because it serves modules through native ESM and applies transforms on demand. That is one reason Vite often feels easier to debug locally.
Webpack Source Maps
Webpack gives you more knobs.
The main setting is devtool:
module.exports = {
devtool: "source-map",
};But webpack does not only have true or false. It has many modes because it lets you trade off build speed, rebuild speed, map quality, whether maps are inline, whether sources are included, and whether maps are meant for development or production.
Common examples:
module.exports = {
// Good quality for development, slower first build, fast rebuilds
devtool: "eval-source-map",
};module.exports = {
// Full separate map file for production
devtool: "source-map",
};module.exports = {
// Useful for error reporting without browser auto-discovery
devtool: "hidden-source-map",
};module.exports = {
// Generates a map without embedding the original source content
devtool: "nosources-source-map",
};Webpack also has SourceMapDevToolPlugin when you need more control, like custom filenames, excluding vendor maps, or hosting maps somewhere else.
That flexibility is powerful, but it is also why webpack source map configuration can feel intimidating.
Vite vs Webpack
Here is the comparison between them:
| Topic | Vite | Webpack |
|---|---|---|
| Main config | build.sourcemap | devtool |
| Production default | false | Usually no source maps unless configured |
| Config style | Small option set | Larger option matrix |
| Dev experience | Fast module serving with native ESM | Depends heavily on which devtool mode you choose |
| Fine control | Simpler, but less granular | More granular control |
| Hidden maps | sourcemap: "hidden" | devtool: "hidden-source-map" |
| No source content | Usually handled through lower-level output config or tooling | nosources-source-map |
Vite is easier when you want the common cases:
build: {
sourcemap: true,
}or:
build: {
sourcemap: "hidden",
}Webpack is better when you need very specific behavior:
- line-only maps for faster builds
- eval-based maps for quick rebuilds
- no original source content
- custom source map file paths
- excluding specific chunks
- external map hosting
Neither is "better" in every case. Vite optimizes for a simpler workflow. Webpack gives you a bigger control panel.
What Should You Use in Production?
My default recommendation:
export default defineConfig({
build: {
sourcemap: "inline",
},
});export default defineConfig({
build: {
sourcemap: "hidden",
},
});For webpack:
module.exports = {
devtool: "inline-source-map",
};module.exports = {
devtool: "hidden-source-map",
};Why? Because inline maps can put a lot of debugging data directly into the built file. That is convenient locally, but not usually what you want for production.
For most production apps, a good setup is:
- Generate source maps during CI.
- Upload them to your error monitoring tool.
- Avoid serving full source maps publicly unless you are okay with that.
- Make sure the uploaded maps match the exact deployed build.
That last point is important. If the source map comes from a different build than the JavaScript file in production, your stack traces will point to the wrong lines.
Common Source Map Problems
DevTools Says It Cannot Load the Map
Usually this means the browser found a sourceMappingURL, but the .map file is missing or not accessible.
Check:
- Was the
.mapfile uploaded? - Is the path correct?
- Is your server blocking
.mapfiles? - Did the file name include a content hash that changed?
Stack Traces Still Look Minified
This usually means your monitoring tool cannot match the error to the right source map.
Check:
- Is the release/version the same?
- Are the asset URLs the same?
- Did you upload maps after the production build?
- Did your deploy upload different files than your CI build?
Breakpoints Jump to Weird Lines
This can happen when maps are low quality, line-only, or produced by a tool in the middle of your build chain.
In webpack, options with cheap are faster but less precise because they usually skip column mappings. That can be fine for development speed, but it is not the best choice when you need exact breakpoint behavior.
Source Code Is Exposed
If your .map file is public and includes sourcesContent, people can read the original source more easily.
Again, do not rely on hiding frontend source code for security. But if you do not want to publish readable source, use hidden maps, private upload flows, or no-sources maps.
A Simple Mental Model
Use this:
- Development: optimize for debugging comfort
- CI: generate maps when you need production error tracking
- Production public files: be intentional about what you expose
- Error monitoring: upload maps privately when possible
That is enough for most teams.
Final Thoughts
Source maps are one of those tools you do not appreciate until something breaks in production.
When they are missing, every stack trace looks like a puzzle. When they are configured well, you can jump from a minified production error back to the exact file and line you wrote.
Vite makes the common source map setup simple. Webpack gives you more choices and more responsibility. Either way, the goal is the same: make debugging less painful without accidentally publishing more source code than you intended.
Stay Updated
Get notified when I publish new articles about web development, frontend engineering, and more.
No spam, unsubscribe anytime.