error-reporting.js 6.1 KB

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