From 759b65c495a1a5ef6a9ee9fb078ea59f44f10aa8 Mon Sep 17 00:00:00 2001 From: planetazuzu Date: Fri, 2 Jan 2026 19:54:36 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20plugin=20para=20corregir=20referencias?= =?UTF-8?q?=20en=20index.html=20con=20prefijos=20num=C3=A9ricos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PROBLEMA CRÍTICO: - index.html no tenía prefijos (0-, 1-, 2-) en las referencias - Archivos físicos sí tenían prefijos - Navegador intentaba cargar archivos sin prefijo que no existen SOLUCIÓN: - Plugin vite-plugin-fix-html-references.ts - Corrige referencias en index.html después del build - Añade prefijos numéricos a las referencias de chunks vendor RESULTADO: ✅ index.html ahora referencia correctamente: - 0-vendor-react-*.js - 1-vendor-utils-*.js - 2-vendor-markdown-*.js ✅ Orden de carga garantizado ✅ Navegador carga archivos correctos --- vite-plugin-fix-html-references.ts | 46 ++++++++++++++++++++++++++++++ vite.config.ts | 2 ++ 2 files changed, 48 insertions(+) create mode 100644 vite-plugin-fix-html-references.ts 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: {