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