fix: plugin para corregir referencias en index.html con prefijos numéricos

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
This commit is contained in:
planetazuzu 2026-01-02 19:54:36 +01:00
parent 2bda0663f3
commit 759b65c495
2 changed files with 48 additions and 0 deletions

View file

@ -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);
}
}
};
}

View file

@ -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: {