react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / node_modules / inline-source-map / index.js
80552 views'use strict';1var SourceMapGenerator = require('source-map').SourceMapGenerator;23function offsetMapping(mapping, offset) {4return { line: offset.line + mapping.line, column: offset.column + mapping.column };5}67function newlinesIn(src) {8if (!src) return 0;9var newlines = src.match(/\n/g);1011return newlines ? newlines.length : 0;12}1314function Generator(opts) {15opts = opts || {};16this.generator = new SourceMapGenerator({ file: opts.file || '', sourceRoot: opts.sourceRoot || '' });17this.sourcesContent = undefined;18}1920/**21* Adds the given mappings to the generator and offsets them if offset is given22*23* @name addMappings24* @function25* @param sourceFile {String} name of the source file26* @param mappings {Array{{Object}} each object has the form { original: { line: _, column: _ }, generated: { line: _, column: _ } }27* @param offset {Object} offset to apply to each mapping. Has the form { line: _, column: _ }28* @return {Object} the generator to allow chaining29*/30Generator.prototype.addMappings = function (sourceFile, mappings, offset) {31var generator = this.generator;3233offset = offset || {};34offset.line = offset.hasOwnProperty('line') ? offset.line : 0;35offset.column = offset.hasOwnProperty('column') ? offset.column : 0;3637mappings.forEach(function (m) {38// only set source if we have original position to handle edgecase (see inline-source-map tests)39generator.addMapping({40source : m.original ? sourceFile : undefined41, original : m.original42, generated : offsetMapping(m.generated, offset)43});44});45return this;46};4748/**49* Generates mappings for the given source, assuming that no translation from original to generated is necessary.50*51* @name addGeneratedMappings52* @function53* @param sourceFile {String} name of the source file54* @param source {String} source of the file55* @param offset {Object} offset to apply to each mapping. Has the form { line: _, column: _ }56* @return {Object} the generator to allow chaining57*/58Generator.prototype.addGeneratedMappings = function (sourceFile, source, offset) {59var mappings = []60, linesToGenerate = newlinesIn(source) + 1;6162for (var line = 1; line <= linesToGenerate; line++) {63var location = { line: line, column: 0 };64mappings.push({ original: location, generated: location });65}6667return this.addMappings(sourceFile, mappings, offset);68};6970/**71* Adds source content for the given source file.72*73* @name addSourceContent74* @function75* @param sourceFile {String} The source file for which a mapping is included76* @param sourcesContent {String} The content of the source file77* @return {Object} The generator to allow chaining78*/79Generator.prototype.addSourceContent = function (sourceFile, sourcesContent) {80this.sourcesContent = this.sourcesContent || {};81this.sourcesContent[sourceFile] = sourcesContent;82return this;83};8485/**86* @name base64Encode87* @function88* @return {String} bas64 encoded representation of the added mappings89*/90Generator.prototype.base64Encode = function () {91var map = this.toString();92return new Buffer(map).toString('base64');93};9495/**96* @name inlineMappingUrl97* @function98* @return {String} comment with base64 encoded representation of the added mappings. Can be inlined at the end of the generated file.99*/100Generator.prototype.inlineMappingUrl = function () {101return '//# sourceMappingURL=data:application/json;base64,' + this.base64Encode();102};103104Generator.prototype.toJSON = function () {105var map = this.generator.toJSON();106if (!this.sourcesContent) return map;107108var toSourcesContent = (function (s) { return this.sourcesContent[s] || null; }).bind(this);109map.sourcesContent = map.sources.map(toSourcesContent);110return map;111};112113Generator.prototype.toString = function () {114return JSON.stringify(this);115};116117Generator.prototype._mappings = function () {118return this.generator._mappings._array;119};120121Generator.prototype.gen = function () {122return this.generator;123};124125module.exports = function (opts) { return new Generator(opts); };126module.exports.Generator = Generator;127128129