From 83a3ba8cd7a061ba1a87bf57b25254ea5f9b8417 Mon Sep 17 00:00:00 2001 From: Gorniaky Date: Sat, 8 Aug 2026 11:57:24 -0300 Subject: [PATCH] refactor: improve versionInjector and error handling in esbuild plugin --- esbuild.mjs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/esbuild.mjs b/esbuild.mjs index 7de98f26..9dc0105d 100644 --- a/esbuild.mjs +++ b/esbuild.mjs @@ -2,14 +2,15 @@ import { context } from "esbuild"; import { readFileSync } from "fs"; import Replace from "unplugin-replace/esbuild"; -export function versionInjector() { +function versionInjector() { + /** @type {import("type-fest").PackageJson} */ + let pkg; return Replace({ include: [/\.(js|ts)$/], values: [{ find: "__PACKAGE_VERSION__", replacement() { - /** @type {import("type-fest").PackageJson} */ - const pkg = JSON.parse(readFileSync("./package.json", "utf8")); + pkg ??= JSON.parse(readFileSync("./package.json", "utf8")); return pkg.version; }, }], @@ -24,12 +25,26 @@ const esbuildProblemMatcherPlugin = { build.onStart(() => console.log("[watch] build started")); build.onEnd((result) => { - for (let i = 0; i < result.errors.length; i++) { - const error = result.errors[i]; + if (result.warnings.length) { + const messages = [], params = []; - console.error("✘ [ERROR] %s", error.text); + for (const m of result.warnings) { + messages.push("⚠ [WARN] %s\n %s:%s:%s:"); + params.push(m.text, m.location.file, m.location.line, m.location.column); + } - console.error(" %s:%s:%s:", error.location.file, error.location.line, error.location.column); + console.warn(messages.join("\n\n"), ...params); + } + + if (result.errors.length) { + const messages = [], params = []; + + for (const m of result.errors) { + messages.push("✘ [ERROR] %s\n %s:%s:%s:"); + params.push(m.text, m.location.file, m.location.line, m.location.column); + } + + console.error(messages.join("\n\n"), ...params); } console.log("[watch] build finished");