|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Application\Twig; |
| 6 | + |
| 7 | +use Twig\Extension\AbstractExtension; |
| 8 | +use Twig\TwigFunction; |
| 9 | + |
| 10 | +final class ViteExtension extends AbstractExtension |
| 11 | +{ |
| 12 | + private ?array $manifest = null; |
| 13 | + |
| 14 | + private ?int $manifestMtime = null; |
| 15 | + |
| 16 | + public function __construct( |
| 17 | + private readonly string $publicPath, |
| 18 | + private readonly bool $isDev, |
| 19 | + ) { |
| 20 | + } |
| 21 | + |
| 22 | + public function getFunctions(): array |
| 23 | + { |
| 24 | + return [ |
| 25 | + new TwigFunction('vite', [$this, 'vite'], ['is_safe' => ['html']]), |
| 26 | + ]; |
| 27 | + } |
| 28 | + |
| 29 | + public function vite(string ...$entries): string |
| 30 | + { |
| 31 | + if ($this->isDev) { |
| 32 | + return $this->devTags($entries); |
| 33 | + } |
| 34 | + |
| 35 | + return $this->prodTags($entries); |
| 36 | + } |
| 37 | + |
| 38 | + private function devTags(array $entries): string |
| 39 | + { |
| 40 | + $html = '<script type="module" src="http://localhost:5173/@vite/client"></script>' . "\n"; |
| 41 | + |
| 42 | + foreach ($entries as $entry) { |
| 43 | + $html .= '<script type="module" src="http://localhost:5173/' . $entry . '"></script>' . "\n"; |
| 44 | + } |
| 45 | + |
| 46 | + return $html; |
| 47 | + } |
| 48 | + |
| 49 | + private function prodTags(array $entries): string |
| 50 | + { |
| 51 | + $manifest = $this->loadManifest(); |
| 52 | + $html = ''; |
| 53 | + $emitted = []; |
| 54 | + |
| 55 | + foreach ($entries as $entry) { |
| 56 | + if (!isset($manifest[$entry])) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + $html .= $this->emitChunk($manifest[$entry], $manifest, $emitted); |
| 61 | + } |
| 62 | + |
| 63 | + return $html; |
| 64 | + } |
| 65 | + |
| 66 | + private function emitChunk(array $chunk, array $manifest, array &$emitted): string |
| 67 | + { |
| 68 | + $html = ''; |
| 69 | + |
| 70 | + // Emit shared imports first (e.g. alerts.js) |
| 71 | + foreach ($chunk['imports'] ?? [] as $importKey) { |
| 72 | + if (isset($emitted[$importKey]) || !isset($manifest[$importKey])) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + $emitted[$importKey] = true; |
| 77 | + $html .= $this->emitChunk($manifest[$importKey], $manifest, $emitted); |
| 78 | + } |
| 79 | + |
| 80 | + // Emit CSS files |
| 81 | + foreach ($chunk['css'] ?? [] as $cssFile) { |
| 82 | + if (isset($emitted[$cssFile])) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + $html .= '<link rel="stylesheet" href="/build/' . $cssFile . '">' . "\n"; |
| 87 | + $emitted[$cssFile] = true; |
| 88 | + } |
| 89 | + |
| 90 | + // Emit the JS file |
| 91 | + if (isset($chunk['file']) && !isset($emitted[$chunk['file']])) { |
| 92 | + $html .= '<script type="module" src="/build/' . $chunk['file'] . '"></script>' . "\n"; |
| 93 | + $emitted[$chunk['file']] = true; |
| 94 | + } |
| 95 | + |
| 96 | + return $html; |
| 97 | + } |
| 98 | + |
| 99 | + private function loadManifest(): array |
| 100 | + { |
| 101 | + $path = $this->publicPath . '/build/.vite/manifest.json'; |
| 102 | + |
| 103 | + if (!file_exists($path)) { |
| 104 | + $this->manifestMtime = null; |
| 105 | + return $this->manifest = []; |
| 106 | + } |
| 107 | + |
| 108 | + $mtime = filemtime($path); |
| 109 | + if ($this->manifest !== null && $this->manifestMtime === $mtime) { |
| 110 | + return $this->manifest; |
| 111 | + } |
| 112 | + |
| 113 | + $this->manifestMtime = $mtime === false ? null : $mtime; |
| 114 | + $this->manifest = json_decode(file_get_contents($path), true) ?: []; |
| 115 | + |
| 116 | + return $this->manifest; |
| 117 | + } |
| 118 | +} |
0 commit comments