previous-map.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /**
  14. * Source map information from input CSS.
  15. * For example, source map after Sass compiler.
  16. *
  17. * This class will automatically find source map in input CSS or in file system
  18. * near input file (according `from` option).
  19. *
  20. * @example
  21. * const root = postcss.parse(css, { from: 'a.sass.css' });
  22. * root.input.map //=> PreviousMap
  23. */
  24. var PreviousMap = function () {
  25. /**
  26. * @param {string} css - input CSS source
  27. * @param {processOptions} [opts] - {@link Processor#process} options
  28. */
  29. function PreviousMap(css, opts) {
  30. _classCallCheck(this, PreviousMap);
  31. this.loadAnnotation(css);
  32. /**
  33. * @member {boolean} - Was source map inlined by data-uri to input CSS.
  34. */
  35. this.inline = this.startWith(this.annotation, 'data:');
  36. var prev = opts.map ? opts.map.prev : undefined;
  37. var text = this.loadMap(opts.from, prev);
  38. if (text) this.text = text;
  39. }
  40. /**
  41. * Create a instance of `SourceMapGenerator` class
  42. * from the `source-map` library to work with source map information.
  43. *
  44. * It is lazy method, so it will create object only on first call
  45. * and then it will use cache.
  46. *
  47. * @return {SourceMapGenerator} object with source map information
  48. */
  49. PreviousMap.prototype.consumer = function consumer() {
  50. if (!this.consumerCache) {
  51. this.consumerCache = new _sourceMap2.default.SourceMapConsumer(this.text);
  52. }
  53. return this.consumerCache;
  54. };
  55. /**
  56. * Does source map contains `sourcesContent` with input source text.
  57. *
  58. * @return {boolean} Is `sourcesContent` present
  59. */
  60. PreviousMap.prototype.withContent = function withContent() {
  61. return !!(this.consumer().sourcesContent && this.consumer().sourcesContent.length > 0);
  62. };
  63. PreviousMap.prototype.startWith = function startWith(string, start) {
  64. if (!string) return false;
  65. return string.substr(0, start.length) === start;
  66. };
  67. PreviousMap.prototype.loadAnnotation = function loadAnnotation(css) {
  68. var match = css.match(/\/\*\s*# sourceMappingURL=(.*)\s*\*\//);
  69. if (match) this.annotation = match[1].trim();
  70. };
  71. PreviousMap.prototype.decodeInline = function decodeInline(text) {
  72. var utfd64 = 'data:application/json;charset=utf-8;base64,';
  73. var utf64 = 'data:application/json;charset=utf8;base64,';
  74. var b64 = 'data:application/json;base64,';
  75. var uri = 'data:application/json,';
  76. if (this.startWith(text, uri)) {
  77. return decodeURIComponent(text.substr(uri.length));
  78. } else if (this.startWith(text, b64)) {
  79. return _jsBase.Base64.decode(text.substr(b64.length));
  80. } else if (this.startWith(text, utf64)) {
  81. return _jsBase.Base64.decode(text.substr(utf64.length));
  82. } else if (this.startWith(text, utfd64)) {
  83. return _jsBase.Base64.decode(text.substr(utfd64.length));
  84. } else {
  85. var encoding = text.match(/data:application\/json;([^,]+),/)[1];
  86. throw new Error('Unsupported source map encoding ' + encoding);
  87. }
  88. };
  89. PreviousMap.prototype.loadMap = function loadMap(file, prev) {
  90. if (prev === false) return false;
  91. if (prev) {
  92. if (typeof prev === 'string') {
  93. return prev;
  94. } else if (typeof prev === 'function') {
  95. var prevPath = prev(file);
  96. if (prevPath && _fs2.default.existsSync && _fs2.default.existsSync(prevPath)) {
  97. return _fs2.default.readFileSync(prevPath, 'utf-8').toString().trim();
  98. } else {
  99. throw new Error('Unable to load previous source map: ' + prevPath.toString());
  100. }
  101. } else if (prev instanceof _sourceMap2.default.SourceMapConsumer) {
  102. return _sourceMap2.default.SourceMapGenerator.fromSourceMap(prev).toString();
  103. } else if (prev instanceof _sourceMap2.default.SourceMapGenerator) {
  104. return prev.toString();
  105. } else if (this.isMap(prev)) {
  106. return JSON.stringify(prev);
  107. } else {
  108. throw new Error('Unsupported previous source map format: ' + prev.toString());
  109. }
  110. } else if (this.inline) {
  111. return this.decodeInline(this.annotation);
  112. } else if (this.annotation) {
  113. var map = this.annotation;
  114. if (file) map = _path2.default.join(_path2.default.dirname(file), map);
  115. this.root = _path2.default.dirname(map);
  116. if (_fs2.default.existsSync && _fs2.default.existsSync(map)) {
  117. return _fs2.default.readFileSync(map, 'utf-8').toString().trim();
  118. } else {
  119. return false;
  120. }
  121. }
  122. };
  123. PreviousMap.prototype.isMap = function isMap(map) {
  124. if ((typeof map === 'undefined' ? 'undefined' : _typeof(map)) !== 'object') return false;
  125. return typeof map.mappings === 'string' || typeof map._mappings === 'string';
  126. };
  127. return PreviousMap;
  128. }();
  129. exports.default = PreviousMap;
  130. module.exports = exports['default'];
  131. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInByZXZpb3VzLW1hcC5lczYiXSwibmFtZXMiOlsiUHJldmlvdXNNYXAiLCJjc3MiLCJvcHRzIiwibG9hZEFubm90YXRpb24iLCJpbmxpbmUiLCJzdGFydFdpdGgiLCJhbm5vdGF0aW9uIiwicHJldiIsIm1hcCIsInVuZGVmaW5lZCIsInRleHQiLCJsb2FkTWFwIiwiZnJvbSIsImNvbnN1bWVyIiwiY29uc3VtZXJDYWNoZSIsIlNvdXJjZU1hcENvbnN1bWVyIiwid2l0aENvbnRlbnQiLCJzb3VyY2VzQ29udGVudCIsImxlbmd0aCIsInN0cmluZyIsInN0YXJ0Iiwic3Vic3RyIiwibWF0Y2giLCJ0cmltIiwi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ICggdGhpcy5zdGFydFdpdGgodGV4dCwgdXRmZDY0KSApIHtcbiAgICAgICAgICAgIHJldHVybiBCYXNlNjQuZGVjb2RlKCB0ZXh0LnN1YnN0cih1dGZkNjQubGVuZ3RoKSApO1xuXG4gICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICBsZXQgZW5jb2RpbmcgPSB0ZXh0Lm1hdGNoKC9kYXRhOmFwcGxpY2F0aW9uXFwvanNvbjsoW14sXSspLC8pWzFdO1xuICAgICAgICAgICAgdGhyb3cgbmV3IEVycm9yKCdVbnN1cHBvcnRlZCBzb3VyY2UgbWFwIGVuY29kaW5nICcgKyBlbmNvZGluZyk7XG4gICAgICAgIH1cbiAgICB9XG5cbiAgICBsb2FkTWFwKGZpbGUsIHByZXYpIHtcbiAgICAgICAgaWYgKCBwcmV2ID09PSBmYWxzZSApIHJldHVybiBmYWxzZTtcblxuICAgICAgICBpZiAoIHByZXYgKSB7XG4gICAgICAgICAgICBpZiAoIHR5cGVvZiBwcmV2ID09PSAnc3RyaW5nJyApIHtcbiAgICAgICAgICAgICAgICByZXR1cm4gcHJldjtcbiAgICAgICAgICAgIH0gZWxzZSBpZiAoIHR5cGVvZiBwcmV2ID09PSAnZnVuY3Rpb24nICkge1xuICAgICAgICAgICAgICAgIGxldCBwcmV2UGF0aCA9IHByZXYoZmlsZSk7XG4gICAgICAgICAgICAgICAgaWYgKCBwcmV2UGF0aCAmJiBmcy5leGlzdHNTeW5jICYmIGZzLmV4aXN0c1N5bmMocHJldlBhdGgpICkge1xuICAgICAgICAgICAgICAgICAgICByZXR1cm4gZnMucmVhZEZpbGVTeW5jKHByZXZQYXRoLCAndXRmLTgnKS50b1N0cmluZygpLnRyaW0oKTtcbiAgICAgICAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IoJ1VuYWJsZSB0byBsb2FkIHByZXZpb3VzIHNvdXJjZSBtYXA6ICcgK1xuICAgICAgICAgICAgICAgICAgICBwcmV2UGF0aC50b1N0cmluZygpKTtcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICB9IGVsc2UgaWYgKCBwcmV2IGluc3RhbmNlb2YgbW96aWxsYS5Tb3VyY2VNYXBDb25zdW1lciApIHtcbiAgICAgICAgICAgICAgICByZXR1cm4gbW96aWxsYS5Tb3VyY2VNYXBHZW5lcmF0b3JcbiAgICAgICAgICAgICAgICAgICAgLmZyb21Tb3VyY2VNYXAocHJldikudG9TdHJpbmcoKTtcbiAgICAgICAgICAgIH0gZWxzZSBpZiAoIHByZXYgaW5zdGFuY2VvZiBtb3ppbGxhLlNvdXJjZU1hcEdlbmVyYXRvciApIHtcbiAgICAgICAgICAgICAgICByZXR1cm4gcHJldi50b1N0cmluZygpO1xuICAgICAgICAgICAgfSBlbHNlIGlmICggdGhpcy5pc01hcChwcmV2KSApIHtcbiAgICAgICAgICAgICAgICByZXR1cm4gSlNPTi5zdHJpbmdpZnkocHJldik7XG4gICAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgICAgIHRocm93IG5ldyBFcnJvcignVW5zdXBwb3J0ZWQgcHJldmlvdXMgc291cmNlIG1hcCBmb3JtYXQ6ICcgK1xuICAgICAgICAgICAgICAgICAgICBwcmV2LnRvU3RyaW5nKCkpO1xuICAgICAgICAgICAgfVxuXG4gICAgICAgIH0gZWxzZSBpZiAoIHRoaXMuaW5saW5lICkge1xuICAgICAgICAgICAgcmV0dXJuIHRoaXMuZGVjb2RlSW5saW5lKHRoaXMuYW5ub3RhdGlvbik7XG5cbiAgICAgICAgfSBlbHNlIGlmICggdGhpcy5hbm5vdGF0aW9uICkge1xuICAgICAgICAgICAgbGV0IG1hcCA9IHRoaXMuYW5ub3RhdGlvbjtcbiAgICAgICAgICAgIGlmICggZmlsZSApIG1hcCA9IHBhdGguam9pbihwYXRoLmRpcm5hbWUoZmlsZSksIG1hcCk7XG5cbiAgICAgICAgICAgIHRoaXMucm9vdCA9IHBhdGguZGlybmFtZShtYXApO1xuICAgICAgICAgICAgaWYgKCBmcy5leGlzdHNTeW5jICYmIGZzLmV4aXN0c1N5bmMobWFwKSApIHtcbiAgICAgICAgICAgICAgICByZXR1cm4gZnMucmVhZEZpbGVTeW5jKG1hcCwgJ3V0Zi04JykudG9TdHJpbmcoKS50cmltKCk7XG4gICAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgICAgIHJldHVybiBmYWxzZTtcbiAgICAgICAgICAgIH1cbiAgICAgICAgfVxuICAgIH1cblxuICAgIGlzTWFwKG1hcCkge1xuICAgICAgICBpZiAoIHR5cGVvZiBtYXAgIT09ICdvYmplY3QnICkgcmV0dXJuIGZhbHNlO1xuICAgICAgICByZXR1cm4gdHlwZW9mIG1hcC5tYXBwaW5ncyA9PT0gJ3N0cmluZycgfHxcbiAgICAgICAgICAgICAgIHR5cGVvZiBtYXAuX21hcHBpbmdzID09PSAnc3RyaW5nJztcbiAgICB9XG59XG5cbmV4cG9ydCBkZWZhdWx0IFByZXZpb3VzTWFwO1xuIl19