• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

nette / assets / 22312221661

23 Feb 2026 03:14PM UTC coverage: 94.106% (+0.06%) from 94.048%
22312221661

push

github

dg
added CLAUDE.md

495 of 526 relevant lines covered (94.11%)

0.94 hits per line

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

91.04
/src/Assets/ViteMapper.php
1
<?php declare(strict_types=1);
1✔
2

3
namespace Nette\Assets;
4

5
use Nette\Utils\FileSystem;
6
use Nette\Utils\Json;
7
use function array_filter, array_values, is_array, preg_match, str_starts_with;
8

9

10
/**
11
 * Maps asset references to Vite-generated files using a Vite manifest.json.
12
 * Supports both development mode (Vite dev server) and production mode.
13
 */
14
class ViteMapper implements Mapper
15
{
16
        /** @var array<string, array{file: string, isEntry?: bool, isDynamicEntry?: bool, css?: list<string>, imports?: list<string>}> */
17
        private array $chunks;
18

19
        /** @var array<string, ?array<string, Asset>> cached dependencies keyed by chunkId */
20
        private array $dependencies = [];
21

22

23
        public function __construct(
1✔
24
                private readonly string $baseUrl,
25
                private readonly string $basePath,
26
                private readonly ?string $manifestPath = null,
27
                private readonly ?string $devServer = null,
28
                private readonly ?Mapper $publicMapper = null,
29
        ) {
30
                if ($devServer !== null && !str_starts_with($devServer, 'http')) {
1✔
31
                        throw new \InvalidArgumentException("Vite devServer must be absolute URL, '$devServer' given");
×
32
                }
33
        }
1✔
34

35

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

44
                if ($this->devServer) {
1✔
45
                        return $this->createDevelopmentAsset($reference);
1✔
46
                }
47

48
                $this->chunks ??= $this->readChunks();
1✔
49

50
                if (isset($this->chunks[$reference])) {
1✔
51
                        return $this->createProductionAsset($reference);
1✔
52

53
                } elseif ($this->publicMapper) {
1✔
54
                        return $this->publicMapper->getAsset($reference);
1✔
55

56
                } else {
57
                        throw new AssetNotFoundException("File '$reference' not found in Vite manifest");
1✔
58
                }
59
        }
60

61

62
        private function createProductionAsset(string $reference): Asset
1✔
63
        {
64
                $chunk = $this->chunks[$reference];
1✔
65
                $entry = isset($chunk['isEntry']) || isset($chunk['isDynamicEntry']);
1✔
66
                if (str_starts_with($reference, '_') && !$entry) {
1✔
67
                        throw new AssetNotFoundException("Cannot directly access internal chunk '$reference'");
1✔
68
                }
69

70
                $dependencies = $this->collectDependencies($reference);
1✔
71
                unset($dependencies[$chunk['file']]);
1✔
72

73
                return $dependencies
1✔
74
                        ? new EntryAsset(
1✔
75
                                url: $this->baseUrl . '/' . $chunk['file'],
1✔
76
                                file: $this->basePath . '/' . $chunk['file'],
1✔
77
                                imports: array_values(array_filter($dependencies, fn($asset) => $asset instanceof StyleAsset)),
1✔
78
                                preloads: array_values(array_filter($dependencies, fn($asset) => $asset instanceof ScriptAsset)),
1✔
79
                                crossorigin: true,
1✔
80
                        )
81
                        : Helpers::createAssetFromUrl(
1✔
82
                                $this->baseUrl . '/' . $chunk['file'],
1✔
83
                                $this->basePath . '/' . $chunk['file'],
1✔
84
                                ['crossorigin' => true],
1✔
85
                        );
86
        }
87

88

89
        private function createDevelopmentAsset(string $reference): Asset
1✔
90
        {
91
                $url = $this->devServer . '/' . $reference;
1✔
92
                return match (1) {
93
                        preg_match('~\.(jsx?|mjs|tsx?)$~i', $reference) => new EntryAsset(
1✔
94
                                url: $url,
1✔
95
                                imports: [new ScriptAsset($this->devServer . '/@vite/client', type: 'module')],
1✔
96
                        ),
97
                        preg_match('~\.(sass|scss)$~i', $reference) => new StyleAsset($url),
1✔
98
                        default => Helpers::createAssetFromUrl($url),
1✔
99
                };
100
        }
101

102

103
        /**
104
         * Recursively collects all imports (including nested) from a chunk.
105
         * @return array<string, Asset>
106
         */
107
        private function collectDependencies(string $chunkId): array
1✔
108
        {
109
                $deps = &$this->dependencies[$chunkId];
1✔
110
                if ($deps === null) {
1✔
111
                        $deps = [];
1✔
112
                        $chunk = $this->chunks[$chunkId] ?? [];
1✔
113
                        foreach ($chunk['css'] ?? [] as $file) {
1✔
114
                                $deps[$file] = Helpers::createAssetFromUrl(
1✔
115
                                        $this->baseUrl . '/' . $file,
1✔
116
                                        $this->basePath . '/' . $file,
1✔
117
                                        ['crossorigin' => true],
1✔
118
                                );
119
                        }
120
                        foreach ($chunk['imports'] ?? [] as $id) {
1✔
121
                                $file = $this->chunks[$id]['file'];
1✔
122
                                $deps[$file] = Helpers::createAssetFromUrl(
1✔
123
                                        $this->baseUrl . '/' . $file,
1✔
124
                                        $this->basePath . '/' . $file,
1✔
125
                                        ['type' => 'module', 'crossorigin' => true],
1✔
126
                                );
127
                                $deps += $this->collectDependencies($id);
1✔
128
                        }
129
                }
130
                return $deps;
1✔
131
        }
132

133

134
        /**
135
         * @return array<string, array{file: string, isEntry?: bool, isDynamicEntry?: bool, css?: list<string>, imports?: list<string>}>
136
         */
137
        private function readChunks(): array
138
        {
139
                $path = $this->manifestPath ?? $this->basePath . '/.vite/manifest.json';
1✔
140
                try {
141
                        $res = Json::decode(FileSystem::read($path), forceArrays: true);
1✔
142
                } catch (\Throwable $e) {
×
143
                        throw new \RuntimeException("Failed to read Vite manifest from '$path'. Did you run 'npm run build'?", 0, $e);
×
144
                }
145
                if (!is_array($res)) {
1✔
146
                        throw new \RuntimeException("Invalid Vite manifest format in '$path'");
×
147
                }
148
                return $res;
1✔
149
        }
150

151

152
        /**
153
         * Returns the base URL for this mapper.
154
         */
155
        public function getBaseUrl(): string
156
        {
157
                return $this->baseUrl;
×
158
        }
159

160

161
        /**
162
         * Returns the base path for this mapper.
163
         */
164
        public function getBasePath(): string
165
        {
166
                return $this->basePath;
×
167
        }
168
}
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