3 exports.__esModule = true;
5 var _jsBase = require('js-base64');
7 var _sourceMap = require('source-map');
9 var _sourceMap2 = _interopRequireDefault(_sourceMap);
11 var _path = require('path');
13 var _path2 = _interopRequireDefault(_path);
15 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
19 var _class = function () {
20 function _class(stringify, root, opts) {
21 _classCallCheck(this, _class);
23 this.stringify = stringify;
24 this.mapOpts = opts.map || {};
29 _class.prototype.isMap = function isMap() {
30 if (typeof this.opts.map !== 'undefined') {
31 return !!this.opts.map;
33 return this.previous().length > 0;
37 _class.prototype.previous = function previous() {
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);
52 return this.previousMaps;
55 _class.prototype.isInline = function isInline() {
56 if (typeof this.mapOpts.inline !== 'undefined') {
57 return this.mapOpts.inline;
60 var annotation = this.mapOpts.annotation;
61 if (typeof annotation !== 'undefined' && annotation !== true) {
65 if (this.previous().length) {
66 return this.previous().some(function (i) {
74 _class.prototype.isSourcesContent = function isSourcesContent() {
75 if (typeof this.mapOpts.sourcesContent !== 'undefined') {
76 return this.mapOpts.sourcesContent;
78 if (this.previous().length) {
79 return this.previous().some(function (i) {
80 return i.withContent();
87 _class.prototype.clearAnnotation = function clearAnnotation() {
88 if (this.mapOpts.annotation === false) return;
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);
100 _class.prototype.setSourcesContent = function setSourcesContent() {
104 this.root.walk(function (node) {
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);
116 _class.prototype.applyPrevMaps = function applyPrevMaps() {
117 for (var _iterator = this.previous(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
121 if (_i >= _iterator.length) break;
122 _ref = _iterator[_i++];
124 _i = _iterator.next();
131 var from = this.relative(prev.file);
132 var root = prev.root || _path2.default.dirname(prev.file);
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 () {
143 map = prev.consumer();
146 this.map.applySourceMap(map, from, this.relative(root));
150 _class.prototype.isAnnotation = function isAnnotation() {
151 if (this.isInline()) {
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) {
164 _class.prototype.addAnnotation = function addAnnotation() {
165 var content = void 0;
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;
172 content = this.outputFile() + '.map';
176 if (this.css.indexOf('\r\n') !== -1) eol = '\r\n';
178 this.css += eol + '/*# sourceMappingURL=' + content + ' */';
181 _class.prototype.outputFile = function outputFile() {
183 return this.relative(this.opts.to);
184 } else if (this.opts.from) {
185 return this.relative(this.opts.from);
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();
197 if (this.isInline()) {
200 return [this.css, this.map];
204 _class.prototype.relative = function relative(file) {
205 var from = this.opts.to ? _path2.default.dirname(this.opts.to) : '.';
207 if (typeof this.mapOpts.annotation === 'string') {
208 from = _path2.default.dirname(_path2.default.resolve(from, this.mapOpts.annotation));
211 file = _path2.default.relative(from, file);
212 /* istanbul ignore next */
213 if (_path2.default.sep === '\\') {
214 return file.replace(/\\/g, '/');
220 _class.prototype.sourcePath = function sourcePath(node) {
221 return this.relative(node.source.input.from);
224 _class.prototype.generateString = function generateString() {
228 this.map = new _sourceMap2.default.SourceMapGenerator({ file: this.outputFile() });
235 this.stringify(this.root, function (str, node, type) {
238 if (node && node.source && node.source.start && type !== 'end') {
239 _this3.map.addMapping({
240 source: _this3.sourcePath(node),
242 line: node.source.start.line,
243 column: node.source.start.column - 1
245 generated: { line: line, column: column - 1 }
249 lines = str.match(/\n/g);
251 line += lines.length;
252 last = str.lastIndexOf('\n');
253 column = str.length - last;
255 column += str.length;
258 if (node && node.source && node.source.end && type !== 'start') {
259 _this3.map.addMapping({
260 source: _this3.sourcePath(node),
262 line: node.source.end.line,
263 column: node.source.end.column
265 generated: { line: line, column: column - 1 }
271 _class.prototype.generate = function generate() {
272 this.clearAnnotation();
275 return this.generateMap();
278 this.stringify(this.root, function (i) {
288 exports.default = _class;
289 module.exports = exports['default'];