• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / assets / 15128346148

20 May 2025 03:42AM UTC coverage: 95.1% (+0.01%) from 95.089%
15128346148

push

github

dg
DIExtension: parameters are passed by constructor (BC break)

5 of 5 new or added lines in 1 file covered. (100.0%)

9 existing lines in 2 files now uncovered.

427 of 449 relevant lines covered (95.1%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/src/Assets/ViteMapper.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Nette\Assets;
6

7
use Nette\Utils\FileSystem;
8
use Nette\Utils\Json;
9

10

11
/**
12
 * Maps asset references to Vite-generated files using a Vite manifest.json.
13
 * Supports both development mode (Vite dev server) and production mode.
14
 */
15
class ViteMapper implements Mapper
16
{
17
        private array $chunks;
18
        private array $dependencies = [];
19

20

21
        public function __construct(
1✔
22
                private readonly string $baseUrl,
23
                private readonly string $basePath,
24
                private readonly ?string $manifestPath = null,
25
                private readonly ?string $devServer = null,
26
                private readonly ?Mapper $publicMapper = null,
27
        ) {
28
        }
1✔
29

30

31
        /**
32
         * Retrieves an Asset for a given Vite entry point.
33
         * @throws AssetNotFoundException when the asset cannot be found in the manifest
34
         */
35
        public function getAsset(string $reference, array $options = []): Asset
1✔
36
        {
37
                Helpers::checkOptions($options);
1✔
38

39
                $this->chunks ??= $this->readChunks();
1✔
40
                $entry = $this->chunks[$id = $reference] ?? $this->chunks[$id = $this->findByName($reference)] ?? null;
1✔
41

42
                if ($entry) {
1✔
43
                        if (str_starts_with($reference, '_') && !isset($entry['isEntry']) && !isset($entry['isDynamicEntry'])) {
1✔
44
                                throw new AssetNotFoundException("Cannot directly access internal chunk '$reference'");
1✔
45
                        }
46

47
                        $dependencies = $this->collectDependencies($id);
1✔
48
                        unset($dependencies[$entry['file']]);
1✔
49

50
                        if ($this->devServer) {
1✔
51
                                $url = $this->devServer . '/' . $reference;
1✔
52
                                return new EntryAsset(
1✔
53
                                        url: $url,
1✔
54
                                        mimeType: Helpers::guessMimeTypeFromExtension($url),
1✔
55
                                        dependencies: [],
1✔
56
                                        file: null,
1✔
57
                                        integrity: null,
1✔
58
                                );
59
                        }
60

61
                        return $dependencies
1✔
62
                                ? new EntryAsset(
1✔
63
                                        url: $this->baseUrl . '/' . $entry['file'],
1✔
64
                                        mimeType: Helpers::guessMimeTypeFromExtension($entry['file']),
1✔
65
                                        file: $this->basePath . '/' . $entry['file'],
1✔
66
                                        dependencies: array_values($dependencies),
1✔
67
                                )
68
                                : Helpers::createAssetFromUrl($this->baseUrl . '/' . $entry['file'], $this->basePath . '/' . $entry['file']);
1✔
69

70
                } elseif ($this->publicMapper) {
1✔
71
                        return $this->publicMapper->getAsset($reference);
1✔
72

73
                } else {
74
                        throw new AssetNotFoundException("File '$reference' not found in Vite manifest");
1✔
75
                }
76
        }
77

78

79
        /**
80
         * Recursively collects all imports (including nested) from a chunk.
81
         */
82
        private function collectDependencies(string $chunkId): array
1✔
83
        {
84
                $deps = &$this->dependencies[$chunkId];
1✔
85
                if ($deps === null) {
1✔
86
                        $deps = [];
1✔
87
                        $entry = $this->chunks[$chunkId] ?? [];
1✔
88
                        foreach ($entry['css'] ?? [] as $file) {
1✔
89
                                $deps[$file] = Helpers::createAssetFromUrl($this->baseUrl . '/' . $file, $this->basePath . '/' . $file);
1✔
90
                        }
91
                        foreach ($entry['imports'] ?? [] as $id) {
1✔
92
                                $file = $this->chunks[$id]['file'];
1✔
93
                                $deps[$file] = Helpers::createAssetFromUrl($this->baseUrl . '/' . $file, $this->basePath . '/' . $file);
1✔
94
                                $deps += $this->collectDependencies($id);
1✔
95
                        }
96
                }
97
                return $deps;
1✔
98
        }
99

100

101
        private function findByName(string $name): ?string
1✔
102
        {
103
                foreach ($this->chunks as $id => $entry) {
1✔
104
                        if (($entry['name'] ?? null) === $name && (isset($entry['isEntry']) || isset($entry['isDynamicEntry']))) {
1✔
105
                                return $id;
1✔
106
                        }
107
                }
108
                return null;
1✔
109
        }
110

111

112
        private function readChunks(): array
113
        {
114
                $path = $this->manifestPath ?? $this->basePath . '/.vite/manifest.json';
1✔
115
                try {
116
                        $res = Json::decode(FileSystem::read($path), forceArrays: true);
1✔
UNCOV
117
                } catch (\Throwable $e) {
×
118
                        throw new \RuntimeException('Failed to parse Vite manifest: ' . $e->getMessage(), 0, $e);
×
119
                }
120
                if (!is_array($res)) {
1✔
UNCOV
121
                        throw new \RuntimeException('Invalid Vite manifest format in ' . $path);
×
122
                }
123
                return $res;
1✔
124
        }
125

126

127
        /**
128
         * Returns the base URL for this mapper.
129
         */
130
        public function getBaseUrl(): string
131
        {
UNCOV
132
                return $this->baseUrl;
×
133
        }
134

135

136
        /**
137
         * Returns the base path for this mapper.
138
         */
139
        public function getBasePath(): string
140
        {
UNCOV
141
                return $this->basePath;
×
142
        }
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc