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

nette / assets / 15134933781

20 May 2025 09:50AM UTC coverage: 95.111% (+0.2%) from 94.943%
15134933781

push

github

dg
readme: added info about custom mappers

428 of 450 relevant lines covered (95.11%)

0.95 hits per line

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

88.68
/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
                                )
69
                                : Helpers::createAssetFromUrl($this->baseUrl . '/' . $chunk['file'], $this->basePath . '/' . $chunk['file']);
1✔
70

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

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

82

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

104

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

119

120
        /**
121
         * Returns the base URL for this mapper.
122
         */
123
        public function getBaseUrl(): string
124
        {
125
                return $this->baseUrl;
×
126
        }
127

128

129
        /**
130
         * Returns the base path for this mapper.
131
         */
132
        public function getBasePath(): string
133
        {
134
                return $this->basePath;
×
135
        }
136
}
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