logs-D80CXhvg.mjs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //#region src/utils/code-frame.ts
  2. function spaces(index) {
  3. let result = "";
  4. while (index--) result += " ";
  5. return result;
  6. }
  7. function tabsToSpaces(value) {
  8. return value.replace(/^\t+/, (match) => match.split(" ").join(" "));
  9. }
  10. const LINE_TRUNCATE_LENGTH = 120;
  11. const MIN_CHARACTERS_SHOWN_AFTER_LOCATION = 10;
  12. const ELLIPSIS = "...";
  13. function getCodeFrame(source, line, column) {
  14. let lines = source.split("\n");
  15. if (line > lines.length) return "";
  16. const maxLineLength = Math.max(tabsToSpaces(lines[line - 1].slice(0, column)).length + MIN_CHARACTERS_SHOWN_AFTER_LOCATION + 3, LINE_TRUNCATE_LENGTH);
  17. const frameStart = Math.max(0, line - 3);
  18. let frameEnd = Math.min(line + 2, lines.length);
  19. lines = lines.slice(frameStart, frameEnd);
  20. while (!/\S/.test(lines[lines.length - 1])) {
  21. lines.pop();
  22. frameEnd -= 1;
  23. }
  24. const digits = String(frameEnd).length;
  25. return lines.map((sourceLine, index) => {
  26. const isErrorLine = frameStart + index + 1 === line;
  27. let lineNumber = String(index + frameStart + 1);
  28. while (lineNumber.length < digits) lineNumber = ` ${lineNumber}`;
  29. let displayedLine = tabsToSpaces(sourceLine);
  30. if (displayedLine.length > maxLineLength) displayedLine = `${displayedLine.slice(0, maxLineLength - 3)}${ELLIPSIS}`;
  31. if (isErrorLine) {
  32. const indicator = spaces(digits + 2 + tabsToSpaces(sourceLine.slice(0, column)).length) + "^";
  33. return `${lineNumber}: ${displayedLine}\n${indicator}`;
  34. }
  35. return `${lineNumber}: ${displayedLine}`;
  36. }).join("\n");
  37. }
  38. //#endregion
  39. //#region src/log/locate-character/index.js
  40. /** @typedef {import('./types').Location} Location */
  41. /**
  42. * @param {import('./types').Range} range
  43. * @param {number} index
  44. */
  45. function rangeContains(range, index) {
  46. return range.start <= index && index < range.end;
  47. }
  48. /**
  49. * @param {string} source
  50. * @param {import('./types').Options} [options]
  51. */
  52. function getLocator(source, options = {}) {
  53. const { offsetLine = 0, offsetColumn = 0 } = options;
  54. let start = 0;
  55. const ranges = source.split("\n").map((line, i) => {
  56. const end = start + line.length + 1;
  57. /** @type {import('./types').Range} */
  58. const range = {
  59. start,
  60. end,
  61. line: i
  62. };
  63. start = end;
  64. return range;
  65. });
  66. let i = 0;
  67. /**
  68. * @param {string | number} search
  69. * @param {number} [index]
  70. * @returns {Location | undefined}
  71. */
  72. function locator(search, index) {
  73. if (typeof search === "string") search = source.indexOf(search, index ?? 0);
  74. if (search === -1) return void 0;
  75. let range = ranges[i];
  76. const d = search >= range.end ? 1 : -1;
  77. while (range) {
  78. if (rangeContains(range, search)) return {
  79. line: offsetLine + range.line,
  80. column: offsetColumn + search - range.start,
  81. character: search
  82. };
  83. i += d;
  84. range = ranges[i];
  85. }
  86. }
  87. return locator;
  88. }
  89. /**
  90. * @param {string} source
  91. * @param {string | number} search
  92. * @param {import('./types').Options} [options]
  93. * @returns {Location | undefined}
  94. */
  95. function locate(source, search, options) {
  96. return getLocator(source, options)(search, options && options.startIndex);
  97. }
  98. //#endregion
  99. //#region src/log/logs.ts
  100. const INVALID_LOG_POSITION = "INVALID_LOG_POSITION", PLUGIN_ERROR = "PLUGIN_ERROR", INPUT_HOOK_IN_OUTPUT_PLUGIN = "INPUT_HOOK_IN_OUTPUT_PLUGIN", CYCLE_LOADING = "CYCLE_LOADING", MULTIPLE_WATCHER_OPTION = "MULTIPLE_WATCHER_OPTION", PARSE_ERROR = "PARSE_ERROR";
  101. function logParseError(message, id, pos) {
  102. return {
  103. code: PARSE_ERROR,
  104. id,
  105. message,
  106. pos
  107. };
  108. }
  109. function logInvalidLogPosition(pluginName) {
  110. return {
  111. code: INVALID_LOG_POSITION,
  112. message: `Plugin "${pluginName}" tried to add a file position to a log or warning. This is only supported in the "transform" hook at the moment and will be ignored.`
  113. };
  114. }
  115. function logInputHookInOutputPlugin(pluginName, hookName) {
  116. return {
  117. code: INPUT_HOOK_IN_OUTPUT_PLUGIN,
  118. message: `The "${hookName}" hook used by the output plugin ${pluginName} is a build time hook and will not be run for that plugin. Either this plugin cannot be used as an output plugin, or it should have an option to configure it as an output plugin.`
  119. };
  120. }
  121. function logCycleLoading(pluginName, moduleId) {
  122. return {
  123. code: CYCLE_LOADING,
  124. message: `Found the module "${moduleId}" cycle loading at ${pluginName} plugin, it maybe blocking fetching modules.`
  125. };
  126. }
  127. function logMultipleWatcherOption() {
  128. return {
  129. code: MULTIPLE_WATCHER_OPTION,
  130. message: `Found multiple watcher options at watch options, using first one to start watcher.`
  131. };
  132. }
  133. function logPluginError(error, plugin, { hook, id } = {}) {
  134. try {
  135. const code = error.code;
  136. if (!error.pluginCode && code != null && (typeof code !== "string" || !code.startsWith("PLUGIN_"))) error.pluginCode = code;
  137. error.code = PLUGIN_ERROR;
  138. error.plugin = plugin;
  139. if (hook) error.hook = hook;
  140. if (id) error.id = id;
  141. } catch (_) {} finally {
  142. return error;
  143. }
  144. }
  145. function error(base) {
  146. if (!(base instanceof Error)) {
  147. base = Object.assign(new Error(base.message), base);
  148. Object.defineProperty(base, "name", {
  149. value: "RolldownError",
  150. writable: true
  151. });
  152. }
  153. throw base;
  154. }
  155. function augmentCodeLocation(properties, pos, source, id) {
  156. if (typeof pos === "object") {
  157. const { line, column } = pos;
  158. properties.loc = {
  159. column,
  160. file: id,
  161. line
  162. };
  163. } else {
  164. properties.pos = pos;
  165. const location = locate(source, pos, { offsetLine: 1 });
  166. if (!location) return;
  167. const { line, column } = location;
  168. properties.loc = {
  169. column,
  170. file: id,
  171. line
  172. };
  173. }
  174. if (properties.frame === void 0) {
  175. const { line, column } = properties.loc;
  176. properties.frame = getCodeFrame(source, line, column);
  177. }
  178. }
  179. //#endregion
  180. export { logInvalidLogPosition as a, logPluginError as c, logInputHookInOutputPlugin as i, locate as l, error as n, logMultipleWatcherOption as o, logCycleLoading as r, logParseError as s, augmentCodeLocation as t, getCodeFrame as u };