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

nette / assets / 15179342226

22 May 2025 06:03AM UTC coverage: 95.436% (+0.4%) from 95.067%
15179342226

push

github

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

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

15 existing lines in 5 files now uncovered.

460 of 482 relevant lines covered (95.44%)

0.95 hits per line

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

90.48
/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
                $chunk = $this->chunks[$reference] ?? null;
1✔
41

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

48
                        $dependencies = $this->collectDependencies($reference);
1✔
49
                        unset($dependencies[$chunk['file']]);
1✔
50

51
                        if ($this->devServer) {
1✔
52
                                return $dependencies
1✔
53
                                        ? new EntryAsset(
1✔
54
                                                url: $this->devServer . '/' . $chunk['src'],
1✔
55
                                                mimeType: Helpers::guessMimeTypeFromExtension($chunk['src']),
1✔
56
                                                imports: [new ScriptAsset($this->devServer . '/@vite/client', type: 'module')],
1✔
57
                                        )
58
                                        : Helpers::createAssetFromUrl($this->devServer . '/' . $chunk['src']);
1✔
59
                        }
60

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

76
                } elseif ($this->publicMapper) {
1✔
77
                        if ($this->devServer) {
1✔
UNCOV
78
                                return Helpers::createAssetFromUrl($this->devServer . '/' . $reference);
×
79
                        }
80
                        return $this->publicMapper->getAsset($reference);
1✔
81

82
                } else {
83
                        throw new AssetNotFoundException("File '$reference' not found in Vite manifest");
1✔
84
                }
85
        }
86

87

88
        /**
89
         * Recursively collects all imports (including nested) from a chunk.
90
         */
91
        private function collectDependencies(string $chunkId): array
1✔
92
        {
93
                $deps = &$this->dependencies[$chunkId];
1✔
94
                if ($deps === null) {
1✔
95
                        $deps = [];
1✔
96
                        $chunk = $this->chunks[$chunkId] ?? [];
1✔
97
                        foreach ($chunk['css'] ?? [] as $file) {
1✔
98
                                $deps[$file] = Helpers::createAssetFromUrl(
1✔
99
                                        $this->baseUrl . '/' . $file,
1✔
100
                                        $this->basePath . '/' . $file,
1✔
101
                                        ['crossorigin' => true],
1✔
102
                                );
103
                        }
104
                        foreach ($chunk['imports'] ?? [] as $id) {
1✔
105
                                $file = $this->chunks[$id]['file'];
1✔
106
                                $deps[$file] = Helpers::createAssetFromUrl(
1✔
107
                                        $this->baseUrl . '/' . $file,
1✔
108
                                        $this->basePath . '/' . $file,
1✔
109
                                        ['type' => 'module', 'crossorigin' => true],
1✔
110
                                );
111
                                $deps += $this->collectDependencies($id);
1✔
112
                        }
113
                }
114
                return $deps;
1✔
115
        }
116

117

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

132

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

141

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