astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
85 lines (84 loc) • 3.29 kB
JavaScript
import { fileURLToPath } from "node:url";
import { stripVTControlCharacters } from "node:util";
import { isAstroError } from "../errors/errors.js";
import { serverShortcuts as formatServerShortcuts } from "../messages/runtime.js";
import { isLogLevelEnabled } from "./core.js";
const PKG_PREFIX = fileURLToPath(new URL("../../../", import.meta.url));
const E2E_PREFIX = fileURLToPath(new URL("../../../e2e", import.meta.url));
function isAstroSrcFile(id) {
return id?.startsWith(PKG_PREFIX) && !id.startsWith(E2E_PREFIX);
}
// capture "page reload some/Component.vue (additional info)" messages
const vitePageReloadMsg = /page reload (.*)/;
// capture "hmr update some/Component.vue" messages
const viteHmrUpdateMsg = /hmr update (.*)/;
// capture "vite v5.0.0 building SSR bundle for production..." and "vite v5.0.0 building for production..." messages
const viteBuildMsg = /vite.*building.*for production/;
// capture "\n Shortcuts" messages
const viteShortcutTitleMsg = /^\s*Shortcuts\s*$/;
// capture "press * + enter to ..." messages
const viteShortcutHelpMsg = /press (.+?) to (.+)$/s;
// 'global' is not recognized as a valid pseudo-class
const lightningcssUnsupportedPseudoMsg = /\[lightningcss\] 'global'.*not recognized.*pseudo-class/s;
export function createViteLogger(astroLogger, viteLogLevel = "info") {
const warnedMessages = new Set();
const loggedErrors = new WeakSet();
const logger = {
hasWarned: false,
info(msg) {
if (!isLogLevelEnabled(viteLogLevel, "info")) return;
const stripped = stripVTControlCharacters(msg);
let m;
// Rewrite HMR page reload message
if (m = vitePageReloadMsg.exec(stripped)) {
if (isAstroSrcFile(m[1])) return;
astroLogger.info("watch", m[1]);
} else if (m = viteHmrUpdateMsg.exec(stripped)) {
if (isAstroSrcFile(m[1])) return;
astroLogger.info("watch", m[1]);
} else if (viteBuildMsg.test(stripped) || viteShortcutTitleMsg.test(stripped)) {} else if (viteShortcutHelpMsg.test(stripped)) {
const [, key, label] = viteShortcutHelpMsg.exec(stripped);
astroLogger.info("SKIP_FORMAT", formatServerShortcuts({
key,
label
}));
} else {
astroLogger.info("vite", msg);
}
},
warn(msg) {
if (!isLogLevelEnabled(viteLogLevel, "warn")) return;
if (lightningcssUnsupportedPseudoMsg.test(msg)) return;
logger.hasWarned = true;
astroLogger.warn("vite", msg);
},
warnOnce(msg) {
if (!isLogLevelEnabled(viteLogLevel, "warn")) return;
if (warnedMessages.has(msg)) return;
logger.hasWarned = true;
astroLogger.warn("vite", msg);
warnedMessages.add(msg);
},
error(msg, opts) {
if (!isLogLevelEnabled(viteLogLevel, "error")) return;
logger.hasWarned = true;
const err = opts?.error;
if (err) loggedErrors.add(err);
// Astro errors are already logged by us, skip logging
if (err && isAstroError(err)) return;
// SSR module and pre-transform errors are always handled by us,
// send to debug logs
if (msg.includes("Error when evaluating SSR module") || msg.includes("Pre-transform error:")) {
astroLogger.debug("vite", msg);
return;
}
astroLogger.error("vite", msg);
},
// Don't allow clear screen
clearScreen: () => {},
hasErrorLogged(error) {
return loggedErrors.has(error);
}
};
return logger;
}