-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ViteVariableTrait.php
executable file
·128 lines (115 loc) · 3.2 KB
/
ViteVariableTrait.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
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
119
120
121
122
123
124
125
126
127
128
<?php
/**
* Vite plugin for Craft CMS 3.x
*
* Allows the use of the Vite.js next generation frontend tooling with Craft CMS
*
* @link https://nystudio107.com
* @copyright Copyright (c) 2021 nystudio107
*/
namespace nystudio107\pluginvite\variables;
use craft\helpers\Template;
use nystudio107\pluginvite\services\ViteService;
use Twig\Markup;
use yii\base\InvalidConfigException;
/**
* @author nystudio107
* @package Vite
* @since 1.0.4
*/
trait ViteVariableTrait
{
// Public Properties
// =========================================================================
/**
* @var null|ViteService the Vite service
*/
public ?ViteService $viteService = null;
// Public Methods
// =========================================================================
/**
* Return the appropriate tags to load the Vite script, either via the dev server or
* extracting it from the manifest.json file
*
* @param string $path
* @param bool $asyncCss
* @param array $scriptTagAttrs
* @param array $cssTagAttrs
*
* @return Markup
*/
public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): Markup
{
return Template::raw(
$this->viteService->script($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs)
);
}
/**
* Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or
* extracting it from the manifest.json file
*
* @param string $path
* @param bool $asyncCss
* @param array $scriptTagAttrs
* @param array $cssTagAttrs
*
* @return Markup
* @throws InvalidConfigException
*/
public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): Markup
{
$this->viteService->register($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
return Template::raw('');
}
/**
* Return the URL for the given entry
*
* @param string $path
*
* @return Markup
*/
public function entry(string $path): Markup
{
return Template::raw(
$this->viteService->entry($path)
);
}
/**
* Return the URL for the given asset
*
* @param string $path
*
* @return Markup
*/
public function asset(string $path, bool $public=false): Markup
{
return Template::raw(
$this->viteService->asset($path, $public)
);
}
/**
* Inline the contents of a local file (via path) or remote file (via URL) in your templates.
* Yii2 aliases and/or environment variables may be used
*
* @param string $pathOrUrl
*
* @return Markup
*/
public function inline(string $pathOrUrl): Markup
{
$file = $this->viteService->fetch($pathOrUrl);
if ($file === null) {
$file = '';
}
return Template::raw($file);
}
/**
* Determine whether the Vite dev server is running
*
* @return bool
*/
public function devServerRunning(): bool
{
return $this->viteService->devServerRunning();
}
}