1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Vite plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Allows the use of the Vite.js next generation frontend tooling with Craft CMS |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2021 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\pluginvite\variables; |
12
|
|
|
|
13
|
|
|
use craft\helpers\Template; |
14
|
|
|
use nystudio107\pluginvite\services\ViteService; |
15
|
|
|
use Twig\Markup; |
16
|
|
|
use yii\base\InvalidConfigException; |
17
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @author nystudio107 |
|
|
|
|
20
|
|
|
* @package Vite |
|
|
|
|
21
|
|
|
* @since 1.0.4 |
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
trait ViteVariableTrait |
24
|
|
|
{ |
25
|
|
|
// Public Properties |
26
|
|
|
// ========================================================================= |
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @var null|ViteService the Vite service |
30
|
|
|
*/ |
31
|
|
|
public ?ViteService $viteService = null; |
32
|
|
|
|
33
|
|
|
// Public Methods |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return the appropriate tags to load the Vite script, either via the dev server or |
38
|
|
|
* extracting it from the manifest.json file |
39
|
|
|
* |
40
|
|
|
* @param string $path |
|
|
|
|
41
|
|
|
* @param bool $asyncCss |
|
|
|
|
42
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
43
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
44
|
|
|
* |
45
|
|
|
* @return Markup |
46
|
|
|
*/ |
47
|
|
|
public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): Markup |
48
|
|
|
{ |
49
|
|
|
return Template::raw( |
50
|
|
|
$this->viteService->script($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs) |
|
|
|
|
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or |
56
|
|
|
* extracting it from the manifest.json file |
57
|
|
|
* |
58
|
|
|
* @param string $path |
|
|
|
|
59
|
|
|
* @param bool $asyncCss |
|
|
|
|
60
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
61
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
62
|
|
|
* |
63
|
|
|
* @return Markup |
64
|
|
|
* @throws InvalidConfigException |
65
|
|
|
*/ |
66
|
|
|
public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): Markup |
67
|
|
|
{ |
68
|
|
|
$this->viteService->register($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
69
|
|
|
|
70
|
|
|
return Template::raw(''); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return the URL for the given entry |
75
|
|
|
* |
76
|
|
|
* @param string $path |
|
|
|
|
77
|
|
|
* |
78
|
|
|
* @return Markup |
79
|
|
|
*/ |
80
|
|
|
public function entry(string $path): Markup |
81
|
|
|
{ |
82
|
|
|
return Template::raw( |
83
|
|
|
$this->viteService->entry($path) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return the integrity hash (or an empty string if not present) for the given entry |
89
|
|
|
* |
90
|
|
|
* @param string $path |
|
|
|
|
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function integrity(string $path): string |
95
|
|
|
{ |
96
|
|
|
return Template::raw( |
97
|
|
|
$this->viteService->integrity($path) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
|
|
|
|
102
|
|
|
* Return the URL for the given asset |
103
|
|
|
* |
104
|
|
|
* @param string $path |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @return Markup |
107
|
|
|
*/ |
108
|
|
|
public function asset(string $path, bool $public = false): Markup |
109
|
|
|
{ |
110
|
|
|
return Template::raw( |
111
|
|
|
$this->viteService->asset($path, $public) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Inline the contents of a local file (via path) or remote file (via URL) in your templates. |
117
|
|
|
* Yii2 aliases and/or environment variables may be used |
118
|
|
|
* |
119
|
|
|
* @param string $pathOrUrl |
|
|
|
|
120
|
|
|
* |
121
|
|
|
* @return Markup |
122
|
|
|
*/ |
123
|
|
|
public function inline(string $pathOrUrl): Markup |
124
|
|
|
{ |
125
|
|
|
$file = $this->viteService->fetch($pathOrUrl); |
126
|
|
|
if ($file === null) { |
127
|
|
|
$file = ''; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return Template::raw($file); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Determine whether the Vite dev server is running |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public function devServerRunning(): bool |
139
|
|
|
{ |
140
|
|
|
return $this->viteService->devServerRunning(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|