171 lines
18 KiB
JavaScript
171 lines
18 KiB
JavaScript
|
'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 _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"); } }
|
||
|
|
||
|
function fromBase64(str) {
|
||
|
if (Buffer) {
|
||
|
if (Buffer.from && Buffer.from !== Uint8Array.from) {
|
||
|
return Buffer.from(str, 'base64').toString();
|
||
|
} else {
|
||
|
return new Buffer(str, 'base64').toString();
|
||
|
}
|
||
|
} else {
|
||
|
return window.atob(str);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Source map information from input CSS.
|
||
|
* For example, source map after Sass compiler.
|
||
|
*
|
||
|
* This class will automatically find source map in input CSS or in file system
|
||
|
* near input file (according `from` option).
|
||
|
*
|
||
|
* @example
|
||
|
* const root = postcss.parse(css, { from: 'a.sass.css' });
|
||
|
* root.input.map //=> PreviousMap
|
||
|
*/
|
||
|
|
||
|
var PreviousMap = function () {
|
||
|
|
||
|
/**
|
||
|
* @param {string} css - input CSS source
|
||
|
* @param {processOptions} [opts] - {@link Processor#process} options
|
||
|
*/
|
||
|
function PreviousMap(css, opts) {
|
||
|
_classCallCheck(this, PreviousMap);
|
||
|
|
||
|
this.loadAnnotation(css);
|
||
|
/**
|
||
|
* @member {boolean} - Was source map inlined by data-uri to input 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;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a instance of `SourceMapGenerator` class
|
||
|
* from the `source-map` library to work with source map information.
|
||
|
*
|
||
|
* It is lazy method, so it will create object only on first call
|
||
|
* and then it will use cache.
|
||
|
*
|
||
|
* @return {SourceMapGenerator} object with source map information
|
||
|
*/
|
||
|
|
||
|
|
||
|
PreviousMap.prototype.consumer = function consumer() {
|
||
|
if (!this.consumerCache) {
|
||
|
this.consumerCache = new _sourceMap2.default.SourceMapConsumer(this.text);
|
||
|
}
|
||
|
return this.consumerCache;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Does source map contains `sourcesContent` with input source text.
|
||
|
*
|
||
|
* @return {boolean} Is `sourcesContent` present
|
||
|
*/
|
||
|
|
||
|
|
||
|
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) {
|
||
|
// data:application/json;charset=utf-8;base64,
|
||
|
// data:application/json;charset=utf8;base64,
|
||
|
// data:application/json;base64,
|
||
|
var baseUri = /^data:application\/json;(?:charset=utf-?8;)?base64,/;
|
||
|
var uri = 'data:application/json,';
|
||
|
|
||
|
if (this.startWith(text, uri)) {
|
||
|
return decodeURIComponent(text.substr(uri.length));
|
||
|
} else if (baseUri.test(text)) {
|
||
|
return fromBase64(text.substr(RegExp.lastMatch.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 (typeof prev === 'function') {
|
||
|
var prevPath = prev(file);
|
||
|
if (prevPath && _fs2.default.existsSync && _fs2.default.existsSync(prevPath)) {
|
||
|
return _fs2.default.readFileSync(prevPath, 'utf-8').toString().trim();
|
||
|
} else {
|
||
|
throw new Error('Unable to load previous source map: ' + prevPath.toString());
|
||
|
}
|
||
|
} 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 (this.isMap(prev)) {
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
PreviousMap.prototype.isMap = function isMap(map) {
|
||
|
if ((typeof map === 'undefined' ? 'undefined' : _typeof(map)) !== 'object') return false;
|
||
|
return typeof map.mappings === 'string' || typeof map._mappings === 'string';
|
||
|
};
|
||
|
|
||
|
return PreviousMap;
|
||
|
}();
|
||
|
|
||
|
exports.default = PreviousMap;
|
||
|
module.exports = exports['default'];
|
||
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInByZXZpb3VzLW1hcC5lczYiXSwibmFtZXMiOlsiZnJvbUJhc2U2NCIsInN0ciIsIkJ1ZmZlciIsImZyb20iLCJVaW50OEFycmF5IiwidG9TdHJpbmciLCJ3aW5kb3ciLCJhdG9iIiwiUHJldmlvdXNNYXAiLCJjc3MiLCJvcHRzIiwibG9hZEFubm90YXRpb24iLCJpbmxpbmUiLCJzdGFydFdpdGgiLCJhbm5vdGF0aW9uIiwicHJldiIsIm1hcCIsInVuZGVmaW5lZCIsInRleHQiLCJsb2FkTWFwIiwiY29uc3VtZXIiLCJjb25zdW1lckNhY2hlIiwibW96aWxsYSIsIlNvdXJjZU1hcENvbnN1bWVyIiwid2l0aENvbnRlbnQiLCJzb3VyY2VzQ29udGVudCIsImxlbmd0aCIsInN0cmluZyIsInN0YXJ0Iiwic3Vic3RyIiwibWF0Y2giLCJ0cmltIiwiZGVjb2RlSW5saW5lIiwiYmFzZVVyaSIsInVyaSIsImRlY29kZVVSSUNvbXBvbmVudCIsInRlc3QiLCJSZWdFeHAiLCJsYXN0TWF0Y2giLCJlbmNvZGluZyIsIkVycm9yIiwiZmlsZSIsInByZXZQYXRoIiwiZnMiLCJleGlzdHNTeW5jIiwicmVhZEZpbGVTeW5jIiwiU291cmNlTWFwR2VuZXJhdG9yIiwiZnJvbVNvdXJjZU1hcCIsImlzTWFwIiwiSlNPTiIsInN0cmluZ2lmeSIsInBhdGgiLCJqb2luIiwiZGlybmFtZSIsInJvb3QiLCJtYXBwaW5ncyIsIl9tYXBwaW5ncyJdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUE7Ozs7QUFDQTs7OztBQUNBOzs7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