index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. var fs = require('fs');
  3. var path = require('path');
  4. var commentRx = /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+;)?base64,(.*)$/mg;
  5. var mapFileCommentRx =
  6. //Example (Extra space between slashes added to solve Safari bug. Exclude space in production):
  7. // / /# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */
  8. /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \t]*$)/mg
  9. function decodeBase64(base64) {
  10. return new Buffer(base64, 'base64').toString();
  11. }
  12. function stripComment(sm) {
  13. return sm.split(',').pop();
  14. }
  15. function readFromFileMap(sm, dir) {
  16. // NOTE: this will only work on the server since it attempts to read the map file
  17. var r = mapFileCommentRx.exec(sm);
  18. mapFileCommentRx.lastIndex = 0;
  19. // for some odd reason //# .. captures in 1 and /* .. */ in 2
  20. var filename = r[1] || r[2];
  21. var filepath = path.resolve(dir, filename);
  22. try {
  23. return fs.readFileSync(filepath, 'utf8');
  24. } catch (e) {
  25. throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e);
  26. }
  27. }
  28. function Converter (sm, opts) {
  29. opts = opts || {};
  30. if (opts.isFileComment) sm = readFromFileMap(sm, opts.commentFileDir);
  31. if (opts.hasComment) sm = stripComment(sm);
  32. if (opts.isEncoded) sm = decodeBase64(sm);
  33. if (opts.isJSON || opts.isEncoded) sm = JSON.parse(sm);
  34. this.sourcemap = sm;
  35. }
  36. function convertFromLargeSource(content){
  37. var lines = content.split('\n');
  38. var line;
  39. // find first line which contains a source map starting at end of content
  40. for (var i = lines.length - 1; i > 0; i--) {
  41. line = lines[i]
  42. if (~line.indexOf('sourceMappingURL=data:')) return exports.fromComment(line);
  43. }
  44. }
  45. Converter.prototype.toJSON = function (space) {
  46. return JSON.stringify(this.sourcemap, null, space);
  47. };
  48. Converter.prototype.toBase64 = function () {
  49. var json = this.toJSON();
  50. return new Buffer(json).toString('base64');
  51. };
  52. Converter.prototype.toComment = function (options) {
  53. var base64 = this.toBase64();
  54. var data = 'sourceMappingURL=data:application/json;base64,' + base64;
  55. return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  56. };
  57. // returns copy instead of original
  58. Converter.prototype.toObject = function () {
  59. return JSON.parse(this.toJSON());
  60. };
  61. Converter.prototype.addProperty = function (key, value) {
  62. if (this.sourcemap.hasOwnProperty(key)) throw new Error('property %s already exists on the sourcemap, use set property instead');
  63. return this.setProperty(key, value);
  64. };
  65. Converter.prototype.setProperty = function (key, value) {
  66. this.sourcemap[key] = value;
  67. return this;
  68. };
  69. Converter.prototype.getProperty = function (key) {
  70. return this.sourcemap[key];
  71. };
  72. exports.fromObject = function (obj) {
  73. return new Converter(obj);
  74. };
  75. exports.fromJSON = function (json) {
  76. return new Converter(json, { isJSON: true });
  77. };
  78. exports.fromBase64 = function (base64) {
  79. return new Converter(base64, { isEncoded: true });
  80. };
  81. exports.fromComment = function (comment) {
  82. comment = comment
  83. .replace(/^\/\*/g, '//')
  84. .replace(/\*\/$/g, '');
  85. return new Converter(comment, { isEncoded: true, hasComment: true });
  86. };
  87. exports.fromMapFileComment = function (comment, dir) {
  88. return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });
  89. };
  90. // Finds last sourcemap comment in file or returns null if none was found
  91. exports.fromSource = function (content, largeSource) {
  92. if (largeSource) {
  93. var res = convertFromLargeSource(content);
  94. return res ? res : null;
  95. }
  96. var m = content.match(commentRx);
  97. commentRx.lastIndex = 0;
  98. return m ? exports.fromComment(m.pop()) : null;
  99. };
  100. // Finds last sourcemap comment in file or returns null if none was found
  101. exports.fromMapFileSource = function (content, dir) {
  102. var m = content.match(mapFileCommentRx);
  103. mapFileCommentRx.lastIndex = 0;
  104. return m ? exports.fromMapFileComment(m.pop(), dir) : null;
  105. };
  106. exports.removeComments = function (src) {
  107. commentRx.lastIndex = 0;
  108. return src.replace(commentRx, '');
  109. };
  110. exports.removeMapFileComments = function (src) {
  111. mapFileCommentRx.lastIndex = 0;
  112. return src.replace(mapFileCommentRx, '');
  113. };
  114. exports.generateMapFileComment = function (file, options) {
  115. var data = 'sourceMappingURL=' + file;
  116. return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  117. };
  118. Object.defineProperty(exports, 'commentRegex', {
  119. get: function getCommentRegex () {
  120. commentRx.lastIndex = 0;
  121. return commentRx;
  122. }
  123. });
  124. Object.defineProperty(exports, 'mapFileCommentRegex', {
  125. get: function getMapFileCommentRegex () {
  126. mapFileCommentRx.lastIndex = 0;
  127. return mapFileCommentRx;
  128. }
  129. });