react / wstein / node_modules / browserify / node_modules / insert-module-globals / 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 = /^[ \t]*(?:\/\/|\/\*)[@#][ \t]+sourceMappingURL=data:(?:application|text)\/json;base64,(.+)(?:\*\/)?/mg;5var mapFileCommentRx =6// //# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */7/(?:^[ \t]*\/\/[@|#][ \t]+sourceMappingURL=(.+?)[ \t]*$)|(?:^[ \t]*\/\*[@#][ \t]+sourceMappingURL=(.+?)[ \t]*\*\/[ \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 || {};36try {37if (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} catch(e) {44console.error(e);45return null;46}47}4849Converter.prototype.toJSON = function (space) {50return JSON.stringify(this.sourcemap, null, space);51};5253Converter.prototype.toBase64 = function () {54var json = this.toJSON();55return new Buffer(json).toString('base64');56};5758Converter.prototype.toComment = function () {59var base64 = this.toBase64();60return '//# sourceMappingURL=data:application/json;base64,' + base64;61};6263// returns copy instead of original64Converter.prototype.toObject = function () {65return JSON.parse(this.toJSON());66};6768Converter.prototype.addProperty = function (key, value) {69if (this.sourcemap.hasOwnProperty(key)) throw new Error('property %s already exists on the sourcemap, use set property instead');70return this.setProperty(key, value);71};7273Converter.prototype.setProperty = function (key, value) {74this.sourcemap[key] = value;75return this;76};7778Converter.prototype.getProperty = function (key) {79return this.sourcemap[key];80};8182exports.fromObject = function (obj) {83return new Converter(obj);84};8586exports.fromJSON = function (json) {87return new Converter(json, { isJSON: true });88};8990exports.fromBase64 = function (base64) {91return new Converter(base64, { isEncoded: true });92};9394exports.fromComment = function (comment) {95comment = comment96.replace(/^\/\*/g, '//')97.replace(/\*\/$/g, '');9899return new Converter(comment, { isEncoded: true, hasComment: true });100};101102exports.fromMapFileComment = function (comment, dir) {103return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });104};105106// Finds last sourcemap comment in file or returns null if none was found107exports.fromSource = function (content) {108var m = content.match(commentRx);109commentRx.lastIndex = 0;110return m ? exports.fromComment(m.pop()) : null;111};112113// Finds last sourcemap comment in file or returns null if none was found114exports.fromMapFileSource = function (content, dir) {115var m = content.match(mapFileCommentRx);116mapFileCommentRx.lastIndex = 0;117return m ? exports.fromMapFileComment(m.pop(), dir) : null;118};119120exports.removeComments = function (src) {121commentRx.lastIndex = 0;122return src.replace(commentRx, '');123};124125exports.removeMapFileComments = function (src) {126mapFileCommentRx.lastIndex = 0;127return src.replace(mapFileCommentRx, '');128};129130exports.__defineGetter__('commentRegex', function () {131commentRx.lastIndex = 0;132return commentRx;133});134135exports.__defineGetter__('mapFileCommentRegex', function () {136mapFileCommentRx.lastIndex = 0;137return mapFileCommentRx;138});139140141