previous-map.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  6. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  7. var _jsBase = require('js-base64');
  8. var _sourceMap = require('source-map');
  9. var _sourceMap2 = _interopRequireDefault(_sourceMap);
  10. var _path = require('path');
  11. var _path2 = _interopRequireDefault(_path);
  12. var _fs = require('fs');
  13. var _fs2 = _interopRequireDefault(_fs);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  16. var PreviousMap = function () {
  17. function PreviousMap(css, opts) {
  18. _classCallCheck(this, PreviousMap);
  19. this.loadAnnotation(css);
  20. this.inline = this.startWith(this.annotation, 'data:');
  21. var prev = opts.map ? opts.map.prev : undefined;
  22. var text = this.loadMap(opts.from, prev);
  23. if (text) this.text = text;
  24. }
  25. _createClass(PreviousMap, [{
  26. key: 'consumer',
  27. value: function consumer() {
  28. if (!this.consumerCache) {
  29. this.consumerCache = new _sourceMap2.default.SourceMapConsumer(this.text);
  30. }
  31. return this.consumerCache;
  32. }
  33. }, {
  34. key: 'withContent',
  35. value: function withContent() {
  36. return !!(this.consumer().sourcesContent && this.consumer().sourcesContent.length > 0);
  37. }
  38. }, {
  39. key: 'startWith',
  40. value: function startWith(string, start) {
  41. if (!string) return false;
  42. return string.substr(0, start.length) === start;
  43. }
  44. }, {
  45. key: 'loadAnnotation',
  46. value: function loadAnnotation(css) {
  47. var match = css.match(/\/\*\s*# sourceMappingURL=((?:(?!sourceMappingURL=).)*)\s*\*\//);
  48. if (match) this.annotation = match[1].trim();
  49. }
  50. }, {
  51. key: 'decodeInline',
  52. value: function decodeInline(text) {
  53. var utfd64 = 'data:application/json;charset=utf-8;base64,';
  54. var utf64 = 'data:application/json;charset=utf8;base64,';
  55. var b64 = 'data:application/json;base64,';
  56. var uri = 'data:application/json,';
  57. if (this.startWith(text, uri)) {
  58. return decodeURIComponent(text.substr(uri.length));
  59. } else if (this.startWith(text, base64)) {
  60. return _jsBase.Base64.decode(text.substr(base64.length));
  61. } else if (this.startWith(text, utf64)) {
  62. return _jsBase.Base64.decode(text.substr(utf64.length));
  63. } else if (this.startWith(text, utfd64)) {
  64. return _jsBase.Base64.decode(text.substr(utfd64.length));
  65. } else {
  66. var encoding = text.match(/data:application\/json;([^,]+),/)[1];
  67. throw new Error('Unsupported source map encoding ' + encoding);
  68. }
  69. }
  70. }, {
  71. key: 'loadMap',
  72. value: function loadMap(file, prev) {
  73. if (prev === false) return false;
  74. if (prev) {
  75. if (typeof prev === 'string') {
  76. return prev;
  77. } else if (prev instanceof _sourceMap2.default.SourceMapConsumer) {
  78. return _sourceMap2.default.SourceMapGenerator.fromSourceMap(prev).toString();
  79. } else if (prev instanceof _sourceMap2.default.SourceMapGenerator) {
  80. return prev.toString();
  81. } else if ((typeof prev === 'undefined' ? 'undefined' : _typeof(prev)) === 'object' && prev.mappings) {
  82. return JSON.stringify(prev);
  83. } else {
  84. throw new Error('Unsupported previous source map format: ' + prev.toString());
  85. }
  86. } else if (this.inline) {
  87. return this.decodeInline(this.annotation);
  88. } else if (this.annotation) {
  89. var map = this.annotation;
  90. if (file) map = _path2.default.join(_path2.default.dirname(file), map);
  91. this.root = _path2.default.dirname(map);
  92. if (_fs2.default.existsSync && _fs2.default.existsSync(map)) {
  93. return _fs2.default.readFileSync(map, 'utf-8').toString().trim();
  94. } else {
  95. return false;
  96. }
  97. }
  98. }
  99. }]);
  100. return PreviousMap;
  101. }();
  102. exports.default = PreviousMap;