Passed
Push — master ( ad6b2a...b41ca0 )
by Caen
07:45 queued 14s
created

Vite   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 65
rs 10
c 2
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A formatAssetPath() 0 12 3
A formatStylesheetLink() 0 3 1
A checkFileExtensionForPath() 0 3 1
A formatScriptInclude() 0 3 1
A assets() 0 9 2
A running() 0 3 1
A isCssPath() 0 3 1
A asset() 0 3 1
A isJsPath() 0 3 1
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