PageRenderTime 77ms CodeModel.GetById 46ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/dimitrisdervas/systima
JavaScript | 288 lines | 237 code | 51 blank | 0 comment | 68 complexity | 27cc67fcc36c87fb8c61429bfc62d848 MD5 | raw file
  1. 'use strict';
  2. exports.__esModule = true;
  3. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  5. var _jsBase64 = require('js-base64');
  6. var _sourceMap = require('source-map');
  7. var _sourceMap2 = _interopRequireDefault(_sourceMap);
  8. var _path = require('path');
  9. var _path2 = _interopRequireDefault(_path);
  10. var _default = (function () {
  11. function _default(stringify, root, opts) {
  12. _classCallCheck(this, _default);
  13. this.stringify = stringify;
  14. this.mapOpts = opts.map || {};
  15. this.root = root;
  16. this.opts = opts;
  17. }
  18. _default.prototype.isMap = function isMap() {
  19. if (typeof this.opts.map !== 'undefined') {
  20. return !!this.opts.map;
  21. } else {
  22. return this.previous().length > 0;
  23. }
  24. };
  25. _default.prototype.previous = function previous() {
  26. var _this = this;
  27. if (!this.previousMaps) {
  28. this.previousMaps = [];
  29. this.root.walk(function (node) {
  30. if (node.source && node.source.input.map) {
  31. var map = node.source.input.map;
  32. if (_this.previousMaps.indexOf(map) === -1) {
  33. _this.previousMaps.push(map);
  34. }
  35. }
  36. });
  37. }
  38. return this.previousMaps;
  39. };
  40. _default.prototype.isInline = function isInline() {
  41. if (typeof this.mapOpts.inline !== 'undefined') {
  42. return this.mapOpts.inline;
  43. }
  44. var annotation = this.mapOpts.annotation;
  45. if (typeof annotation !== 'undefined' && annotation !== true) {
  46. return false;
  47. }
  48. if (this.previous().length) {
  49. return this.previous().some(function (i) {
  50. return i.inline;
  51. });
  52. } else {
  53. return true;
  54. }
  55. };
  56. _default.prototype.isSourcesContent = function isSourcesContent() {
  57. if (typeof this.mapOpts.sourcesContent !== 'undefined') {
  58. return this.mapOpts.sourcesContent;
  59. }
  60. if (this.previous().length) {
  61. return this.previous().some(function (i) {
  62. return i.withContent();
  63. });
  64. } else {
  65. return true;
  66. }
  67. };
  68. _default.prototype.clearAnnotation = function clearAnnotation() {
  69. if (this.mapOpts.annotation === false) return;
  70. var node = undefined;
  71. for (var i = this.root.nodes.length - 1; i >= 0; i--) {
  72. node = this.root.nodes[i];
  73. if (node.type !== 'comment') continue;
  74. if (node.text.indexOf('# sourceMappingURL=') === 0) {
  75. this.root.removeChild(i);
  76. }
  77. }
  78. };
  79. _default.prototype.setSourcesContent = function setSourcesContent() {
  80. var _this2 = this;
  81. var already = {};
  82. this.root.walk(function (node) {
  83. if (node.source) {
  84. var from = node.source.input.from;
  85. if (from && !already[from]) {
  86. already[from] = true;
  87. var relative = _this2.relative(from);
  88. _this2.map.setSourceContent(relative, node.source.input.css);
  89. }
  90. }
  91. });
  92. };
  93. _default.prototype.applyPrevMaps = function applyPrevMaps() {
  94. for (var _iterator = this.previous(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  95. var _ref;
  96. if (_isArray) {
  97. if (_i >= _iterator.length) break;
  98. _ref = _iterator[_i++];
  99. } else {
  100. _i = _iterator.next();
  101. if (_i.done) break;
  102. _ref = _i.value;
  103. }
  104. var prev = _ref;
  105. var from = this.relative(prev.file);
  106. var root = prev.root || _path2['default'].dirname(prev.file);
  107. var map = undefined;
  108. if (this.mapOpts.sourcesContent === false) {
  109. map = new _sourceMap2['default'].SourceMapConsumer(prev.text);
  110. if (map.sourcesContent) {
  111. map.sourcesContent = map.sourcesContent.map(function () {
  112. return null;
  113. });
  114. }
  115. } else {
  116. map = prev.consumer();
  117. }
  118. this.map.applySourceMap(map, from, this.relative(root));
  119. }
  120. };
  121. _default.prototype.isAnnotation = function isAnnotation() {
  122. if (this.isInline()) {
  123. return true;
  124. } else if (typeof this.mapOpts.annotation !== 'undefined') {
  125. return this.mapOpts.annotation;
  126. } else if (this.previous().length) {
  127. return this.previous().some(function (i) {
  128. return i.annotation;
  129. });
  130. } else {
  131. return true;
  132. }
  133. };
  134. _default.prototype.addAnnotation = function addAnnotation() {
  135. var content = undefined;
  136. if (this.isInline()) {
  137. content = 'data:application/json;base64,' + _jsBase64.Base64.encode(this.map.toString());
  138. } else if (typeof this.mapOpts.annotation === 'string') {
  139. content = this.mapOpts.annotation;
  140. } else {
  141. content = this.outputFile() + '.map';
  142. }
  143. var eol = '\n';
  144. if (this.css.indexOf('\r\n') !== -1) eol = '\r\n';
  145. this.css += eol + '/*# sourceMappingURL=' + content + ' */';
  146. };
  147. _default.prototype.outputFile = function outputFile() {
  148. if (this.opts.to) {
  149. return this.relative(this.opts.to);
  150. } else if (this.opts.from) {
  151. return this.relative(this.opts.from);
  152. } else {
  153. return 'to.css';
  154. }
  155. };
  156. _default.prototype.generateMap = function generateMap() {
  157. this.generateString();
  158. if (this.isSourcesContent()) this.setSourcesContent();
  159. if (this.previous().length > 0) this.applyPrevMaps();
  160. if (this.isAnnotation()) this.addAnnotation();
  161. if (this.isInline()) {
  162. return [this.css];
  163. } else {
  164. return [this.css, this.map];
  165. }
  166. };
  167. _default.prototype.relative = function relative(file) {
  168. var from = this.opts.to ? _path2['default'].dirname(this.opts.to) : '.';
  169. if (typeof this.mapOpts.annotation === 'string') {
  170. from = _path2['default'].dirname(_path2['default'].resolve(from, this.mapOpts.annotation));
  171. }
  172. file = _path2['default'].relative(from, file);
  173. if (_path2['default'].sep === '\\') {
  174. return file.replace(/\\/g, '/');
  175. } else {
  176. return file;
  177. }
  178. };
  179. _default.prototype.sourcePath = function sourcePath(node) {
  180. return this.relative(node.source.input.from);
  181. };
  182. _default.prototype.generateString = function generateString() {
  183. var _this3 = this;
  184. this.css = '';
  185. this.map = new _sourceMap2['default'].SourceMapGenerator({ file: this.outputFile() });
  186. var line = 1;
  187. var column = 1;
  188. var lines = undefined,
  189. last = undefined;
  190. this.stringify(this.root, function (str, node, type) {
  191. _this3.css += str;
  192. if (node && node.source && node.source.start && type !== 'end') {
  193. _this3.map.addMapping({
  194. source: _this3.sourcePath(node),
  195. original: {
  196. line: node.source.start.line,
  197. column: node.source.start.column - 1
  198. },
  199. generated: { line: line, column: column - 1 }
  200. });
  201. }
  202. lines = str.match(/\n/g);
  203. if (lines) {
  204. line += lines.length;
  205. last = str.lastIndexOf('\n');
  206. column = str.length - last;
  207. } else {
  208. column += str.length;
  209. }
  210. if (node && node.source && node.source.end && type !== 'start') {
  211. _this3.map.addMapping({
  212. source: _this3.sourcePath(node),
  213. original: {
  214. line: node.source.end.line,
  215. column: node.source.end.column
  216. },
  217. generated: { line: line, column: column - 1 }
  218. });
  219. }
  220. });
  221. };
  222. _default.prototype.generate = function generate() {
  223. this.clearAnnotation();
  224. if (this.isMap()) {
  225. return this.generateMap();
  226. } else {
  227. var result = '';
  228. this.stringify(this.root, function (i) {
  229. return result += i;
  230. });
  231. return [result];
  232. }
  233. };
  234. return _default;
  235. })();
  236. exports['default'] = _default;
  237. module.exports = exports['default'];