-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVite.php
86 lines (72 loc) · 1.71 KB
/
Vite.php
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
<?php
namespace Oblik\KirbyVite;
use Kirby\Http\Uri;
class Vite
{
protected static $instance;
public static function instance()
{
return static::$instance ??= new static();
}
public $manifest;
public function __construct()
{
$path = implode(DIRECTORY_SEPARATOR, array_filter([
kirby()->root(),
option('oblik.vite.build.outDir'),
'manifest.json'
], 'strlen'));
try {
$this->manifest = json_decode(file_get_contents($path), true);
} catch (\Throwable $t) {
// Vite is running in development mode.
}
}
public function isDev(): bool
{
return $this->manifest === null;
}
public function prodUrl(string $path): string
{
return implode('/', array_filter([
kirby()->url(),
option('oblik.vite.build.outDir'),
$path
], 'strlen'));
}
public function devUrl(string $path): string
{
$uri = new Uri([
'scheme' => option('oblik.vite.server.https') ? 'https' : 'http',
'host' => option('oblik.vite.server.host'),
'port' => option('oblik.vite.server.port'),
'path' => $path
]);
return $uri->toString();
}
/**
* Output a `<script>` tag for an entry point.
* @param string $entry e.g. `src/index.js`.
*/
public function js(string $entry): string
{
if (is_array($this->manifest)) {
$url = $this->prodUrl($this->manifest[$entry]['file']);
} else {
$url = $this->devUrl($entry);
}
return js($url, ['type' => 'module']);
}
/**
* Outputs `<link>` tags for each CSS file of an entry point.
* @param string $entry The JavaScript entry point that includes your CSS.
*/
public function css(string $entry)
{
if (is_array($this->manifest)) {
foreach ($this->manifest[$entry]['css'] as $file) {
return css($this->prodUrl($file));
}
}
}
}