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

nette / assets / 15131058368

20 May 2025 07:02AM UTC coverage: 94.678% (-0.4%) from 95.089%
15131058368

push

github

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

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

15 existing lines in 3 files now uncovered.

427 of 451 relevant lines covered (94.68%)

0.95 hits per line

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

87.93
/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
                                if (isset($entry['isEntry'])) {
1✔
52
                                        $dependencies = array_values(array_filter($dependencies, fn($asset) => $asset instanceof StyleAsset));
1✔
53
                                        return new EntryAsset(
1✔
54
                                                url: $this->devServer . '/' . $entry['src'],
1✔
55
                                                mimeType: Helpers::guessMimeTypeFromExtension($entry['src']),
1✔
56
                                                imports: array_merge($dependencies, [new ScriptAsset($this->devServer . '/@vite/client')]),
1✔
57
                                        );
58
                                }
UNCOV
59
                                return Helpers::createAssetFromUrl($this->devServer . '/' . $entry['src']);
×
60
                        }
61

62
                        return $dependencies
1✔
63
                                ? new EntryAsset(
1✔
64
                                        url: $this->baseUrl . '/' . $entry['file'],
1✔
65
                                        mimeType: Helpers::guessMimeTypeFromExtension($entry['file']),
1✔
66
                                        file: $this->basePath . '/' . $entry['file'],
1✔
67
                                        imports: array_values(array_filter($dependencies, fn($asset) => $asset instanceof StyleAsset)),
1✔
68
                                        preloads: array_values(array_filter($dependencies, fn($asset) => $asset instanceof ScriptAsset)),
1✔
69
                                )
70
                                : Helpers::createAssetFromUrl($this->baseUrl . '/' . $entry['file'], $this->basePath . '/' . $entry['file']);
1✔
71

72
                } elseif ($this->publicMapper) {
1✔
73
                        if ($this->devServer) {
1✔
UNCOV
74
                                return Helpers::createAssetFromUrl($this->devServer . '/' . $reference, null);
×
75
                        }
76
                        return $this->publicMapper->getAsset($reference);
1✔
77

78
                } else {
79
                        throw new AssetNotFoundException("File '$reference' not found in Vite manifest");
1✔
80
                }
81
        }
82

83

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

105

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

116

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

131

132
        /**
133
         * Returns the base URL for this mapper.
134
         */
135
        public function getBaseUrl(): string
136
        {
UNCOV
137
                return $this->baseUrl;
×
138
        }
139

140

141
        /**
142
         * Returns the base path for this mapper.
143
         */
144
        public function getBasePath(): string
145
        {
UNCOV
146
                return $this->basePath;
×
147
        }
148
}
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