import { defineConfig } from 'vite'; import { resolve } from 'path'; import fs from 'fs'; import { createRequire } from 'module'; const require = createRequire(import.meta.url); function getHtmlEntries() { const entries = { main: resolve(__dirname, 'index.html') }; const rootDirs = fs.readdirSync(__dirname); for (const dir of rootDirs) { if (['.git', '.github', 'node_modules', 'dist'].includes(dir)) continue; const fullPath = resolve(__dirname, dir); if (!fs.statSync(fullPath).isDirectory()) continue; // Check candidate HTML entrypoints const candidates = [ 'index.html', 'docs/index.html', 'public/index.html', 'ui.html', 'web/index.html' ]; for (const cand of candidates) { const candPath = resolve(fullPath, cand); if (fs.existsSync(candPath)) { const entryKey = cand === 'index.html' ? dir : `${dir}_${cand.replace('/', '_').replace('.html', '')}`; entries[entryKey] = candPath; break; // Use the first found matching entrypoint } } } console.log('Vite MPA Detected Entries:', entries); return entries; } // Vite plugin to rewrite HTML absolute assets to be subfolder-scoped const mpaHtmlPlugin = () => { return { name: 'mpa-html-plugin', transformIndexHtml: { order: 'pre', handler(html, ctx) { const parts = ctx.path.split('/').filter(Boolean); if (parts.length > 1) { const subfolder = parts[0]; let transformed = html // Rewrite absolute src/href paths like "/src/main.ts" to "/subfolder/src/main.ts" .replace(/(src|href)="\/([^"]+)"/g, (match, attr, path) => { if (path.startsWith('http://') || path.startsWith('https://')) { return match; } if (path.startsWith(subfolder)) { return match; } return `${attr}="/${subfolder}/${path}"`; }); // Clean up platform-specific non-module scripts like capacitor.js which fail Vite build transformed = transformed.replace(/