error-reporting.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. var utils = __importStar(require("./utils"));
  26. var browser_1 = __importDefault(require("./browser"));
  27. exports.default = (function (window, less, options) {
  28. function errorHTML(e, rootHref) {
  29. var id = "less-error-message:" + utils.extractId(rootHref || '');
  30. var template = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>';
  31. var elem = window.document.createElement('div');
  32. var timer;
  33. var content;
  34. var errors = [];
  35. var filename = e.filename || rootHref;
  36. var filenameNoPath = filename.match(/([^\/]+(\?.*)?)$/)[1];
  37. elem.id = id;
  38. elem.className = 'less-error-message';
  39. content = "<h3>" + (e.type || 'Syntax') + "Error: " + (e.message || 'There is an error in your .less file') +
  40. ("</h3><p>in <a href=\"" + filename + "\">" + filenameNoPath + "</a> ");
  41. var errorline = function (e, i, classname) {
  42. if (e.extract[i] !== undefined) {
  43. errors.push(template.replace(/\{line\}/, (parseInt(e.line, 10) || 0) + (i - 1))
  44. .replace(/\{class\}/, classname)
  45. .replace(/\{content\}/, e.extract[i]));
  46. }
  47. };
  48. if (e.line) {
  49. errorline(e, 0, '');
  50. errorline(e, 1, 'line');
  51. errorline(e, 2, '');
  52. content += "on line " + e.line + ", column " + (e.column + 1) + ":</p><ul>" + errors.join('') + "</ul>";
  53. }
  54. if (e.stack && (e.extract || options.logLevel >= 4)) {
  55. content += "<br/>Stack Trace</br />" + e.stack.split('\n').slice(1).join('<br/>');
  56. }
  57. elem.innerHTML = content;
  58. // CSS for error messages
  59. browser_1.default.createCSS(window.document, [
  60. '.less-error-message ul, .less-error-message li {',
  61. 'list-style-type: none;',
  62. 'margin-right: 15px;',
  63. 'padding: 4px 0;',
  64. 'margin: 0;',
  65. '}',
  66. '.less-error-message label {',
  67. 'font-size: 12px;',
  68. 'margin-right: 15px;',
  69. 'padding: 4px 0;',
  70. 'color: #cc7777;',
  71. '}',
  72. '.less-error-message pre {',
  73. 'color: #dd6666;',
  74. 'padding: 4px 0;',
  75. 'margin: 0;',
  76. 'display: inline-block;',
  77. '}',
  78. '.less-error-message pre.line {',
  79. 'color: #ff0000;',
  80. '}',
  81. '.less-error-message h3 {',
  82. 'font-size: 20px;',
  83. 'font-weight: bold;',
  84. 'padding: 15px 0 5px 0;',
  85. 'margin: 0;',
  86. '}',
  87. '.less-error-message a {',
  88. 'color: #10a',
  89. '}',
  90. '.less-error-message .error {',
  91. 'color: red;',
  92. 'font-weight: bold;',
  93. 'padding-bottom: 2px;',
  94. 'border-bottom: 1px dashed red;',
  95. '}'
  96. ].join('\n'), { title: 'error-message' });
  97. elem.style.cssText = [
  98. 'font-family: Arial, sans-serif',
  99. 'border: 1px solid #e00',
  100. 'background-color: #eee',
  101. 'border-radius: 5px',
  102. '-webkit-border-radius: 5px',
  103. '-moz-border-radius: 5px',
  104. 'color: #e00',
  105. 'padding: 15px',
  106. 'margin-bottom: 15px'
  107. ].join(';');
  108. if (options.env === 'development') {
  109. timer = setInterval(function () {
  110. var document = window.document;
  111. var body = document.body;
  112. if (body) {
  113. if (document.getElementById(id)) {
  114. body.replaceChild(elem, document.getElementById(id));
  115. }
  116. else {
  117. body.insertBefore(elem, body.firstChild);
  118. }
  119. clearInterval(timer);
  120. }
  121. }, 10);
  122. }
  123. }
  124. function removeErrorHTML(path) {
  125. var node = window.document.getElementById("less-error-message:" + utils.extractId(path));
  126. if (node) {
  127. node.parentNode.removeChild(node);
  128. }
  129. }
  130. function removeErrorConsole(path) {
  131. // no action
  132. }
  133. function removeError(path) {
  134. if (!options.errorReporting || options.errorReporting === 'html') {
  135. removeErrorHTML(path);
  136. }
  137. else if (options.errorReporting === 'console') {
  138. removeErrorConsole(path);
  139. }
  140. else if (typeof options.errorReporting === 'function') {
  141. options.errorReporting('remove', path);
  142. }
  143. }
  144. function errorConsole(e, rootHref) {
  145. var template = '{line} {content}';
  146. var filename = e.filename || rootHref;
  147. var errors = [];
  148. var content = (e.type || 'Syntax') + "Error: " + (e.message || 'There is an error in your .less file') + " in " + filename;
  149. var errorline = function (e, i, classname) {
  150. if (e.extract[i] !== undefined) {
  151. errors.push(template.replace(/\{line\}/, (parseInt(e.line, 10) || 0) + (i - 1))
  152. .replace(/\{class\}/, classname)
  153. .replace(/\{content\}/, e.extract[i]));
  154. }
  155. };
  156. if (e.line) {
  157. errorline(e, 0, '');
  158. errorline(e, 1, 'line');
  159. errorline(e, 2, '');
  160. content += " on line " + e.line + ", column " + (e.column + 1) + ":\n" + errors.join('\n');
  161. }
  162. if (e.stack && (e.extract || options.logLevel >= 4)) {
  163. content += "\nStack Trace\n" + e.stack;
  164. }
  165. less.logger.error(content);
  166. }
  167. function error(e, rootHref) {
  168. if (!options.errorReporting || options.errorReporting === 'html') {
  169. errorHTML(e, rootHref);
  170. }
  171. else if (options.errorReporting === 'console') {
  172. errorConsole(e, rootHref);
  173. }
  174. else if (typeof options.errorReporting === 'function') {
  175. options.errorReporting('add', e, rootHref);
  176. }
  177. }
  178. return {
  179. add: error,
  180. remove: removeError
  181. };
  182. });
  183. //# sourceMappingURL=error-reporting.js.map