|
| 1 | +import { promises as fs } from 'node:fs'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import type { Plugin } from 'vite'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Recursively find all .tsx files in a directory |
| 7 | + */ |
| 8 | +async function findTsxFiles(dir: string): Promise<string[]> { |
| 9 | + const files: string[] = []; |
| 10 | + const entries = await fs.readdir(dir, { withFileTypes: true }); |
| 11 | + |
| 12 | + for (const entry of entries) { |
| 13 | + const fullPath = join(dir, entry.name); |
| 14 | + if (entry.isDirectory()) { |
| 15 | + files.push(...(await findTsxFiles(fullPath))); |
| 16 | + } else if (entry.isFile() && entry.name.endsWith('.tsx')) { |
| 17 | + files.push(fullPath); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + return files; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Vite plugin to validate that image files referenced in route meta tags exist. |
| 26 | + * |
| 27 | + * This plugin scans all route files for og:image and twitter:image meta tags, |
| 28 | + * extracts the image paths, and verifies that the corresponding files exist |
| 29 | + * in the public directory. If any images are missing, the build fails with |
| 30 | + * a clear error message. |
| 31 | + */ |
| 32 | +export function validateRouteImages(): Plugin { |
| 33 | + return { |
| 34 | + name: 'validate-route-images', |
| 35 | + async buildStart() { |
| 36 | + const publicDir = join(process.cwd(), 'public'); |
| 37 | + const routesDir = join(process.cwd(), 'src', 'routes'); |
| 38 | + |
| 39 | + // Find all route files |
| 40 | + const routeFiles = await findTsxFiles(routesDir); |
| 41 | + |
| 42 | + const missingImagesByPath = new Map<string, Set<string>>(); |
| 43 | + const imageRegex = |
| 44 | + /(?:property|name):\s*['"](?:og:image|twitter:image)['"]\s*,\s*content:\s*['"](\/images\/[^'"]+)['"]/g; |
| 45 | + |
| 46 | + for (const routeFile of routeFiles) { |
| 47 | + const content = await fs.readFile(routeFile, 'utf-8'); |
| 48 | + |
| 49 | + // Find all image references in this file |
| 50 | + const imagesInFile = new Set<string>(); |
| 51 | + let match; |
| 52 | + while ((match = imageRegex.exec(content)) !== null) { |
| 53 | + const imagePath = match[1]; // e.g., '/images/ethereum/forks.png' |
| 54 | + imagesInFile.add(imagePath); |
| 55 | + } |
| 56 | + |
| 57 | + // Check each unique image in this file |
| 58 | + for (const imagePath of imagesInFile) { |
| 59 | + const fullImagePath = join(publicDir, imagePath); |
| 60 | + |
| 61 | + try { |
| 62 | + await fs.access(fullImagePath); |
| 63 | + } catch { |
| 64 | + // Image doesn't exist |
| 65 | + const relativeRoutePath = routeFile.replace(process.cwd() + '/', ''); |
| 66 | + if (!missingImagesByPath.has(imagePath)) { |
| 67 | + missingImagesByPath.set(imagePath, new Set()); |
| 68 | + } |
| 69 | + missingImagesByPath.get(imagePath)!.add(relativeRoutePath); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (missingImagesByPath.size > 0) { |
| 75 | + const errorLines = ['\n❌ Route image validation failed!\n']; |
| 76 | + errorLines.push('The following images are referenced but do not exist:\n'); |
| 77 | + |
| 78 | + for (const [imagePath, files] of missingImagesByPath) { |
| 79 | + errorLines.push(`\n Missing: ${imagePath}`); |
| 80 | + errorLines.push(' Referenced in:'); |
| 81 | + for (const file of files) { |
| 82 | + errorLines.push(` • ${file}`); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + errorLines.push('\nPlease create the missing images in the public directory or remove the references.\n'); |
| 87 | + |
| 88 | + throw new Error(errorLines.join('\n')); |
| 89 | + } |
| 90 | + |
| 91 | + console.log('✅ All route images validated successfully'); |
| 92 | + }, |
| 93 | + }; |
| 94 | +} |
0 commit comments