Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
vite_tags
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
6.06
1<?php
2
3if (! function_exists('vite_tags')) {
4    /**
5     * @param list<string> $entries
6     */
7    function vite_tags(array $entries = ['resources/js/app.js']): string
8    {
9        $manifestPath = ROOTPATH . 'public/build/.vite/manifest.json';
10
11        if (! is_file($manifestPath)) {
12            return '<!-- Vite manifest missing. Run npm run build. -->';
13        }
14
15        /** @var array<string, array<string, mixed>>|null $manifest */
16        static $manifest = null;
17        if ($manifest === null) {
18            $manifest = json_decode((string) file_get_contents($manifestPath), true);
19        }
20
21        $html = [];
22
23        foreach ($entries as $entry) {
24            if (! isset($manifest[$entry])) {
25                continue;
26            }
27
28            $item = $manifest[$entry];
29
30            foreach ($item['css'] ?? [] as $css) {
31                $href   = base_url('build/' . ltrim($css, '/'));
32                $html[] = '<link rel="stylesheet" href="' . esc($href, 'attr') . '">';
33            }
34
35            $src    = base_url('build/' . ltrim($item['file'], '/'));
36            $html[] = '<script type="module" src="' . esc($src, 'attr') . '"></script>';
37        }
38
39        return implode(PHP_EOL, array_unique($html));
40    }
41}