codigo0/vite-plugin-fix-html-references.ts
planetazuzu 759b65c495 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
2026-01-02 19:54:36 +01:00

47 lines
1.6 KiB
TypeScript

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