wishthis/node_modules/postcss/lib/map-generator.js

348 lines
30 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _sourceMap = _interopRequireDefault(require("source-map"));
var _path = _interopRequireDefault(require("path"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
var MapGenerator = /*#__PURE__*/function () {
function MapGenerator(stringify, root, opts) {
this.stringify = stringify;
this.mapOpts = opts.map || {};
this.root = root;
this.opts = opts;
}
var _proto = MapGenerator.prototype;
_proto.isMap = function isMap() {
if (typeof this.opts.map !== 'undefined') {
return !!this.opts.map;
}
return this.previous().length > 0;
};
_proto.previous = function previous() {
var _this = this;
if (!this.previousMaps) {
this.previousMaps = [];
this.root.walk(function (node) {
if (node.source && node.source.input.map) {
var map = node.source.input.map;
if (_this.previousMaps.indexOf(map) === -1) {
_this.previousMaps.push(map);
}
}
});
}
return this.previousMaps;
};
_proto.isInline = function isInline() {
if (typeof this.mapOpts.inline !== 'undefined') {
return this.mapOpts.inline;
}
var annotation = this.mapOpts.annotation;
if (typeof annotation !== 'undefined' && annotation !== true) {
return false;
}
if (this.previous().length) {
return this.previous().some(function (i) {
return i.inline;
});
}
return true;
};
_proto.isSourcesContent = function isSourcesContent() {
if (typeof this.mapOpts.sourcesContent !== 'undefined') {
return this.mapOpts.sourcesContent;
}
if (this.previous().length) {
return this.previous().some(function (i) {
return i.withContent();
});
}
return true;
};
_proto.clearAnnotation = function clearAnnotation() {
if (this.mapOpts.annotation === false) return;
var node;
for (var i = this.root.nodes.length - 1; i >= 0; i--) {
node = this.root.nodes[i];
if (node.type !== 'comment') continue;
if (node.text.indexOf('# sourceMappingURL=') === 0) {
this.root.removeChild(i);
}
}
};
_proto.setSourcesContent = function setSourcesContent() {
var _this2 = this;
var already = {};
this.root.walk(function (node) {
if (node.source) {
var from = node.source.input.from;
if (from && !already[from]) {
already[from] = true;
var relative = _this2.relative(from);
_this2.map.setSourceContent(relative, node.source.input.css);
}
}
});
};
_proto.applyPrevMaps = function applyPrevMaps() {
for (var _iterator = _createForOfIteratorHelperLoose(this.previous()), _step; !(_step = _iterator()).done;) {
var prev = _step.value;
var from = this.relative(prev.file);
var root = prev.root || _path.default.dirname(prev.file);
var map = void 0;
if (this.mapOpts.sourcesContent === false) {
map = new _sourceMap.default.SourceMapConsumer(prev.text);
if (map.sourcesContent) {
map.sourcesContent = map.sourcesContent.map(function () {
return null;
});
}
} else {
map = prev.consumer();
}
this.map.applySourceMap(map, from, this.relative(root));
}
};
_proto.isAnnotation = function isAnnotation() {
if (this.isInline()) {
return true;
}
if (typeof this.mapOpts.annotation !== 'undefined') {
return this.mapOpts.annotation;
}
if (this.previous().length) {
return this.previous().some(function (i) {
return i.annotation;
});
}
return true;
};
_proto.toBase64 = function toBase64(str) {
if (Buffer) {
return Buffer.from(str).toString('base64');
}
return window.btoa(unescape(encodeURIComponent(str)));
};
_proto.addAnnotation = function addAnnotation() {
var content;
if (this.isInline()) {
content = 'data:application/json;base64,' + this.toBase64(this.map.toString());
} else if (typeof this.mapOpts.annotation === 'string') {
content = this.mapOpts.annotation;
} else {
content = this.outputFile() + '.map';
}
var eol = '\n';
if (this.css.indexOf('\r\n') !== -1) eol = '\r\n';
this.css += eol + '/*# sourceMappingURL=' + content + ' */';
};
_proto.outputFile = function outputFile() {
if (this.opts.to) {
return this.relative(this.opts.to);
}
if (this.opts.from) {
return this.relative(this.opts.from);
}
return 'to.css';
};
_proto.generateMap = function generateMap() {
this.generateString();
if (this.isSourcesContent()) this.setSourcesContent();
if (this.previous().length > 0) this.applyPrevMaps();
if (this.isAnnotation()) this.addAnnotation();
if (this.isInline()) {
return [this.css];
}
return [this.css, this.map];
};
_proto.relative = function relative(file) {
if (file.indexOf('<') === 0) return file;
if (/^\w+:\/\//.test(file)) return file;
var from = this.opts.to ? _path.default.dirname(this.opts.to) : '.';
if (typeof this.mapOpts.annotation === 'string') {
from = _path.default.dirname(_path.default.resolve(from, this.mapOpts.annotation));
}
file = _path.default.relative(from, file);
if (_path.default.sep === '\\') {
return file.replace(/\\/g, '/');
}
return file;
};
_proto.sourcePath = function sourcePath(node) {
if (this.mapOpts.from) {
return this.mapOpts.from;
}
return this.relative(node.source.input.from);
};
_proto.generateString = function generateString() {
var _this3 = this;
this.css = '';
this.map = new _sourceMap.default.SourceMapGenerator({
file: this.outputFile()
});
var line = 1;
var column = 1;
var lines, last;
this.stringify(this.root, function (str, node, type) {
_this3.css += str;
if (node && type !== 'end') {
if (node.source && node.source.start) {
_this3.map.addMapping({
source: _this3.sourcePath(node),
generated: {
line: line,
column: column - 1
},
original: {
line: node.source.start.line,
column: node.source.start.column - 1
}
});
} else {
_this3.map.addMapping({
source: '<no source>',
original: {
line: 1,
column: 0
},
generated: {
line: line,
column: column - 1
}
});
}
}
lines = str.match(/\n/g);
if (lines) {
line += lines.length;
last = str.lastIndexOf('\n');
column = str.length - last;
} else {
column += str.length;
}
if (node && type !== 'start') {
var p = node.parent || {
raws: {}
};
if (node.type !== 'decl' || node !== p.last || p.raws.semicolon) {
if (node.source && node.source.end) {
_this3.map.addMapping({
source: _this3.sourcePath(node),
generated: {
line: line,
column: column - 2
},
original: {
line: node.source.end.line,
column: node.source.end.column - 1
}
});
} else {
_this3.map.addMapping({
source: '<no source>',
original: {
line: 1,
column: 0
},
generated: {
line: line,
column: column - 1
}
});
}
}
}
});
};
_proto.generate = function generate() {
this.clearAnnotation();
if (this.isMap()) {
return this.generateMap();
}
var result = '';
this.stringify(this.root, function (i) {
result += i;
});
return [result];
};
return MapGenerator;
}();
var _default = MapGenerator;
exports.default = _default;
module.exports = exports.default;
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm1hcC1nZW5lcmF0b3IuZXM2Il0sIm5hbWVzIjpbIk1hcEdlbmVyYXRvciIsInN0cmluZ2lmeSIsInJvb3QiLCJvcHRzIiwibWFwT3B0cyIsIm1hcCIsImlzTWFwIiwicHJldmlvdXMiLCJsZW5ndGgiLCJwcmV2aW91c01hcHMiLCJ3YWxrIiwibm9kZSIsInNvdXJjZSIsImlucHV0IiwiaW5kZXhPZiIsInB1c2giLCJpc0lubGluZSIsImlubGluZSIsImFubm90YXRpb24iLCJzb21lIiwiaSIsImlzU291cmNlc0NvbnRlbnQiLCJzb3VyY2VzQ29udGVudCIsIndpdGhDb250ZW50IiwiY2xlYXJBbm5vdGF0aW9uIiwibm9kZXMiLCJ0eXBlIiwidGV4dCIsInJlbW92ZUNoaWxkIiwic2V0U291cmNlc0NvbnRlbnQiLCJhbHJlYWR5IiwiZnJvbSIsInJlbGF0aXZlIiwic2V0U291cmNlQ29udGVudCIsImNzcyIsImFwcGx5UHJldk1hcHMiLCJwcmV2IiwiZmlsZSIsInBhdGgiLCJkaXJuYW1lIiwibW96aWxsYSIsIlNvdXJjZU1hcENvbnN1bWVyIiwiY29uc3VtZXIiLCJhcHBseVN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