-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.php
More file actions
118 lines (92 loc) · 2.86 KB
/
vite.php
File metadata and controls
118 lines (92 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
if (!function_exists('vite_server_origin')) {
function vite_server_origin(): string
{
$host = 'http://127.0.0.1';
$port = 5173;
return sprintf('%s:%d', $host, $port);
}
}
if (!function_exists('vite_is_dev_server_running')) {
function vite_is_dev_server_running(): bool
{
static $isRunning;
if ($isRunning !== null) {
return $isRunning;
}
$host = '127.0.0.1';
$port = 5173;
$connection = @fsockopen($host, $port, $errno, $errstr, 0.1);
if (is_resource($connection)) {
fclose($connection);
$isRunning = true;
} else {
$isRunning = false;
}
return $isRunning;
}
}
if (!function_exists('vite_manifest')) {
/**
* @throws RuntimeException if the manifest file is missing or malformed.
*/
function vite_manifest(): array
{
static $manifest;
if ($manifest !== null) {
return $manifest;
}
$manifestPath = __DIR__ . '/dist/.vite/manifest.json';
if (!file_exists($manifestPath)) {
throw new RuntimeException('Vite manifest not found. Run `npm run build` to generate it.');
}
$contents = file_get_contents($manifestPath);
$decoded = json_decode($contents, true);
if (!is_array($decoded)) {
throw new RuntimeException('Invalid Vite manifest. Delete dist/ and rebuild the assets.');
}
$manifest = $decoded;
return $manifest;
}
}
if (!function_exists('vite_asset')) {
/**
* @throws InvalidArgumentException if an entry could not be found in the manifest.
*/
function vite_asset(string $entry): string
{
if (vite_is_dev_server_running()) {
return rtrim(vite_server_origin(), '/') . '/' . ltrim($entry, '/');
}
$manifest = vite_manifest();
if (!array_key_exists($entry, $manifest) || !isset($manifest[$entry]['file'])) {
throw new InvalidArgumentException("Unable to locate Vite asset for entry [{$entry}].");
}
return '/dist/' . $manifest[$entry]['file'];
}
}
if (!function_exists('vite_css')) {
function vite_css(string $entry): array
{
if (vite_is_dev_server_running()) {
return [];
}
$manifest = vite_manifest();
if (!array_key_exists($entry, $manifest) || empty($manifest[$entry]['css'])) {
return [];
}
return array_map(
static fn (string $path): string => '/dist/' . ltrim($path, '/'),
$manifest[$entry]['css']
);
}
}
if (!function_exists('vite_client')) {
function vite_client(): ?string
{
if (!vite_is_dev_server_running()) {
return null;
}
return rtrim(vite_server_origin(), '/') . '/@vite/client';
}
}