react / wstein / node_modules / browserify / node_modules / browser-pack / 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;18this.opts = opts;19}2021/**22* Adds the given mappings to the generator and offsets them if offset is given23*24* @name addMappings25* @function26* @param sourceFile {String} name of the source file27* @param mappings {Array{{Object}} each object has the form { original: { line: _, column: _ }, generated: { line: _, column: _ } }28* @param offset {Object} offset to apply to each mapping. Has the form { line: _, column: _ }29* @return {Object} the generator to allow chaining30*/31Generator.prototype.addMappings = function (sourceFile, mappings, offset) {32var generator = this.generator;3334offset = offset || {};35offset.line = offset.hasOwnProperty('line') ? offset.line : 0;36offset.column = offset.hasOwnProperty('column') ? offset.column : 0;3738mappings.forEach(function (m) {39// only set source if we have original position to handle edgecase (see inline-source-map tests)40generator.addMapping({41source : m.original ? sourceFile : undefined42, original : m.original43, generated : offsetMapping(m.generated, offset)44});45});46return this;47};4849/**50* Generates mappings for the given source, assuming that no translation from original to generated is necessary.51*52* @name addGeneratedMappings53* @function54* @param sourceFile {String} name of the source file55* @param source {String} source of the file56* @param offset {Object} offset to apply to each mapping. Has the form { line: _, column: _ }57* @return {Object} the generator to allow chaining58*/59Generator.prototype.addGeneratedMappings = function (sourceFile, source, offset) {60var mappings = []61, linesToGenerate = newlinesIn(source) + 1;6263for (var line = 1; line <= linesToGenerate; line++) {64var location = { line: line, column: 0 };65mappings.push({ original: location, generated: location });66}6768return this.addMappings(sourceFile, mappings, offset);69};7071/**72* Adds source content for the given source file.73*74* @name addSourceContent75* @function76* @param sourceFile {String} The source file for which a mapping is included77* @param sourcesContent {String} The content of the source file78* @return {Object} The generator to allow chaining79*/80Generator.prototype.addSourceContent = function (sourceFile, sourcesContent) {81this.sourcesContent = this.sourcesContent || {};82this.sourcesContent[sourceFile] = sourcesContent;83return this;84};8586/**87* @name base64Encode88* @function89* @return {String} bas64 encoded representation of the added mappings90*/91Generator.prototype.base64Encode = function () {92var map = this.toString();93return new Buffer(map).toString('base64');94};9596/**97* @name inlineMappingUrl98* @function99* @return {String} comment with base64 encoded representation of the added mappings. Can be inlined at the end of the generated file.100*/101Generator.prototype.inlineMappingUrl = function () {102var charset = this.opts.charset || 'utf-8';103return '//# sourceMappingURL=data:application/json;charset:' + charset + ';base64,' + this.base64Encode();104};105106Generator.prototype.toJSON = function () {107var map = this.generator.toJSON();108if (!this.sourcesContent) return map;109110var toSourcesContent = (function (s) { return this.sourcesContent[s] || null; }).bind(this);111map.sourcesContent = map.sources.map(toSourcesContent);112return map;113};114115Generator.prototype.toString = function () {116return JSON.stringify(this);117};118119Generator.prototype._mappings = function () {120return this.generator._mappings._array;121};122123Generator.prototype.gen = function () {124return this.generator;125};126127module.exports = function (opts) { return new Generator(opts); };128module.exports.Generator = Generator;129130131