diff --git a/vite-plugin-fix-html-references.ts b/vite-plugin-fix-html-references.ts new file mode 100644 index 00000000..87399b59 --- /dev/null +++ b/vite-plugin-fix-html-references.ts @@ -0,0 +1,46 @@ +import type { Plugin } from 'vite'; +import { readFileSync, writeFileSync } from 'fs'; +import { resolve } from 'path'; + +/** + * Plugin para corregir las referencias en index.html después del build + * Asegura que los chunks con prefijos numéricos (0-, 1-, 2-) se referencien correctamente + */ +export function fixHtmlReferencesPlugin(): Plugin { + return { + name: 'fix-html-references', + writeBundle() { + const distDir = resolve(__dirname, 'dist'); + const indexPath = resolve(distDir, 'index.html'); + + try { + // Leer index.html generado + let htmlContent = readFileSync(indexPath, 'utf-8'); + + // Buscar y reemplazar referencias de chunks vendor sin prefijos + // Patrón: vendor-react-[hash].js -> 0-vendor-react-[hash].js + htmlContent = htmlContent.replace( + /(src|href)="([^"]*\/)vendor-react-([^"]*\.js)"/g, + '$1="$20-vendor-react-$3"' + ); + + htmlContent = htmlContent.replace( + /(src|href)="([^"]*\/)vendor-utils-([^"]*\.js)"/g, + '$1="$21-vendor-utils-$3"' + ); + + htmlContent = htmlContent.replace( + /(src|href)="([^"]*\/)vendor-markdown-([^"]*\.js)"/g, + '$1="$22-vendor-markdown-$3"' + ); + + // Escribir index.html corregido + writeFileSync(indexPath, htmlContent, 'utf-8'); + console.log('[fix-html-references] ✅ Referencias corregidas en index.html'); + } catch (error) { + console.warn('[fix-html-references] ⚠️ No se pudo corregir index.html:', error); + } + } + }; +} + diff --git a/vite.config.ts b/vite.config.ts index eb3f48d1..52a41472 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -2,6 +2,7 @@ import { defineConfig } from "vite"; import react from "@vitejs/plugin-react-swc"; import path from "path"; import { manifestPlugin } from "./vite-plugin-manifest"; +import { fixHtmlReferencesPlugin } from "./vite-plugin-fix-html-references"; // Detectar si estamos en GitHub Pages // GitHub Pages usa el formato: https://username.github.io/repository-name/ @@ -32,6 +33,7 @@ export default defineConfig({ plugins: [ react(), manifestPlugin(), + fixHtmlReferencesPlugin(), // Corregir referencias en index.html después del build ], resolve: { alias: {