|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Foundation; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Illuminate\Support\HtmlString; |
| 7 | +use Illuminate\Support\Str; |
| 8 | + |
| 9 | +class Vite |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Generate Vite tags for an entrypoint. |
| 13 | + * |
| 14 | + * @param string|string[] $entrypoints |
| 15 | + * @param string $buildDirectory |
| 16 | + * @return \Illuminate\Support\HtmlString |
| 17 | + * |
| 18 | + * @throws \Exception |
| 19 | + */ |
| 20 | + public function __invoke($entrypoints, $buildDirectory = 'build') |
| 21 | + { |
| 22 | + static $manifests = []; |
| 23 | + |
| 24 | + $entrypoints = collect($entrypoints); |
| 25 | + $buildDirectory = Str::start($buildDirectory, '/'); |
| 26 | + |
| 27 | + if (is_file(public_path('/hot'))) { |
| 28 | + $url = rtrim(file_get_contents(public_path('/hot'))); |
| 29 | + |
| 30 | + return new HtmlString( |
| 31 | + $entrypoints |
| 32 | + ->map(fn ($entrypoint) => $this->makeTag("{$url}/{$entrypoint}")) |
| 33 | + ->prepend($this->makeScriptTag("{$url}/@vite/client")) |
| 34 | + ->join('') |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + $manifestPath = public_path($buildDirectory.'/manifest.json'); |
| 39 | + |
| 40 | + if (! isset($manifests[$manifestPath])) { |
| 41 | + if (! is_file($manifestPath)) { |
| 42 | + throw new Exception("Vite manifest not found at: {$manifestPath}"); |
| 43 | + } |
| 44 | + |
| 45 | + $manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true); |
| 46 | + } |
| 47 | + |
| 48 | + $manifest = $manifests[$manifestPath]; |
| 49 | + |
| 50 | + $tags = collect(); |
| 51 | + |
| 52 | + foreach ($entrypoints as $entrypoint) { |
| 53 | + if (! isset($manifest[$entrypoint])) { |
| 54 | + throw new Exception("Unable to locate file in Vite manifest: {$entrypoint}."); |
| 55 | + } |
| 56 | + |
| 57 | + $tags->push($this->makeTag(asset("{$buildDirectory}/{$manifest[$entrypoint]['file']}"))); |
| 58 | + |
| 59 | + if (isset($manifest[$entrypoint]['css'])) { |
| 60 | + foreach ($manifest[$entrypoint]['css'] as $css) { |
| 61 | + $tags->push($this->makeStylesheetTag(asset("{$buildDirectory}/{$css}"))); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (isset($manifest[$entrypoint]['imports'])) { |
| 66 | + foreach ($manifest[$entrypoint]['imports'] as $import) { |
| 67 | + if (isset($manifest[$import]['css'])) { |
| 68 | + foreach ($manifest[$import]['css'] as $css) { |
| 69 | + $tags->push($this->makeStylesheetTag(asset("{$buildDirectory}/{$css}"))); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + [$stylesheets, $scripts] = $tags->partition(fn ($tag) => str_starts_with($tag, '<link')); |
| 77 | + |
| 78 | + return new HtmlString($stylesheets->join('').$scripts->join('')); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Generate React refresh runtime script. |
| 83 | + * |
| 84 | + * @return \Illuminate\Support\HtmlString|void |
| 85 | + */ |
| 86 | + public function reactRefresh() |
| 87 | + { |
| 88 | + if (! is_file(public_path('/hot'))) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $url = rtrim(file_get_contents(public_path('/hot'))); |
| 93 | + |
| 94 | + return new HtmlString( |
| 95 | + sprintf( |
| 96 | + <<<'HTML' |
| 97 | + <script type="module"> |
| 98 | + import RefreshRuntime from '%s/@react-refresh' |
| 99 | + RefreshRuntime.injectIntoGlobalHook(window) |
| 100 | + window.$RefreshReg$ = () => {} |
| 101 | + window.$RefreshSig$ = () => (type) => type |
| 102 | + window.__vite_plugin_react_preamble_installed__ = true |
| 103 | + </script> |
| 104 | + HTML, |
| 105 | + $url |
| 106 | + ) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Generate an appropriate tag for the given URL. |
| 112 | + * |
| 113 | + * @param string $url |
| 114 | + * @return string |
| 115 | + */ |
| 116 | + protected function makeTag($url) |
| 117 | + { |
| 118 | + if ($this->isCssPath($url)) { |
| 119 | + return $this->makeStylesheetTag($url); |
| 120 | + } |
| 121 | + |
| 122 | + return $this->makeScriptTag($url); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Generate a script tag for the given URL. |
| 127 | + * |
| 128 | + * @param string $url |
| 129 | + * @return string |
| 130 | + */ |
| 131 | + protected function makeScriptTag($url) |
| 132 | + { |
| 133 | + return sprintf('<script type="module" src="%s"></script>', $url); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Generate a stylesheet tag for the given URL. |
| 138 | + * |
| 139 | + * @param string $url |
| 140 | + * @return string |
| 141 | + */ |
| 142 | + protected function makeStylesheetTag($url) |
| 143 | + { |
| 144 | + return sprintf('<link rel="stylesheet" href="%s" />', $url); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Determine whether the given path is a CSS file. |
| 149 | + * |
| 150 | + * @param string $path |
| 151 | + * @return bool |
| 152 | + */ |
| 153 | + protected function isCssPath($path) |
| 154 | + { |
| 155 | + return preg_match('/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/', $path) === 1; |
| 156 | + } |
| 157 | +} |
0 commit comments