react / wstein / node_modules / browserify / node_modules / browser-pack / node_modules / combine-source-map / node_modules / convert-source-map / index.js
80552 views'use strict';1var fs = require('fs');2var path = require('path');34var commentRx = /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+;)?base64,(.*)$/mg;5var mapFileCommentRx =6// //# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */7/(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \t]*$)/mg89function decodeBase64(base64) {10return new Buffer(base64, 'base64').toString();11}1213function stripComment(sm) {14return sm.split(',').pop();15}1617function readFromFileMap(sm, dir) {18// NOTE: this will only work on the server since it attempts to read the map file1920var r = mapFileCommentRx.exec(sm);21mapFileCommentRx.lastIndex = 0;2223// for some odd reason //# .. captures in 1 and /* .. */ in 224var filename = r[1] || r[2];25var filepath = path.join(dir, filename);2627try {28return fs.readFileSync(filepath, 'utf8');29} catch (e) {30throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e);31}32}3334function Converter (sm, opts) {35opts = opts || {};3637if (opts.isFileComment) sm = readFromFileMap(sm, opts.commentFileDir);38if (opts.hasComment) sm = stripComment(sm);39if (opts.isEncoded) sm = decodeBase64(sm);40if (opts.isJSON || opts.isEncoded) sm = JSON.parse(sm);4142this.sourcemap = sm;43}4445function convertFromLargeSource(content){46var lines = content.split('\n');47var line;48// find first line which contains a source map starting at end of content49for (var i = lines.length - 1; i > 0; i--) {50line = lines[i]51if (~line.indexOf('sourceMappingURL=data:')) return exports.fromComment(line);52}53}5455Converter.prototype.toJSON = function (space) {56return JSON.stringify(this.sourcemap, null, space);57};5859Converter.prototype.toBase64 = function () {60var json = this.toJSON();61return new Buffer(json).toString('base64');62};6364Converter.prototype.toComment = function (options) {65var base64 = this.toBase64();66var data = 'sourceMappingURL=data:application/json;base64,' + base64;67return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;68};6970// returns copy instead of original71Converter.prototype.toObject = function () {72return JSON.parse(this.toJSON());73};7475Converter.prototype.addProperty = function (key, value) {76if (this.sourcemap.hasOwnProperty(key)) throw new Error('property %s already exists on the sourcemap, use set property instead');77return this.setProperty(key, value);78};7980Converter.prototype.setProperty = function (key, value) {81this.sourcemap[key] = value;82return this;83};8485Converter.prototype.getProperty = function (key) {86return this.sourcemap[key];87};8889exports.fromObject = function (obj) {90return new Converter(obj);91};9293exports.fromJSON = function (json) {94return new Converter(json, { isJSON: true });95};9697exports.fromBase64 = function (base64) {98return new Converter(base64, { isEncoded: true });99};100101exports.fromComment = function (comment) {102comment = comment103.replace(/^\/\*/g, '//')104.replace(/\*\/$/g, '');105106return new Converter(comment, { isEncoded: true, hasComment: true });107};108109exports.fromMapFileComment = function (comment, dir) {110return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });111};112113// Finds last sourcemap comment in file or returns null if none was found114exports.fromSource = function (content, largeSource) {115if (largeSource) return convertFromLargeSource(content);116117var m = content.match(commentRx);118commentRx.lastIndex = 0;119return m ? exports.fromComment(m.pop()) : null;120};121122// Finds last sourcemap comment in file or returns null if none was found123exports.fromMapFileSource = function (content, dir) {124var m = content.match(mapFileCommentRx);125mapFileCommentRx.lastIndex = 0;126return m ? exports.fromMapFileComment(m.pop(), dir) : null;127};128129exports.removeComments = function (src) {130commentRx.lastIndex = 0;131return src.replace(commentRx, '');132};133134exports.removeMapFileComments = function (src) {135mapFileCommentRx.lastIndex = 0;136return src.replace(mapFileCommentRx, '');137};138139Object.defineProperty(exports, 'commentRegex', {140get: function getCommentRegex () {141commentRx.lastIndex = 0;142return commentRx;143}144});145146Object.defineProperty(exports, 'mapFileCommentRegex', {147get: function getMapFileCommentRegex () {148mapFileCommentRx.lastIndex = 0;149return mapFileCommentRx;150}151});152153154