|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Facades; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\HtmlString; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Vite facade for handling Vite-related operations. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Vite |
|
14
|
|
|
{ |
|
15
|
|
|
protected const CSS_EXTENSIONS = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'pcss', 'postcss']; |
|
16
|
|
|
protected const JS_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx']; |
|
17
|
|
|
|
|
18
|
|
|
public static function running(): bool |
|
19
|
|
|
{ |
|
20
|
|
|
return Filesystem::exists('app/storage/framework/runtime/vite.hot'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public static function asset(string $path): HtmlString |
|
24
|
|
|
{ |
|
25
|
|
|
return static::assets([$path]); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** @param array<string> $paths */ |
|
29
|
|
|
public static function assets(array $paths): HtmlString |
|
30
|
|
|
{ |
|
31
|
|
|
$html = '<script src="http://localhost:5173/@vite/client" type="module"></script>'; |
|
32
|
|
|
|
|
33
|
|
|
foreach ($paths as $path) { |
|
34
|
|
|
$html .= static::formatAssetPath($path); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return new HtmlString($html); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @throws InvalidArgumentException If the asset type is not supported. */ |
|
41
|
|
|
protected static function formatAssetPath(string $path): string |
|
42
|
|
|
{ |
|
43
|
|
|
if (static::isCssPath($path)) { |
|
44
|
|
|
return static::formatStylesheetLink($path); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (static::isJsPath($path)) { |
|
48
|
|
|
return static::formatScriptInclude($path); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// We don't know how to handle other asset types, so we throw an exception to let the user know. |
|
52
|
|
|
throw new InvalidArgumentException("Unsupported asset type for path: '$path'"); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected static function isCssPath(string $path): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return static::checkFileExtensionForPath($path, static::CSS_EXTENSIONS); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected static function isJsPath(string $path): bool |
|
61
|
|
|
{ |
|
62
|
|
|
return static::checkFileExtensionForPath($path, static::JS_EXTENSIONS); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected static function checkFileExtensionForPath(string $path, array $extensions): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return preg_match('/\.('.implode('|', $extensions).')$/', $path) === 1; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected static function formatStylesheetLink(string $path): string |
|
71
|
|
|
{ |
|
72
|
|
|
return sprintf('<link rel="stylesheet" href="http://localhost:5173/%s">', $path); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected static function formatScriptInclude(string $path): string |
|
76
|
|
|
{ |
|
77
|
|
|
return sprintf('<script src="http://localhost:5173/%s" type="module"></script>', $path); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|