react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / index.js
80537 views'use strict';12var convert = require('convert-source-map');3var createGenerator = require('inline-source-map');4var mappingsFromMap = require('./lib/mappings-from-map');56function resolveMap(source) {7var gen = convert.fromSource(source);8return gen ? gen.toObject() : null;9}1011function hasInlinedSource(existingMap) {12return existingMap.sourcesContent && !!existingMap.sourcesContent[0];13}1415function Combiner(file, sourceRoot) {16// since we include the original code in the map sourceRoot actually not needed17this.generator = createGenerator({ file: file || 'generated.js', sourceRoot: sourceRoot });18}1920Combiner.prototype._addGeneratedMap = function (sourceFile, source, offset) {21this.generator.addGeneratedMappings(sourceFile, source, offset);22this.generator.addSourceContent(sourceFile, source);23return this;24};2526Combiner.prototype._addExistingMap = function (sourceFile, source, existingMap, offset) {27var mappings = mappingsFromMap(existingMap);2829var originalSource = existingMap.sourcesContent[0]30, originalSourceFile = existingMap.sources[0];3132this.generator.addMappings(originalSourceFile || sourceFile, mappings, offset);33this.generator.addSourceContent(originalSourceFile || sourceFile, originalSource);34return this;35};3637/**38* Adds map to underlying source map.39* If source contains a source map comment that has the source of the original file inlined it will offset these40* mappings and include them.41* If no source map comment is found or it has no source inlined, mappings for the file will be generated and included42*43* @name addMap44* @function45* @param opts {Object} { sourceFile: {String}, source: {String} }46* @param offset {Object} { line: {Number}, column: {Number} }47*/48Combiner.prototype.addFile = function (opts, offset) {4950offset = offset || {};51if (!offset.hasOwnProperty('line')) offset.line = 0;52if (!offset.hasOwnProperty('column')) offset.column = 0;5354var existingMap = resolveMap(opts.source);5556return existingMap && hasInlinedSource(existingMap)57? this._addExistingMap(opts.sourceFile, opts.source, existingMap, offset)58: this._addGeneratedMap(opts.sourceFile, opts.source, offset);59};6061/**62* @name base6463* @function64* @return {String} base64 encoded combined source map65*/66Combiner.prototype.base64 = function () {67return this.generator.base64Encode();68};6970/**71* @name comment72* @function73* @return {String} base64 encoded sourceMappingUrl comment of the combined source map74*/75Combiner.prototype.comment = function () {76return this.generator.inlineMappingUrl();77};7879/**80* @name create81* @function82* @param file {String} optional name of the generated file83* @param sourceRoot { String} optional sourceRoot of the map to be generated84* @return {Object} Combiner instance to which source maps can be added and later combined85*/86exports.create = function (file, sourceRoot) { return new Combiner(file, sourceRoot); };8788/**89* @name removeComments90* @function91* @param src92* @return {String} src with all sourceMappingUrl comments removed93*/94exports.removeComments = function (src) {95if (!src.replace) return src;96return src.replace(convert.commentRegex, '');97};9899100