logger.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import { s as __toESM, t as __commonJSMin } from "./chunk.js";
  2. import { readFileSync } from "node:fs";
  3. import path, { resolve } from "node:path";
  4. import { fileURLToPath } from "node:url";
  5. import readline from "node:readline";
  6. //#region ../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js
  7. var require_picocolors = /* @__PURE__ */ __commonJSMin(((exports, module) => {
  8. let p = process || {}, argv = p.argv || [], env = p.env || {};
  9. let isColorSupported = !(!!env.NO_COLOR || argv.includes("--no-color")) && (!!env.FORCE_COLOR || argv.includes("--color") || p.platform === "win32" || (p.stdout || {}).isTTY && env.TERM !== "dumb" || !!env.CI);
  10. let formatter = (open, close, replace = open) => (input) => {
  11. let string = "" + input, index = string.indexOf(close, open.length);
  12. return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close;
  13. };
  14. let replaceClose = (string, close, replace, index) => {
  15. let result = "", cursor = 0;
  16. do {
  17. result += string.substring(cursor, index) + replace;
  18. cursor = index + close.length;
  19. index = string.indexOf(close, cursor);
  20. } while (~index);
  21. return result + string.substring(cursor);
  22. };
  23. let createColors = (enabled = isColorSupported) => {
  24. let f = enabled ? formatter : () => String;
  25. return {
  26. isColorSupported: enabled,
  27. reset: f("\x1B[0m", "\x1B[0m"),
  28. bold: f("\x1B[1m", "\x1B[22m", "\x1B[22m\x1B[1m"),
  29. dim: f("\x1B[2m", "\x1B[22m", "\x1B[22m\x1B[2m"),
  30. italic: f("\x1B[3m", "\x1B[23m"),
  31. underline: f("\x1B[4m", "\x1B[24m"),
  32. inverse: f("\x1B[7m", "\x1B[27m"),
  33. hidden: f("\x1B[8m", "\x1B[28m"),
  34. strikethrough: f("\x1B[9m", "\x1B[29m"),
  35. black: f("\x1B[30m", "\x1B[39m"),
  36. red: f("\x1B[31m", "\x1B[39m"),
  37. green: f("\x1B[32m", "\x1B[39m"),
  38. yellow: f("\x1B[33m", "\x1B[39m"),
  39. blue: f("\x1B[34m", "\x1B[39m"),
  40. magenta: f("\x1B[35m", "\x1B[39m"),
  41. cyan: f("\x1B[36m", "\x1B[39m"),
  42. white: f("\x1B[37m", "\x1B[39m"),
  43. gray: f("\x1B[90m", "\x1B[39m"),
  44. bgBlack: f("\x1B[40m", "\x1B[49m"),
  45. bgRed: f("\x1B[41m", "\x1B[49m"),
  46. bgGreen: f("\x1B[42m", "\x1B[49m"),
  47. bgYellow: f("\x1B[43m", "\x1B[49m"),
  48. bgBlue: f("\x1B[44m", "\x1B[49m"),
  49. bgMagenta: f("\x1B[45m", "\x1B[49m"),
  50. bgCyan: f("\x1B[46m", "\x1B[49m"),
  51. bgWhite: f("\x1B[47m", "\x1B[49m"),
  52. blackBright: f("\x1B[90m", "\x1B[39m"),
  53. redBright: f("\x1B[91m", "\x1B[39m"),
  54. greenBright: f("\x1B[92m", "\x1B[39m"),
  55. yellowBright: f("\x1B[93m", "\x1B[39m"),
  56. blueBright: f("\x1B[94m", "\x1B[39m"),
  57. magentaBright: f("\x1B[95m", "\x1B[39m"),
  58. cyanBright: f("\x1B[96m", "\x1B[39m"),
  59. whiteBright: f("\x1B[97m", "\x1B[39m"),
  60. bgBlackBright: f("\x1B[100m", "\x1B[49m"),
  61. bgRedBright: f("\x1B[101m", "\x1B[49m"),
  62. bgGreenBright: f("\x1B[102m", "\x1B[49m"),
  63. bgYellowBright: f("\x1B[103m", "\x1B[49m"),
  64. bgBlueBright: f("\x1B[104m", "\x1B[49m"),
  65. bgMagentaBright: f("\x1B[105m", "\x1B[49m"),
  66. bgCyanBright: f("\x1B[106m", "\x1B[49m"),
  67. bgWhiteBright: f("\x1B[107m", "\x1B[49m")
  68. };
  69. };
  70. module.exports = createColors();
  71. module.exports.createColors = createColors;
  72. }));
  73. //#endregion
  74. //#region src/node/constants.ts
  75. const { version } = JSON.parse(readFileSync(new URL("../../package.json", new URL("../../../src/node/constants.ts", import.meta.url))).toString());
  76. const ROLLUP_HOOKS = [
  77. "options",
  78. "buildStart",
  79. "buildEnd",
  80. "renderStart",
  81. "renderError",
  82. "renderChunk",
  83. "writeBundle",
  84. "generateBundle",
  85. "banner",
  86. "footer",
  87. "augmentChunkHash",
  88. "outputOptions",
  89. "renderDynamicImport",
  90. "resolveFileUrl",
  91. "resolveImportMeta",
  92. "intro",
  93. "outro",
  94. "closeBundle",
  95. "closeWatcher",
  96. "load",
  97. "moduleParsed",
  98. "watchChange",
  99. "resolveDynamicImport",
  100. "resolveId",
  101. "shouldTransformCachedModule",
  102. "transform",
  103. "onLog"
  104. ];
  105. const VERSION = version;
  106. const DEFAULT_MAIN_FIELDS = [
  107. "browser",
  108. "module",
  109. "jsnext:main",
  110. "jsnext"
  111. ];
  112. const DEFAULT_CLIENT_MAIN_FIELDS = Object.freeze(DEFAULT_MAIN_FIELDS);
  113. const DEFAULT_SERVER_MAIN_FIELDS = Object.freeze(DEFAULT_MAIN_FIELDS.filter((f) => f !== "browser"));
  114. /**
  115. * A special condition that would be replaced with production or development
  116. * depending on NODE_ENV env variable
  117. */
  118. const DEV_PROD_CONDITION = `development|production`;
  119. const DEFAULT_CONDITIONS = [
  120. "module",
  121. "browser",
  122. "node",
  123. DEV_PROD_CONDITION
  124. ];
  125. const DEFAULT_CLIENT_CONDITIONS = Object.freeze(DEFAULT_CONDITIONS.filter((c) => c !== "node"));
  126. const DEFAULT_SERVER_CONDITIONS = Object.freeze(DEFAULT_CONDITIONS.filter((c) => c !== "browser"));
  127. const DEFAULT_EXTERNAL_CONDITIONS = Object.freeze(["node", "module-sync"]);
  128. const DEFAULT_EXTENSIONS = [
  129. ".mjs",
  130. ".js",
  131. ".mts",
  132. ".ts",
  133. ".jsx",
  134. ".tsx",
  135. ".json"
  136. ];
  137. /**
  138. * The browser versions that are included in the Baseline Widely Available on 2025-05-01.
  139. *
  140. * This value would be bumped on each major release of Vite.
  141. *
  142. * The value is generated by `pnpm generate-target` script.
  143. */
  144. const ESBUILD_BASELINE_WIDELY_AVAILABLE_TARGET = [
  145. "chrome107",
  146. "edge107",
  147. "firefox104",
  148. "safari16"
  149. ];
  150. const DEFAULT_CONFIG_FILES = [
  151. "vite.config.js",
  152. "vite.config.mjs",
  153. "vite.config.ts",
  154. "vite.config.cjs",
  155. "vite.config.mts",
  156. "vite.config.cts"
  157. ];
  158. const JS_TYPES_RE = /\.(?:j|t)sx?$|\.mjs$/;
  159. const CSS_LANGS_RE = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;
  160. const OPTIMIZABLE_ENTRY_RE = /\.[cm]?[jt]s$/;
  161. const SPECIAL_QUERY_RE = /[?&](?:worker|sharedworker|raw|url)\b/;
  162. /**
  163. * Prefix for resolved fs paths, since windows paths may not be valid as URLs.
  164. */
  165. const FS_PREFIX = `/@fs/`;
  166. const CLIENT_PUBLIC_PATH = `/@vite/client`;
  167. const ENV_PUBLIC_PATH = `/@vite/env`;
  168. const VITE_PACKAGE_DIR = resolve(fileURLToPath(new URL("../../../src/node/constants.ts", import.meta.url)), "../../..");
  169. const CLIENT_ENTRY = resolve(VITE_PACKAGE_DIR, "dist/client/client.mjs");
  170. const ENV_ENTRY = resolve(VITE_PACKAGE_DIR, "dist/client/env.mjs");
  171. const CLIENT_DIR = path.dirname(CLIENT_ENTRY);
  172. const KNOWN_ASSET_TYPES = [
  173. "apng",
  174. "bmp",
  175. "png",
  176. "jpe?g",
  177. "jfif",
  178. "pjpeg",
  179. "pjp",
  180. "gif",
  181. "svg",
  182. "ico",
  183. "webp",
  184. "avif",
  185. "cur",
  186. "jxl",
  187. "mp4",
  188. "webm",
  189. "ogg",
  190. "mp3",
  191. "wav",
  192. "flac",
  193. "aac",
  194. "opus",
  195. "mov",
  196. "m4a",
  197. "vtt",
  198. "woff2?",
  199. "eot",
  200. "ttf",
  201. "otf",
  202. "webmanifest",
  203. "pdf",
  204. "txt"
  205. ];
  206. const DEFAULT_ASSETS_RE = new RegExp(`\\.(` + KNOWN_ASSET_TYPES.join("|") + `)(\\?.*)?$`, "i");
  207. const DEP_VERSION_RE = /[?&](v=[\w.-]+)\b/;
  208. const loopbackHosts = new Set([
  209. "localhost",
  210. "127.0.0.1",
  211. "::1",
  212. "0000:0000:0000:0000:0000:0000:0000:0001"
  213. ]);
  214. const wildcardHosts = new Set([
  215. "0.0.0.0",
  216. "::",
  217. "0000:0000:0000:0000:0000:0000:0000:0000"
  218. ]);
  219. const DEFAULT_DEV_PORT = 5173;
  220. const DEFAULT_PREVIEW_PORT = 4173;
  221. const DEFAULT_ASSETS_INLINE_LIMIT = 4096;
  222. const defaultAllowedOrigins = /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/;
  223. const METADATA_FILENAME = "_metadata.json";
  224. const ERR_OPTIMIZE_DEPS_PROCESSING_ERROR = "ERR_OPTIMIZE_DEPS_PROCESSING_ERROR";
  225. const ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR = "ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR";
  226. //#endregion
  227. //#region src/node/logger.ts
  228. var import_picocolors = /* @__PURE__ */ __toESM(require_picocolors(), 1);
  229. const LogLevels = {
  230. silent: 0,
  231. error: 1,
  232. warn: 2,
  233. info: 3
  234. };
  235. let lastType;
  236. let lastMsg;
  237. let sameCount = 0;
  238. function clearScreen() {
  239. const repeatCount = process.stdout.rows - 2;
  240. const blank = repeatCount > 0 ? "\n".repeat(repeatCount) : "";
  241. console.log(blank);
  242. readline.cursorTo(process.stdout, 0, 0);
  243. readline.clearScreenDown(process.stdout);
  244. }
  245. let timeFormatter;
  246. function getTimeFormatter() {
  247. timeFormatter ??= new Intl.DateTimeFormat(void 0, {
  248. hour: "numeric",
  249. minute: "numeric",
  250. second: "numeric"
  251. });
  252. return timeFormatter;
  253. }
  254. function createLogger(level = "info", options = {}) {
  255. if (options.customLogger) return options.customLogger;
  256. const loggedErrors = /* @__PURE__ */ new WeakSet();
  257. const { prefix = "[vite]", allowClearScreen = true, console: console$1 = globalThis.console } = options;
  258. const thresh = LogLevels[level];
  259. const canClearScreen = allowClearScreen && process.stdout.isTTY && !process.env.CI;
  260. const clear = canClearScreen ? clearScreen : () => {};
  261. function format(type, msg, options$1 = {}) {
  262. if (options$1.timestamp) {
  263. let tag = "";
  264. if (type === "info") tag = import_picocolors.default.cyan(import_picocolors.default.bold(prefix));
  265. else if (type === "warn") tag = import_picocolors.default.yellow(import_picocolors.default.bold(prefix));
  266. else tag = import_picocolors.default.red(import_picocolors.default.bold(prefix));
  267. const environment = options$1.environment ? options$1.environment + " " : "";
  268. return `${import_picocolors.default.dim(getTimeFormatter().format(/* @__PURE__ */ new Date()))} ${tag} ${environment}${msg}`;
  269. } else return msg;
  270. }
  271. function output(type, msg, options$1 = {}) {
  272. if (thresh >= LogLevels[type]) {
  273. const method = type === "info" ? "log" : type;
  274. if (options$1.error) loggedErrors.add(options$1.error);
  275. if (canClearScreen) if (type === lastType && msg === lastMsg) {
  276. sameCount++;
  277. clear();
  278. console$1[method](format(type, msg, options$1), import_picocolors.default.yellow(`(x${sameCount + 1})`));
  279. } else {
  280. sameCount = 0;
  281. lastMsg = msg;
  282. lastType = type;
  283. if (options$1.clear) clear();
  284. console$1[method](format(type, msg, options$1));
  285. }
  286. else console$1[method](format(type, msg, options$1));
  287. }
  288. }
  289. const warnedMessages = /* @__PURE__ */ new Set();
  290. const logger = {
  291. hasWarned: false,
  292. info(msg, opts) {
  293. output("info", msg, opts);
  294. },
  295. warn(msg, opts) {
  296. logger.hasWarned = true;
  297. output("warn", msg, opts);
  298. },
  299. warnOnce(msg, opts) {
  300. if (warnedMessages.has(msg)) return;
  301. logger.hasWarned = true;
  302. output("warn", msg, opts);
  303. warnedMessages.add(msg);
  304. },
  305. error(msg, opts) {
  306. logger.hasWarned = true;
  307. output("error", msg, opts);
  308. },
  309. clearScreen(type) {
  310. if (thresh >= LogLevels[type]) clear();
  311. },
  312. hasErrorLogged(error) {
  313. return loggedErrors.has(error);
  314. }
  315. };
  316. return logger;
  317. }
  318. function printServerUrls(urls, optionsHost, info) {
  319. const colorUrl = (url) => import_picocolors.default.cyan(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
  320. for (const url of urls.local) info(` ${import_picocolors.default.green("➜")} ${import_picocolors.default.bold("Local")}: ${colorUrl(url)}`);
  321. for (const url of urls.network) info(` ${import_picocolors.default.green("➜")} ${import_picocolors.default.bold("Network")}: ${colorUrl(url)}`);
  322. if (urls.network.length === 0 && optionsHost === void 0) info(import_picocolors.default.dim(` ${import_picocolors.default.green("➜")} ${import_picocolors.default.bold("Network")}: use `) + import_picocolors.default.bold("--host") + import_picocolors.default.dim(" to expose"));
  323. }
  324. //#endregion
  325. export { OPTIMIZABLE_ENTRY_RE as A, ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR as C, JS_TYPES_RE as D, FS_PREFIX as E, defaultAllowedOrigins as F, loopbackHosts as I, wildcardHosts as L, SPECIAL_QUERY_RE as M, VERSION as N, KNOWN_ASSET_TYPES as O, VITE_PACKAGE_DIR as P, require_picocolors as R, ENV_PUBLIC_PATH as S, ESBUILD_BASELINE_WIDELY_AVAILABLE_TARGET as T, DEFAULT_SERVER_CONDITIONS as _, CLIENT_ENTRY as a, DEV_PROD_CONDITION as b, DEFAULT_ASSETS_INLINE_LIMIT as c, DEFAULT_CLIENT_MAIN_FIELDS as d, DEFAULT_CONFIG_FILES as f, DEFAULT_PREVIEW_PORT as g, DEFAULT_EXTERNAL_CONDITIONS as h, CLIENT_DIR as i, ROLLUP_HOOKS as j, METADATA_FILENAME as k, DEFAULT_ASSETS_RE as l, DEFAULT_EXTENSIONS as m, createLogger as n, CLIENT_PUBLIC_PATH as o, DEFAULT_DEV_PORT as p, printServerUrls as r, CSS_LANGS_RE as s, LogLevels as t, DEFAULT_CLIENT_CONDITIONS as u, DEFAULT_SERVER_MAIN_FIELDS as v, ERR_OPTIMIZE_DEPS_PROCESSING_ERROR as w, ENV_ENTRY as x, DEP_VERSION_RE as y };