163 lines
18 KiB
JavaScript
163 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 _jsBase = require('js-base64');
|
||
|
|
||
|
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"); } }
|
||
|
|
||
|
/**
|
||
|
* 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) {
|
||
|
var utfd64 = 'data:application/json;charset=utf-8;base64,';
|
||
|
var utf64 = 'data:application/json;charset=utf8;base64,';
|
||
|
var b64 = 'data:application/json;base64,';
|
||
|
var uri = 'data:application/json,';
|
||
|
|
||
|
if (this.startWith(text, uri)) {
|
||
|
return decodeURIComponent(text.substr(uri.length));
|
||
|
} else if (this.startWith(text, b64)) {
|
||
|
return _jsBase.Base64.decode(text.substr(b64.length));
|
||
|
} else if (this.startWith(text, utf64)) {
|
||
|
return _jsBase.Base64.decode(text.substr(utf64.length));
|
||
|
} else if (this.startWith(text, utfd64)) {
|
||
|
return _jsBase.Base64.decode(text.substr(utfd64.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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInByZXZpb3VzLW1hcC5lczYiXSwibmFtZXMiOlsiUHJldmlvdXNNYXAiLCJjc3MiLCJvcHRzIiwibG9hZEFubm90YXRpb24iLCJpbmxpbmUiLCJzdGFydFdpdGgiLCJhbm5vdGF0aW9uIiwicHJldiIsIm1hcCIsInVuZGVmaW5lZCIsInRleHQiLCJsb2FkTWFwIiwiZnJvbSIsImNvbnN1bWVyIiwiY29uc3VtZXJDYWNoZSIsIlNvdXJjZU1hcENvbnN1bWVyIiwid2l0aENvbnRlbnQiLCJzb3VyY2VzQ29udGVudCIsImxlbmd0aCIsInN0cmluZyIsInN0YXJ0Iiwic3Vic3RyIiwibWF0Y2giLCJ0cmltIiwiZGVjb2RlSW5saW5lIiwidXRmZDY0IiwidXRmNjQiLCJiNjQiLCJ1cmkiLCJkZWNvZGVVUklDb21wb25lbnQiLCJkZWNvZGUiLCJlbmNvZGluZyIsIkVycm9yIiwiZmlsZSIsInByZXZQYXRoIiwiZXhpc3RzU3luYyIsInJlYWRGaWxlU3luYyIsInRvU3RyaW5nIiwiU291cmNlTWFwR2VuZXJhdG9yIiwiZnJvbVNvdXJjZU1hcCIsImlzTWFwIiwiSlNPTiIsInN0cmluZ2lmeSIsImpvaW4iLCJkaXJuYW1lIiwicm9vdCIsIm1hcHBpbmdzIiwiX21hcHBpbmdzIl0sIm1hcHBpbmdzIjoiOzs7Ozs7QUFBQTs7QUFDQTs7OztBQUNBOzs7O0FBQ0E7Ozs7Ozs7O0FBRUE7Ozs7Ozs7Ozs7O0lBV01BLFc7O0FBRUY7Ozs7QUFJQSx5QkFBWUMsR0FBWixFQUFpQkMsSUFBakIsRUFBdUI7QUFBQTs7QUFDbkIsYUFBS0MsY0FBTCxDQUFvQkYsR0FBcEI7QUFDQTs7O0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