71 lines
2.2 KiB
JavaScript
71 lines
2.2 KiB
JavaScript
// public/bootstrap.js
|
|
(function() {
|
|
// --- CONFIGURACIÓN ---
|
|
// Cambie esto por el dominio final en producción
|
|
const WIDGETS_HOST = 'https://elecciones2025.eldia.com';
|
|
|
|
// --- FUNCIONES AUXILIARES (sin cambios) ---
|
|
function loadScript(src) {
|
|
return new Promise((resolve, reject) => {
|
|
const script = document.createElement('script');
|
|
script.type = 'module';
|
|
script.src = src;
|
|
script.onload = resolve;
|
|
script.onerror = reject;
|
|
document.head.appendChild(script);
|
|
});
|
|
}
|
|
|
|
function loadCSS(href) {
|
|
const link = document.createElement('link');
|
|
link.rel = 'stylesheet';
|
|
link.href = href;
|
|
document.head.appendChild(link);
|
|
}
|
|
|
|
// --- LÓGICA PRINCIPAL ---
|
|
async function initWidgets() {
|
|
try {
|
|
// 1. Obtener el manifest.json
|
|
const response = await fetch(`${WIDGETS_HOST}/manifest.json`);
|
|
if (!response.ok) throw new Error('No se pudo cargar el manifest.');
|
|
const manifest = await response.json();
|
|
|
|
// 2. Encontrar el punto de entrada principal (nuestro main.tsx)
|
|
// En modo 'lib', la entrada es el propio archivo de entrada.
|
|
const entryKey = 'src/main.tsx';
|
|
const entry = manifest[entryKey];
|
|
|
|
if (!entry) throw new Error('No se encontró el punto de entrada en el manifest.');
|
|
|
|
const jsUrl = `${WIDGETS_HOST}/${entry.file}`;
|
|
|
|
// 3. Cargar el CSS asociado, si existe
|
|
if (entry.css && entry.css.length > 0) {
|
|
entry.css.forEach(cssFile => {
|
|
const cssUrl = `${WIDGETS_HOST}/${cssFile}`;
|
|
loadCSS(cssUrl);
|
|
});
|
|
}
|
|
|
|
// 4. Cargar el script principal de la librería
|
|
await loadScript(jsUrl);
|
|
|
|
// 5. Esperar a que la página esté completamente cargada
|
|
// y luego llamar a la función de renderizado.
|
|
window.addEventListener('load', function () {
|
|
if (window.EleccionesWidgets && typeof window.EleccionesWidgets.render === 'function') {
|
|
console.log('Elecciones Widgets listos. Renderizando...');
|
|
window.EleccionesWidgets.render();
|
|
}
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Error al inicializar los widgets de Elecciones:', error);
|
|
}
|
|
}
|
|
|
|
// Iniciar el proceso
|
|
initWidgets();
|
|
|
|
})(); |