index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. var fs = require('fs');
  3. var path = require('path');
  4. Object.defineProperty(exports, 'commentRegex', {
  5. get: function getCommentRegex () {
  6. return /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/mg;
  7. }
  8. });
  9. Object.defineProperty(exports, 'mapFileCommentRegex', {
  10. get: function getMapFileCommentRegex () {
  11. // Matches sourceMappingURL in either // or /* comment styles.
  12. return /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"`]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \t]*$)/mg;
  13. }
  14. });
  15. var decodeBase64;
  16. if (typeof Buffer !== 'undefined') {
  17. if (typeof Buffer.from === 'function') {
  18. decodeBase64 = decodeBase64WithBufferFrom;
  19. } else {
  20. decodeBase64 = decodeBase64WithNewBuffer;
  21. }
  22. } else {
  23. decodeBase64 = decodeBase64WithAtob;
  24. }
  25. function decodeBase64WithBufferFrom(base64) {
  26. return Buffer.from(base64, 'base64').toString();
  27. }
  28. function decodeBase64WithNewBuffer(base64) {
  29. if (typeof value === 'number') {
  30. throw new TypeError('The value to decode must not be of type number.');
  31. }
  32. return new Buffer(base64, 'base64').toString();
  33. }
  34. function decodeBase64WithAtob(base64) {
  35. return decodeURIComponent(escape(atob(base64)));
  36. }
  37. function stripComment(sm) {
  38. return sm.split(',').pop();
  39. }
  40. function readFromFileMap(sm, dir) {
  41. // NOTE: this will only work on the server since it attempts to read the map file
  42. var r = exports.mapFileCommentRegex.exec(sm);
  43. // for some odd reason //# .. captures in 1 and /* .. */ in 2
  44. var filename = r[1] || r[2];
  45. var filepath = path.resolve(dir, filename);
  46. try {
  47. return fs.readFileSync(filepath, 'utf8');
  48. } catch (e) {
  49. throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e);
  50. }
  51. }
  52. function Converter (sm, opts) {
  53. opts = opts || {};
  54. if (opts.isFileComment) sm = readFromFileMap(sm, opts.commentFileDir);
  55. if (opts.hasComment) sm = stripComment(sm);
  56. if (opts.isEncoded) sm = decodeBase64(sm);
  57. if (opts.isJSON || opts.isEncoded) sm = JSON.parse(sm);
  58. this.sourcemap = sm;
  59. }
  60. Converter.prototype.toJSON = function (space) {
  61. return JSON.stringify(this.sourcemap, null, space);
  62. };
  63. if (typeof Buffer !== 'undefined') {
  64. if (typeof Buffer.from === 'function') {
  65. Converter.prototype.toBase64 = encodeBase64WithBufferFrom;
  66. } else {
  67. Converter.prototype.toBase64 = encodeBase64WithNewBuffer;
  68. }
  69. } else {
  70. Converter.prototype.toBase64 = encodeBase64WithBtoa;
  71. }
  72. function encodeBase64WithBufferFrom() {
  73. var json = this.toJSON();
  74. return Buffer.from(json, 'utf8').toString('base64');
  75. }
  76. function encodeBase64WithNewBuffer() {
  77. var json = this.toJSON();
  78. if (typeof json === 'number') {
  79. throw new TypeError('The json to encode must not be of type number.');
  80. }
  81. return new Buffer(json, 'utf8').toString('base64');
  82. }
  83. function encodeBase64WithBtoa() {
  84. var json = this.toJSON();
  85. return btoa(unescape(encodeURIComponent(json)));
  86. }
  87. Converter.prototype.toComment = function (options) {
  88. var base64 = this.toBase64();
  89. var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
  90. return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  91. };
  92. // returns copy instead of original
  93. Converter.prototype.toObject = function () {
  94. return JSON.parse(this.toJSON());
  95. };
  96. Converter.prototype.addProperty = function (key, value) {
  97. if (this.sourcemap.hasOwnProperty(key)) throw new Error('property "' + key + '" already exists on the sourcemap, use set property instead');
  98. return this.setProperty(key, value);
  99. };
  100. Converter.prototype.setProperty = function (key, value) {
  101. this.sourcemap[key] = value;
  102. return this;
  103. };
  104. Converter.prototype.getProperty = function (key) {
  105. return this.sourcemap[key];
  106. };
  107. exports.fromObject = function (obj) {
  108. return new Converter(obj);
  109. };
  110. exports.fromJSON = function (json) {
  111. return new Converter(json, { isJSON: true });
  112. };
  113. exports.fromBase64 = function (base64) {
  114. return new Converter(base64, { isEncoded: true });
  115. };
  116. exports.fromComment = function (comment) {
  117. comment = comment
  118. .replace(/^\/\*/g, '//')
  119. .replace(/\*\/$/g, '');
  120. return new Converter(comment, { isEncoded: true, hasComment: true });
  121. };
  122. exports.fromMapFileComment = function (comment, dir) {
  123. return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });
  124. };
  125. // Finds last sourcemap comment in file or returns null if none was found
  126. exports.fromSource = function (content) {
  127. var m = content.match(exports.commentRegex);
  128. return m ? exports.fromComment(m.pop()) : null;
  129. };
  130. // Finds last sourcemap comment in file or returns null if none was found
  131. exports.fromMapFileSource = function (content, dir) {
  132. var m = content.match(exports.mapFileCommentRegex);
  133. return m ? exports.fromMapFileComment(m.pop(), dir) : null;
  134. };
  135. exports.removeComments = function (src) {
  136. return src.replace(exports.commentRegex, '');
  137. };
  138. exports.removeMapFileComments = function (src) {
  139. return src.replace(exports.mapFileCommentRegex, '');
  140. };
  141. exports.generateMapFileComment = function (file, options) {
  142. var data = 'sourceMappingURL=' + file;
  143. return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  144. };