Presupuestador_web/client/vite.config.ts

96 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-06-06 11:05:54 +00:00
import react from "@vitejs/plugin-react";
2025-04-16 09:10:22 +00:00
import { readFileSync } from "fs";
2024-08-18 20:39:06 +00:00
import { createRequire } from "node:module";
2026-10-13 16:20:06 +00:00
import { randomUUID } from "node:crypto";
2024-08-18 20:39:06 +00:00
import path from "node:path";
import { defineConfig, normalizePath } from "vite";
2024-12-10 15:41:58 +00:00
import { robots } from "vite-plugin-robots";
2024-08-18 20:39:06 +00:00
import { viteStaticCopy } from "vite-plugin-static-copy";
2024-06-06 11:05:54 +00:00
2025-04-16 09:10:22 +00:00
const pkg = JSON.parse(readFileSync("./package.json", "utf-8"));
2024-08-18 20:39:06 +00:00
const require = createRequire(import.meta.url);
2026-10-13 16:20:06 +00:00
const buildId = randomUUID();
2026-10-14 10:00:19 +00:00
const htmlTranslations: Array<{
language: string;
labels: { title: string; javascript_required: string };
}> = ["es", "en", "fr", "ca"].map((language) => ({
language,
labels: JSON.parse(readFileSync(`./src/locales/${language}.json`, "utf8")).translation.app,
}));
const escapeHtml = (value: string) =>
value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
2024-08-18 20:39:06 +00:00
const pdfjsDistPath = path.dirname(require.resolve("pdfjs-dist/package.json"));
const cMapsDir = normalizePath(path.join(pdfjsDistPath, "cmaps"));
2024-06-06 11:05:54 +00:00
const standardFontsDir = normalizePath(
2024-08-22 17:23:12 +00:00
path.join(path.dirname(require.resolve("pdfjs-dist/package.json")), "standard_fonts")
);
2024-06-06 11:05:54 +00:00
export default defineConfig({
2025-04-16 09:10:22 +00:00
define: {
__APP_VERSION__: JSON.stringify(pkg.version),
2026-10-13 16:20:06 +00:00
__APP_BUILD_ID__: JSON.stringify(buildId),
2025-04-16 09:10:22 +00:00
},
2024-10-01 15:50:07 +00:00
build: {
outDir: "../dist/client",
emptyOutDir: true,
},
2024-08-18 20:39:06 +00:00
plugins: [
2026-10-14 10:00:19 +00:00
{
name: "localized-html",
transformIndexHtml(html) {
return html
.replace("<!-- app-title -->", escapeHtml(htmlTranslations[0].labels.title))
.replace(
"<!-- app-noscript -->",
htmlTranslations
.map(({ language, labels }) =>
`<p lang="${language}">${escapeHtml(labels.javascript_required)}</p>`
)
.join("\n")
);
},
},
2026-10-13 16:20:06 +00:00
{
name: "app-build-version",
apply: "build",
generateBundle() {
this.emitFile({
type: "asset",
fileName: "version.json",
source: JSON.stringify({ buildId }),
});
},
},
2024-08-18 20:39:06 +00:00
react(),
2024-12-10 15:41:58 +00:00
robots({}),
2025-04-16 12:16:08 +00:00
2024-08-18 20:39:06 +00:00
viteStaticCopy({
targets: [
{
src: cMapsDir,
dest: "",
},
2024-08-22 17:23:12 +00:00
{
src: standardFontsDir,
dest: "",
},
2024-08-18 20:39:06 +00:00
],
}),
],
2024-06-06 11:05:54 +00:00
css: { postcss: "./postcss.config.js" },
resolve: {
alias: [
{
find: "@",
2024-08-18 20:39:06 +00:00
replacement: path.resolve(__dirname, "./src"),
2024-06-06 11:05:54 +00:00
},
{
find: "@shared",
2024-08-18 20:39:06 +00:00
replacement: path.resolve(__dirname, "../shared/lib/"),
2024-06-06 11:05:54 +00:00
},
],
},
});