previous-map.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. exports.__esModule = true;
  3. 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; };
  4. var _jsBase = require('js-base64');
  5. var _sourceMap = require('source-map');
  6. var _sourceMap2 = _interopRequireDefault(_sourceMap);
  7. var _path = require('path');
  8. var _path2 = _interopRequireDefault(_path);
  9. var _fs = require('fs');
  10. var _fs2 = _interopRequireDefault(_fs);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  13. var PreviousMap = function () {
  14. function PreviousMap(css, opts) {
  15. _classCallCheck(this, PreviousMap);
  16. this.loadAnnotation(css);
  17. this.inline = this.startWith(this.annotation, 'data:');
  18. var prev = opts.map ? opts.map.prev : undefined;
  19. var text = this.loadMap(opts.from, prev);
  20. if (text) this.text = text;
  21. }
  22. PreviousMap.prototype.consumer = function consumer() {
  23. if (!this.consumerCache) {
  24. this.consumerCache = new _sourceMap2.default.SourceMapConsumer(this.text);
  25. }
  26. return this.consumerCache;
  27. };
  28. PreviousMap.prototype.withContent = function withContent() {
  29. return !!(this.consumer().sourcesContent && this.consumer().sourcesContent.length > 0);
  30. };
  31. PreviousMap.prototype.startWith = function startWith(string, start) {
  32. if (!string) return false;
  33. return string.substr(0, start.length) === start;
  34. };
  35. PreviousMap.prototype.loadAnnotation = function loadAnnotation(css) {
  36. var match = css.match(/\/\*\s*# sourceMappingURL=(.*)\s*\*\//);
  37. if (match) this.annotation = match[1].trim();
  38. };
  39. PreviousMap.prototype.decodeInline = function decodeInline(text) {
  40. var uri = 'data:application/json,';
  41. var base64 = 'data:application/json;base64,';
  42. if (this.startWith(text, uri)) {
  43. return decodeURIComponent(text.substr(uri.length));
  44. } else if (this.startWith(text, base64)) {
  45. return _jsBase.Base64.decode(text.substr(base64.length));
  46. } else {
  47. var encoding = text.match(/data:application\/json;([^,]+),/)[1];
  48. throw new Error('Unsupported source map encoding ' + encoding);
  49. }
  50. };
  51. PreviousMap.prototype.loadMap = function loadMap(file, prev) {
  52. if (prev === false) return false;
  53. if (prev) {
  54. if (typeof prev === 'string') {
  55. return prev;
  56. } else if (prev instanceof _sourceMap2.default.SourceMapConsumer) {
  57. return _sourceMap2.default.SourceMapGenerator.fromSourceMap(prev).toString();
  58. } else if (prev instanceof _sourceMap2.default.SourceMapGenerator) {
  59. return prev.toString();
  60. } else if ((typeof prev === 'undefined' ? 'undefined' : _typeof(prev)) === 'object' && prev.mappings) {
  61. return JSON.stringify(prev);
  62. } else {
  63. throw new Error('Unsupported previous source map format: ' + prev.toString());
  64. }
  65. } else if (this.inline) {
  66. return this.decodeInline(this.annotation);
  67. } else if (this.annotation) {
  68. var map = this.annotation;
  69. if (file) map = _path2.default.join(_path2.default.dirname(file), map);
  70. this.root = _path2.default.dirname(map);
  71. if (_fs2.default.existsSync && _fs2.default.existsSync(map)) {
  72. return _fs2.default.readFileSync(map, 'utf-8').toString().trim();
  73. } else {
  74. return false;
  75. }
  76. }
  77. };
  78. return PreviousMap;
  79. }();
  80. exports.default = PreviousMap;