Branch: master
Clone or download
Pull request Compare This branch is 598 commits behind mozilla:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Source Map

This is a library to generate and consume the source map format described here.

Learn more here.

This library was written in the Asynchronous Module Definition format. It should work in the following environments:

  • Modern Browsers (either after the build, or with an AMD loader such as RequireJS)

  • Inside Firefox (as a JSM file, after the build)

  • With NodeJS versions 0.4.X

Installing with NPM (for use with NodeJS)


$ npm install source-map

Or, if you'd like to hack on this library and have it installed via npm so you can try out your changes:

$ git clone
$ cd source-map
$ npm link .

Building from Source (for everywhere else)

Install NodeJS and Dryice. Then run node Makefile.dryice.js. This should create the following files:

  • dist/source-map.js - The unminified browser version.

  • dist/source-map.min.js - The minified browser version.

  • dist/SourceMapConsumer.jsm - The JavaScript Module for inclusion in Firefox source.


Get a reference to the module:

// NodeJS
var sourceMap = require('source-map');

// Browser builds
var sourceMap = window.sourceMap;

// Inside Firefox
let sourceMap = {};
Components.utils.import('resource:///modules/SourceMapConsumer.jsm', sourceMap);


A SourceMapConsumer instance represents a parsed source map which we can query for information about the original file positions by giving it a file position in the generated source.

new SourceMapConsumer(rawSourceMap)

The only parameter is the raw source map (either as a string which can be JSON.parse'd, or an object). According to the spec, source maps have the following attributes:

  • version: Which version of the source map spec this map is following.

  • sources: An array of URLs to the original source files.

  • names: An array of identifiers which can be referrenced by individual mappings.

  • sourceRoot: Optional. The URL root from which all sources are relative.

  • mappings: A string of base64 VLQs which contain the actual mappings.

  • file: The generated filename this source map is associated with.


Returns the original source, line, and column information for the generated source's line and column positions provided. The only argument is an object with the following properties:

  • line: The line number in the generated source.

  • column: The column number in the generated source.

and an object is returned with the following properties:

  • source: The original source file, or null if this information is not available.

  • line: The line number in the original source, or null if this information is not available.

  • column: The column number in the original source, or null or null if this information is not available.

  • name: The original identifier, or null if this information is not available.


An instance of the SourceMapGenerator represents a source map which is being built incrementally.

new SourceMapGenerator(startOfSourceMap)

To create a new one, you must pass an object with the following properties:

  • file: The filename of the generated source that this source map is associated with.

  • sourceRoot: An optional root for all relative URLs in this source map.


Add a single mapping from original source line and column to the generated source's line and column for this source map being created. The mapping object should have the following properties:

  • generated: An object with the generated line and column positions.

  • original: An object with the original line and column positions.

  • source: The original source file (relative to the sourceRoot).

  • name: An optional original token name for this mapping.


Renders the source map being generated to a string.


SourceNodes provide a way to abstract over interpolating and/or concatenating snippets of generated JavaScript source code, while maintaining the line and column information associated between those snippets and the original source code. This is useful as the final intermediate representation a compiler might use before outputting the generated JS and source map.

new SourceNode(line, column[, chunk])

  • line: The original line number associated with this source node, or null if it isn't associated with an original line.

  • column: The original column number associated with this source node, or null if it isn't associated with an original column.

  • chunk: Optional. Is immediately passed to SourceNode.prototype.add, see below.


Add a chunk of generated JS to this source node.

  • chunk: A string snippet of generated JS code, another instance of SourceNode, or an array where each member is one of those things.


Walk over the tree of JS snippets in this node and its children. The walking function is called once for each snippet of JS and is passed that snippet and the its original associated source's line/column location.

  • fn: The traversal function.


Like Array.prototype.join except for SourceNodes. Inserts the separator between each of this source node's children.

  • sep: The separator.

SourceNode.prototype.replaceRight(pattern, replacement)

Call String.prototype.replace on the very right-most source snippet. Useful for trimming whitespace from the end of a source node, etc.

  • pattern: The pattern to replace.

  • replacement: The thing to replace the pattern with.


Return the string representation of this source node. Walks over the tree and concatenates all the various snippets together to one string.


Returns the string representation of this tree of source nodes, plus a SourceMapGenerator which contains all the mappings between the generated and original sources.

The arguments are the same as those to new SourceMapGenerator.


Install NodeJS version 0.4.0 or greater, then run node test/run-tests.js.

To add new tests, create a new file named test/test-<your new test name>.js and export your test functions with names that start with "test", for example

exports["test doing the foo bar"] = function () {

The new test will be located automatically when you run the suite.