You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
30 KiB

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