c0a8e019771ef5bc9d7473c2010083ab2127aec0
[yaffs-website] / node_modules / uncss / node_modules / postcss / lib / map-generator.js
1 'use strict';
2
3 exports.__esModule = true;
4
5 var _jsBase = require('js-base64');
6
7 var _sourceMap = require('source-map');
8
9 var _sourceMap2 = _interopRequireDefault(_sourceMap);
10
11 var _path = require('path');
12
13 var _path2 = _interopRequireDefault(_path);
14
15 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
18
19 var _class = function () {
20     function _class(stringify, root, opts) {
21         _classCallCheck(this, _class);
22
23         this.stringify = stringify;
24         this.mapOpts = opts.map || {};
25         this.root = root;
26         this.opts = opts;
27     }
28
29     _class.prototype.isMap = function isMap() {
30         if (typeof this.opts.map !== 'undefined') {
31             return !!this.opts.map;
32         } else {
33             return this.previous().length > 0;
34         }
35     };
36
37     _class.prototype.previous = function previous() {
38         var _this = this;
39
40         if (!this.previousMaps) {
41             this.previousMaps = [];
42             this.root.walk(function (node) {
43                 if (node.source && node.source.input.map) {
44                     var map = node.source.input.map;
45                     if (_this.previousMaps.indexOf(map) === -1) {
46                         _this.previousMaps.push(map);
47                     }
48                 }
49             });
50         }
51
52         return this.previousMaps;
53     };
54
55     _class.prototype.isInline = function isInline() {
56         if (typeof this.mapOpts.inline !== 'undefined') {
57             return this.mapOpts.inline;
58         }
59
60         var annotation = this.mapOpts.annotation;
61         if (typeof annotation !== 'undefined' && annotation !== true) {
62             return false;
63         }
64
65         if (this.previous().length) {
66             return this.previous().some(function (i) {
67                 return i.inline;
68             });
69         } else {
70             return true;
71         }
72     };
73
74     _class.prototype.isSourcesContent = function isSourcesContent() {
75         if (typeof this.mapOpts.sourcesContent !== 'undefined') {
76             return this.mapOpts.sourcesContent;
77         }
78         if (this.previous().length) {
79             return this.previous().some(function (i) {
80                 return i.withContent();
81             });
82         } else {
83             return true;
84         }
85     };
86
87     _class.prototype.clearAnnotation = function clearAnnotation() {
88         if (this.mapOpts.annotation === false) return;
89
90         var node = void 0;
91         for (var i = this.root.nodes.length - 1; i >= 0; i--) {
92             node = this.root.nodes[i];
93             if (node.type !== 'comment') continue;
94             if (node.text.indexOf('# sourceMappingURL=') === 0) {
95                 this.root.removeChild(i);
96             }
97         }
98     };
99
100     _class.prototype.setSourcesContent = function setSourcesContent() {
101         var _this2 = this;
102
103         var already = {};
104         this.root.walk(function (node) {
105             if (node.source) {
106                 var from = node.source.input.from;
107                 if (from && !already[from]) {
108                     already[from] = true;
109                     var relative = _this2.relative(from);
110                     _this2.map.setSourceContent(relative, node.source.input.css);
111                 }
112             }
113         });
114     };
115
116     _class.prototype.applyPrevMaps = function applyPrevMaps() {
117         for (var _iterator = this.previous(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
118             var _ref;
119
120             if (_isArray) {
121                 if (_i >= _iterator.length) break;
122                 _ref = _iterator[_i++];
123             } else {
124                 _i = _iterator.next();
125                 if (_i.done) break;
126                 _ref = _i.value;
127             }
128
129             var prev = _ref;
130
131             var from = this.relative(prev.file);
132             var root = prev.root || _path2.default.dirname(prev.file);
133             var map = void 0;
134
135             if (this.mapOpts.sourcesContent === false) {
136                 map = new _sourceMap2.default.SourceMapConsumer(prev.text);
137                 if (map.sourcesContent) {
138                     map.sourcesContent = map.sourcesContent.map(function () {
139                         return null;
140                     });
141                 }
142             } else {
143                 map = prev.consumer();
144             }
145
146             this.map.applySourceMap(map, from, this.relative(root));
147         }
148     };
149
150     _class.prototype.isAnnotation = function isAnnotation() {
151         if (this.isInline()) {
152             return true;
153         } else if (typeof this.mapOpts.annotation !== 'undefined') {
154             return this.mapOpts.annotation;
155         } else if (this.previous().length) {
156             return this.previous().some(function (i) {
157                 return i.annotation;
158             });
159         } else {
160             return true;
161         }
162     };
163
164     _class.prototype.addAnnotation = function addAnnotation() {
165         var content = void 0;
166
167         if (this.isInline()) {
168             content = 'data:application/json;base64,' + _jsBase.Base64.encode(this.map.toString());
169         } else if (typeof this.mapOpts.annotation === 'string') {
170             content = this.mapOpts.annotation;
171         } else {
172             content = this.outputFile() + '.map';
173         }
174
175         var eol = '\n';
176         if (this.css.indexOf('\r\n') !== -1) eol = '\r\n';
177
178         this.css += eol + '/*# sourceMappingURL=' + content + ' */';
179     };
180
181     _class.prototype.outputFile = function outputFile() {
182         if (this.opts.to) {
183             return this.relative(this.opts.to);
184         } else if (this.opts.from) {
185             return this.relative(this.opts.from);
186         } else {
187             return 'to.css';
188         }
189     };
190
191     _class.prototype.generateMap = function generateMap() {
192         this.generateString();
193         if (this.isSourcesContent()) this.setSourcesContent();
194         if (this.previous().length > 0) this.applyPrevMaps();
195         if (this.isAnnotation()) this.addAnnotation();
196
197         if (this.isInline()) {
198             return [this.css];
199         } else {
200             return [this.css, this.map];
201         }
202     };
203
204     _class.prototype.relative = function relative(file) {
205         var from = this.opts.to ? _path2.default.dirname(this.opts.to) : '.';
206
207         if (typeof this.mapOpts.annotation === 'string') {
208             from = _path2.default.dirname(_path2.default.resolve(from, this.mapOpts.annotation));
209         }
210
211         file = _path2.default.relative(from, file);
212         /* istanbul ignore next */
213         if (_path2.default.sep === '\\') {
214             return file.replace(/\\/g, '/');
215         } else {
216             return file;
217         }
218     };
219
220     _class.prototype.sourcePath = function sourcePath(node) {
221         return this.relative(node.source.input.from);
222     };
223
224     _class.prototype.generateString = function generateString() {
225         var _this3 = this;
226
227         this.css = '';
228         this.map = new _sourceMap2.default.SourceMapGenerator({ file: this.outputFile() });
229
230         var line = 1;
231         var column = 1;
232
233         var lines = void 0,
234             last = void 0;
235         this.stringify(this.root, function (str, node, type) {
236             _this3.css += str;
237
238             if (node && node.source && node.source.start && type !== 'end') {
239                 _this3.map.addMapping({
240                     source: _this3.sourcePath(node),
241                     original: {
242                         line: node.source.start.line,
243                         column: node.source.start.column - 1
244                     },
245                     generated: { line: line, column: column - 1 }
246                 });
247             }
248
249             lines = str.match(/\n/g);
250             if (lines) {
251                 line += lines.length;
252                 last = str.lastIndexOf('\n');
253                 column = str.length - last;
254             } else {
255                 column += str.length;
256             }
257
258             if (node && node.source && node.source.end && type !== 'start') {
259                 _this3.map.addMapping({
260                     source: _this3.sourcePath(node),
261                     original: {
262                         line: node.source.end.line,
263                         column: node.source.end.column
264                     },
265                     generated: { line: line, column: column - 1 }
266                 });
267             }
268         });
269     };
270
271     _class.prototype.generate = function generate() {
272         this.clearAnnotation();
273
274         if (this.isMap()) {
275             return this.generateMap();
276         } else {
277             var result = '';
278             this.stringify(this.root, function (i) {
279                 result += i;
280             });
281             return [result];
282         }
283     };
284
285     return _class;
286 }();
287
288 exports.default = _class;
289 module.exports = exports['default'];