Compacts sourcemaps, merging spurious mappings.

Downloads in past


1.0.17 years ago7 years agoMinified + gzip package size for source-map-compactor in KB


Compacts sourcemaps, merging spurious mappings.


I love Rollup. I really do.
Sometimes the sourcemaps look crazy, and make my browser choke on them, confuse variable scopes, prevent setting breakpoints, and generally misbehave.
This is due to the way magic-string handles sourcemap mappings.
To remedy that, source-map-compactor looks at a sourcemap and performs four naïve optimizations:
  • Inserts blank line mappings when there is no mapping for a generated line.
  • Inserts blank character mappings at column 0 if there is no mapping for column 0 in a generated line.
  • Removes duplicated mappings to the same generated line-column (happens when magic-string replaces something with an empty string)
  • Removes duplicated mappings for contiguous segments of the same length (happens when magic-string copies several tokens in succesion)

These optimizations are not exhaustive, but they should create simpler and more reliable sourcemaps in most workflows.


The optimizations are easier to see with something like source-map-visualization:


Sourcemap visualization before


Sourcemap visualization after


Install it from NPM:
yarn add source-map-compactor

In JS, require it and call it. It expects a string representing the sourcemap JSON, and returns a string representing a sourcemap JSON.
var compactor = require('source-map-compactor');
var fs = require('fs');

var sourceMapText = fs.readFileSync('').toString();
var compactedSourceMapText = compactor(sourceMapText);
fs.writeFileSync('', compactedSourceMap);

In a shell, run source-map-compactor with the filenames for input and output files, e.g.:
./node_modules/source-map-compactor build/ build/


"THE BEER-WARE LICENSE": wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return.