1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Vite plugin for Craft CMS 3.x |
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\helpers; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\helpers\Json as JsonHelper; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* @author nystudio107 |
|
|
|
|
19
|
|
|
* @package Vite |
|
|
|
|
20
|
|
|
* @since 1.0.5 |
|
|
|
|
21
|
|
|
*/ |
|
|
|
|
22
|
|
|
class ManifestHelper |
23
|
|
|
{ |
24
|
|
|
// Constants |
25
|
|
|
// ========================================================================= |
26
|
|
|
|
27
|
|
|
const LEGACY_EXTENSION = '-legacy.'; |
28
|
|
|
|
29
|
|
|
// Protected Static Properties |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @var array|null |
34
|
|
|
*/ |
35
|
|
|
protected static $manifest; |
36
|
|
|
|
37
|
|
|
// Public Static Methods |
38
|
|
|
// ========================================================================= |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Fetch and memoize the manifest file |
42
|
|
|
* |
43
|
|
|
* @param string $manifestPath |
|
|
|
|
44
|
|
|
*/ |
|
|
|
|
45
|
|
|
public static function fetchManifest(string $manifestPath) |
46
|
|
|
{ |
47
|
|
|
// Grab the manifest |
48
|
|
|
$pathOrUrl = (string)Craft::parseEnv($manifestPath); |
49
|
|
|
$manifest = FileHelper::fetch($pathOrUrl, [JsonHelper::class, 'decodeIfJson']); |
50
|
|
|
// If no manifest file is found, log it |
51
|
|
|
if ($manifest === null) { |
52
|
|
|
Craft::error('Manifest not found at ' . $manifestPath, __METHOD__); |
53
|
|
|
} |
54
|
|
|
// Ensure we're dealing with an array |
55
|
|
|
self::$manifest = (array)$manifest; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return an array of tags from the manifest, for both modern and legacy builds |
60
|
|
|
* |
61
|
|
|
* @param string $path |
|
|
|
|
62
|
|
|
* @param bool $asyncCss |
|
|
|
|
63
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
64
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public static function manifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): array |
69
|
|
|
{ |
70
|
|
|
// Get the modern tags for this $path |
71
|
|
|
return self::extractManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return an array of tags from the manifest, for both modern and legacy builds |
76
|
|
|
* |
77
|
|
|
* @param string $path |
|
|
|
|
78
|
|
|
* @param bool $asyncCss |
|
|
|
|
79
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
80
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public static function legacyManifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): array |
85
|
|
|
{ |
86
|
|
|
// Get the legacy tags for this $path |
87
|
|
|
$parts = pathinfo($path); |
88
|
|
|
$legacyPath = $parts['dirname'] |
89
|
|
|
. '/' |
90
|
|
|
. $parts['filename'] |
91
|
|
|
. self::LEGACY_EXTENSION |
92
|
|
|
. $parts['extension']; |
93
|
|
|
|
94
|
|
|
return self::extractManifestTags($legacyPath, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return an array of data describing the script, module link, and CSS link tags for the |
99
|
|
|
* script from the manifest.json file |
100
|
|
|
* |
101
|
|
|
* @param string $path |
|
|
|
|
102
|
|
|
* @param bool $asyncCss |
|
|
|
|
103
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
104
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
105
|
|
|
* @param bool $legacy |
|
|
|
|
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public static function extractManifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = [], bool $legacy = false): array |
110
|
|
|
{ |
111
|
|
|
if (self::$manifest === null) { |
112
|
|
|
return []; |
113
|
|
|
} |
114
|
|
|
$tags = []; |
115
|
|
|
// Set the async CSS args |
116
|
|
|
$asyncCssOptions = []; |
117
|
|
|
if ($asyncCss) { |
118
|
|
|
$asyncCssOptions = [ |
119
|
|
|
'media' => 'print', |
120
|
|
|
'onload' => "this.media='all'", |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
// Set the script args |
124
|
|
|
$scriptOptions = [ |
125
|
|
|
'type' => 'module', |
126
|
|
|
'crossorigin' => true, |
127
|
|
|
]; |
128
|
|
|
if ($legacy) { |
129
|
|
|
$scriptOptions = [ |
130
|
|
|
'nomodule' => true, |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
// Iterate through the manifest |
134
|
|
|
foreach (self::$manifest as $manifestKey => $entry) { |
135
|
|
|
// If it's not an entry, skip it |
136
|
|
|
if (!isset($entry['isEntry']) || !$entry['isEntry']) { |
137
|
|
|
continue; |
138
|
|
|
} |
139
|
|
|
// If there's no file, skip it |
140
|
|
|
if (!isset($entry['file'])) { |
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
// If the $path isn't in the $manifestKey, skip it |
144
|
|
|
if (strpos($path, $manifestKey) === false) { |
145
|
|
|
continue; |
146
|
|
|
} |
147
|
|
|
// Include the entry script |
148
|
|
|
$tagOptions = array_merge($scriptOptions, $scriptTagAttrs); |
149
|
|
|
$tags[] = [ |
150
|
|
|
'type' => 'file', |
151
|
|
|
'url' => $entry['file'], |
152
|
|
|
'options' => $tagOptions |
153
|
|
|
]; |
154
|
|
|
// Include any imports |
155
|
|
|
$importFiles = []; |
|
|
|
|
156
|
|
|
self::extractImportFiles(self::$manifest, $manifestKey, $importFiles); |
|
|
|
|
157
|
|
|
foreach ($importFiles as $importFile) { |
|
|
|
|
158
|
|
|
$tags[] = [ |
|
|
|
|
159
|
|
|
'crossorigin' => $tagOptions['crossorigin'] ?? true, |
|
|
|
|
160
|
|
|
'type' => 'import', |
|
|
|
|
161
|
|
|
'url' => $importFile, |
|
|
|
|
162
|
|
|
]; |
|
|
|
|
163
|
|
|
} |
|
|
|
|
164
|
|
|
// Include any CSS tags |
165
|
|
|
$cssFiles = []; |
166
|
|
|
self::extractCssFiles(self::$manifest, $manifestKey, $cssFiles); |
167
|
|
|
foreach ($cssFiles as $cssFile) { |
168
|
|
|
$tags[] = [ |
169
|
|
|
'type' => 'css', |
170
|
|
|
'url' => $cssFile, |
171
|
|
|
'options' => array_merge([ |
|
|
|
|
172
|
|
|
'rel' => 'stylesheet', |
173
|
|
|
], $asyncCssOptions, $cssTagAttrs) |
|
|
|
|
174
|
|
|
]; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $tags; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
|
|
|
|
182
|
|
|
* Extract any import files from entries recursively |
183
|
|
|
* |
184
|
|
|
* @param array $manifest |
|
|
|
|
185
|
|
|
* @param string $manifestKey |
|
|
|
|
186
|
|
|
* @param array $importFiles |
|
|
|
|
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
protected static function extractImportFiles(array $manifest, string $manifestKey, array &$importFiles): array |
|
|
|
|
191
|
|
|
{ |
|
|
|
|
192
|
|
|
$entry = $manifest[$manifestKey] ?? null; |
|
|
|
|
193
|
|
|
if (!$entry) { |
|
|
|
|
194
|
|
|
return []; |
|
|
|
|
195
|
|
|
} |
|
|
|
|
196
|
|
|
|
197
|
|
|
$imports = $entry['imports'] ?? []; |
|
|
|
|
198
|
|
|
foreach($imports as $import) { |
|
|
|
|
199
|
|
|
$importFiles[] = $manifest[$import]['file']; |
|
|
|
|
200
|
|
|
self::extractImportFiles($manifest, $import, $importFiles); |
|
|
|
|
201
|
|
|
} |
|
|
|
|
202
|
|
|
|
203
|
|
|
return $importFiles; |
|
|
|
|
204
|
|
|
} |
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
|
|
|
|
207
|
|
|
* Extract any CSS files from entries recursively |
|
|
|
|
208
|
|
|
* |
|
|
|
|
209
|
|
|
* @param array $manifest |
|
|
|
|
210
|
|
|
* @param string $manifestKey |
|
|
|
|
211
|
|
|
* @param array $cssFiles |
|
|
|
|
212
|
|
|
* |
|
|
|
|
213
|
|
|
* @return array |
|
|
|
|
214
|
|
|
*/ |
|
|
|
|
215
|
|
|
protected static function extractCssFiles(array $manifest, string $manifestKey, array &$cssFiles): array |
216
|
|
|
{ |
217
|
|
|
$entry = $manifest[$manifestKey] ?? null; |
218
|
|
|
if (!$entry) { |
219
|
|
|
return []; |
220
|
|
|
} |
221
|
|
|
$cssFiles = array_merge($cssFiles, $entry['css'] ?? []); |
222
|
|
|
$imports = array_merge($entry['imports'] ?? [], $entry['dynamicImport'] ?? []); |
223
|
|
|
foreach ($imports as $import) { |
224
|
|
|
self::extractCssFiles($manifest, $import, $cssFiles); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $cssFiles; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|