Blame view

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