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\services; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\Component; |
15
|
|
|
use craft\helpers\Html as HtmlHelper; |
16
|
|
|
use craft\helpers\Json as JsonHelper; |
17
|
|
|
use craft\helpers\UrlHelper; |
18
|
|
|
use craft\web\View; |
19
|
|
|
|
20
|
|
|
use yii\base\InvalidConfigException; |
21
|
|
|
use yii\caching\ChainedDependency; |
22
|
|
|
use yii\caching\FileDependency; |
23
|
|
|
use yii\caching\TagDependency; |
24
|
|
|
|
25
|
|
|
use GuzzleHttp\Client; |
26
|
|
|
use GuzzleHttp\RequestOptions; |
27
|
|
|
|
28
|
|
|
use Throwable; |
29
|
|
|
|
30
|
|
|
/** |
|
|
|
|
31
|
|
|
* @author nystudio107 |
|
|
|
|
32
|
|
|
* @package Vite |
|
|
|
|
33
|
|
|
* @since 1.0.0 |
|
|
|
|
34
|
|
|
*/ |
|
|
|
|
35
|
|
|
class ViteService extends Component |
36
|
|
|
{ |
37
|
|
|
// Constants |
38
|
|
|
// ========================================================================= |
39
|
|
|
|
40
|
|
|
const VITE_CLIENT = '@vite/client.js'; |
41
|
|
|
const LEGACY_EXTENSION = '-legacy.'; |
42
|
|
|
const LEGACY_POLYFILLS = 'vite/legacy-polyfills'; |
43
|
|
|
|
44
|
|
|
const CACHE_KEY = 'vite'; |
45
|
|
|
const CACHE_TAG = 'vite'; |
46
|
|
|
|
47
|
|
|
const DEVMODE_CACHE_DURATION = 30; |
48
|
|
|
|
49
|
|
|
const USER_AGENT_STRING = 'User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'; |
50
|
|
|
|
51
|
|
|
const SAFARI_NOMODULE_FIX = '!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()},!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();'; |
52
|
|
|
|
53
|
|
|
// Public Properties |
54
|
|
|
// ========================================================================= |
55
|
|
|
|
56
|
|
|
/** |
|
|
|
|
57
|
|
|
* @var bool Should the dev server be used for? |
58
|
|
|
*/ |
59
|
|
|
public $useDevServer; |
60
|
|
|
|
61
|
|
|
/** |
|
|
|
|
62
|
|
|
* @var string File system path (or URL) to the Vite-built manifest.json |
63
|
|
|
*/ |
64
|
|
|
public $manifestPath; |
65
|
|
|
|
66
|
|
|
/** |
|
|
|
|
67
|
|
|
* @var string The public URL to the dev server (what appears in `<script src="">` tags |
68
|
|
|
*/ |
69
|
|
|
public $devServerPublic; |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @var string The public URL to use when not using the dev server |
73
|
|
|
*/ |
74
|
|
|
public $serverPublic; |
75
|
|
|
|
76
|
|
|
/** |
|
|
|
|
77
|
|
|
* @var string The JavaScript entry from the manifest.json to inject on Twig error pages |
78
|
|
|
* This can be a string or an array of strings |
79
|
|
|
*/ |
80
|
|
|
public $errorEntry = ''; |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* @var string String to be appended to the cache key |
84
|
|
|
*/ |
85
|
|
|
public $cacheKeySuffix = ''; |
86
|
|
|
|
87
|
|
|
/** |
|
|
|
|
88
|
|
|
* @var string The internal URL to the dev server, when accessed from the environment in which PHP is executing |
89
|
|
|
* This can be the same as `$devServerPublic`, but may be different in containerized or VM setups. |
90
|
|
|
* ONLY used if $checkDevServer = true |
91
|
|
|
*/ |
92
|
|
|
public $devServerInternal; |
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* @var bool Should we check for the presence of the dev server by pinging $devServerInternal to make sure it's running? |
96
|
|
|
*/ |
97
|
|
|
public $checkDevServer = false; |
98
|
|
|
|
99
|
|
|
// Protected Properties |
100
|
|
|
// ========================================================================= |
101
|
|
|
|
102
|
|
|
/** |
|
|
|
|
103
|
|
|
* @var bool Whether any legacy tags were found in this request |
104
|
|
|
*/ |
105
|
|
|
protected $hasLegacyTags = false; |
106
|
|
|
|
107
|
|
|
/** |
|
|
|
|
108
|
|
|
* @var bool Whether the legacy polyfill has been included yet or not |
109
|
|
|
*/ |
110
|
|
|
protected $legacyPolyfillIncluded = false; |
111
|
|
|
|
112
|
|
|
// Public Methods |
113
|
|
|
// ========================================================================= |
114
|
|
|
|
115
|
|
|
/** |
|
|
|
|
116
|
|
|
* @inheritDoc |
117
|
|
|
*/ |
|
|
|
|
118
|
|
|
public function init() |
119
|
|
|
{ |
120
|
|
|
parent::init(); |
121
|
|
|
// Do nothing for console requests |
122
|
|
|
$request = Craft::$app->getRequest(); |
123
|
|
|
if ($request->getIsConsoleRequest()) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
// Our component is lazily loaded, so the View will be instantiated by now |
127
|
|
|
if (Craft::$app->getConfig()->getGeneral()->devMode) { |
128
|
|
|
Craft::$app->getView()->on(View::EVENT_END_BODY, [$this, 'injectErrorEntry']); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return the appropriate tags to load the Vite script, either via the dev server or |
134
|
|
|
* extracting it from the manifest.json file |
135
|
|
|
* |
136
|
|
|
* @param string $path |
|
|
|
|
137
|
|
|
* @param bool $asyncCss |
|
|
|
|
138
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
139
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string |
144
|
|
|
{ |
145
|
|
|
if ($this->devServerRunning()) { |
146
|
|
|
return $this->devServerScript($path, $scriptTagAttrs); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this->manifestScript($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Return the script tag to load the script from the Vite dev server |
154
|
|
|
* |
155
|
|
|
* @param string $path |
|
|
|
|
156
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function devServerScript(string $path, array $scriptTagAttrs = []): string |
161
|
|
|
{ |
162
|
|
|
$lines = []; |
163
|
|
|
// Include the entry script |
164
|
|
|
$url = $this->createUrl($this->devServerPublic, $path); |
165
|
|
|
$lines[] = HtmlHelper::jsFile($url, array_merge([ |
|
|
|
|
166
|
|
|
'type' => 'module', |
167
|
|
|
], $scriptTagAttrs)); |
|
|
|
|
168
|
|
|
|
169
|
|
|
return implode("\r\n", $lines); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Return the script, module link, and CSS link tags for the script from the manifest.json file |
174
|
|
|
* |
175
|
|
|
* @param string $path |
|
|
|
|
176
|
|
|
* @param bool $asyncCss |
|
|
|
|
177
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
178
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function manifestScript(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string |
183
|
|
|
{ |
184
|
|
|
$lines = []; |
185
|
|
|
$tags = $this->manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
186
|
|
|
// Handle any legacy polyfills |
187
|
|
|
if ($this->hasLegacyTags && !$this->legacyPolyfillIncluded) { |
188
|
|
|
$lines[] = HtmlHelper::script(self::SAFARI_NOMODULE_FIX, []); |
189
|
|
|
$legacyPolyfillTags = $this->extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
190
|
|
|
$tags = array_merge($legacyPolyfillTags, $tags); |
191
|
|
|
$this->legacyPolyfillIncluded = true; |
192
|
|
|
} |
193
|
|
|
foreach($tags as $tag) { |
|
|
|
|
194
|
|
|
if (!empty($tag)) { |
195
|
|
|
switch ($tag['type']) { |
196
|
|
|
case 'file': |
|
|
|
|
197
|
|
|
$lines[] = HtmlHelper::jsFile($tag['url'], $tag['options']); |
198
|
|
|
break; |
199
|
|
|
case 'css': |
|
|
|
|
200
|
|
|
$lines[] = HtmlHelper::cssFile($tag['url'], $tag['options']); |
201
|
|
|
break; |
202
|
|
|
default: |
|
|
|
|
203
|
|
|
break; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return implode("\r\n", $lines); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or |
213
|
|
|
* extracting it from the manifest.json file |
214
|
|
|
* |
215
|
|
|
* @param string $path |
|
|
|
|
216
|
|
|
* @param bool $asyncCss |
|
|
|
|
217
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
218
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
219
|
|
|
* |
220
|
|
|
* @return void |
221
|
|
|
* @throws InvalidConfigException |
222
|
|
|
*/ |
223
|
|
|
public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []) |
224
|
|
|
{ |
225
|
|
|
if ($this->devServerRunning()) { |
226
|
|
|
$this->devServerRegister($path, $scriptTagAttrs); |
227
|
|
|
|
228
|
|
|
return; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$this->manifestRegister($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Register the script tag to the Craft View to load the script from the Vite dev server |
236
|
|
|
* |
237
|
|
|
* @param string $path |
|
|
|
|
238
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
239
|
|
|
* |
240
|
|
|
* @return void |
241
|
|
|
* @throws InvalidConfigException |
242
|
|
|
*/ |
243
|
|
|
public function devServerRegister(string $path, array $scriptTagAttrs = []) |
244
|
|
|
{ |
245
|
|
|
$view = Craft::$app->getView(); |
246
|
|
|
// Include the entry script |
247
|
|
|
$url = $this->createUrl($this->devServerPublic, $path); |
248
|
|
|
$view->registerJsFile( |
249
|
|
|
$url, |
250
|
|
|
array_merge(['type' => 'module'], $scriptTagAttrs), |
251
|
|
|
md5($url . JsonHelper::encode($scriptTagAttrs)) |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Register the script, module link, and CSS link tags to the Craft View for the script from the manifest.json file |
257
|
|
|
* |
258
|
|
|
* @param string $path |
|
|
|
|
259
|
|
|
* @param bool $asyncCss |
|
|
|
|
260
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
261
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
262
|
|
|
* |
263
|
|
|
* @return void |
264
|
|
|
* @throws InvalidConfigException |
265
|
|
|
*/ |
266
|
|
|
public function manifestRegister(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []) |
267
|
|
|
{ |
268
|
|
|
$view = Craft::$app->getView(); |
269
|
|
|
$tags = $this->manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
270
|
|
|
// Handle any legacy polyfills |
271
|
|
|
if ($this->hasLegacyTags && !$this->legacyPolyfillIncluded) { |
272
|
|
|
$view->registerScript(self::SAFARI_NOMODULE_FIX, $view::POS_HEAD, [], 'SAFARI_NOMODULE_FIX'); |
273
|
|
|
$legacyPolyfillTags = $this->extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
274
|
|
|
$tags = array_merge($legacyPolyfillTags, $tags); |
275
|
|
|
$this->legacyPolyfillIncluded = true; |
276
|
|
|
} |
277
|
|
|
foreach($tags as $tag) { |
|
|
|
|
278
|
|
|
if (!empty($tag)) { |
279
|
|
|
switch ($tag['type']) { |
280
|
|
|
case 'file': |
|
|
|
|
281
|
|
|
$view->registerJsFile( |
282
|
|
|
$tag['url'], |
283
|
|
|
$tag['options'], |
284
|
|
|
md5($tag['url'] . JsonHelper::encode($tag['options'])) |
285
|
|
|
); |
286
|
|
|
break; |
287
|
|
|
case 'css': |
|
|
|
|
288
|
|
|
$view->registerCssFile( |
289
|
|
|
$tag['url'], |
290
|
|
|
$tag['options'] |
291
|
|
|
); |
292
|
|
|
break; |
293
|
|
|
default: |
|
|
|
|
294
|
|
|
break; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Return the contents of a local file (via path) or remote file (via URL), |
302
|
|
|
* or null if the file doesn't exist or couldn't be fetched |
303
|
|
|
* Yii2 aliases and/or environment variables may be used |
304
|
|
|
* |
305
|
|
|
* @param string $pathOrUrl |
|
|
|
|
306
|
|
|
* @param callable|null $callback |
|
|
|
|
307
|
|
|
* |
308
|
|
|
* @return string|null |
309
|
|
|
*/ |
310
|
|
|
public function fetch(string $pathOrUrl, callable $callback = null) |
311
|
|
|
{ |
312
|
|
|
$pathOrUrl = (string)Craft::parseEnv($pathOrUrl); |
313
|
|
|
// Create the dependency tags |
314
|
|
|
$dependency = new TagDependency([ |
|
|
|
|
315
|
|
|
'tags' => [ |
316
|
|
|
self::CACHE_TAG . $this->cacheKeySuffix, |
317
|
|
|
self::CACHE_TAG . $this->cacheKeySuffix . $pathOrUrl, |
318
|
|
|
], |
319
|
|
|
]); |
|
|
|
|
320
|
|
|
// If this is a file path such as for the `manifest.json`, add a FileDependency so it's cache bust if the file changes |
321
|
|
|
if (!UrlHelper::isAbsoluteUrl($pathOrUrl)) { |
322
|
|
|
$dependency = new ChainedDependency([ |
|
|
|
|
323
|
|
|
'dependencies' => [ |
324
|
|
|
new FileDependency([ |
|
|
|
|
325
|
|
|
'fileName' => $pathOrUrl |
326
|
|
|
]), |
|
|
|
|
327
|
|
|
$dependency |
328
|
|
|
] |
329
|
|
|
]); |
|
|
|
|
330
|
|
|
} |
331
|
|
|
// Set the cache duration based on devMode |
332
|
|
|
$cacheDuration = Craft::$app->getConfig()->getGeneral()->devMode |
333
|
|
|
? self::DEVMODE_CACHE_DURATION |
334
|
|
|
: null; |
335
|
|
|
// Get the result from the cache, or parse the file |
336
|
|
|
$cache = Craft::$app->getCache(); |
337
|
|
|
return $cache->getOrSet( |
338
|
|
|
self::CACHE_KEY . $this->cacheKeySuffix . $pathOrUrl, |
339
|
|
|
function () use ($pathOrUrl, $callback) { |
340
|
|
|
$contents = null; |
341
|
|
|
$result = null; |
342
|
|
|
if (UrlHelper::isAbsoluteUrl($pathOrUrl)) { |
343
|
|
|
// See if we can connect to the server |
344
|
|
|
$clientOptions = [ |
345
|
|
|
RequestOptions::HTTP_ERRORS => false, |
346
|
|
|
RequestOptions::CONNECT_TIMEOUT => 3, |
347
|
|
|
RequestOptions::VERIFY => false, |
348
|
|
|
RequestOptions::TIMEOUT => 5, |
349
|
|
|
]; |
350
|
|
|
$client = new Client($clientOptions); |
351
|
|
|
try { |
352
|
|
|
$response = $client->request('GET', $pathOrUrl, [ |
|
|
|
|
353
|
|
|
RequestOptions::HEADERS => [ |
354
|
|
|
'User-Agent' => self::USER_AGENT_STRING, |
355
|
|
|
'Accept' => '*/*', |
356
|
|
|
], |
357
|
|
|
]); |
|
|
|
|
358
|
|
|
if ($response->getStatusCode() === 200) { |
359
|
|
|
$contents = $response->getBody()->getContents(); |
360
|
|
|
} |
361
|
|
|
} catch (Throwable $e) { |
362
|
|
|
Craft::error($e, __METHOD__); |
363
|
|
|
} |
364
|
|
|
} else { |
365
|
|
|
$contents = @file_get_contents($pathOrUrl); |
366
|
|
|
} |
367
|
|
|
if ($contents) { |
368
|
|
|
$result = $contents; |
369
|
|
|
if ($callback) { |
370
|
|
|
$result = $callback($result); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
return $result; |
375
|
|
|
}, |
376
|
|
|
$cacheDuration, |
377
|
|
|
$dependency |
378
|
|
|
); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Determine whether the Vite dev server is running |
383
|
|
|
* |
384
|
|
|
* @return bool |
385
|
|
|
*/ |
386
|
|
|
public function devServerRunning(): bool |
387
|
|
|
{ |
388
|
|
|
// If the dev server is turned off via config, say it's not running |
389
|
|
|
if (!$this->useDevServer) { |
390
|
|
|
return false; |
391
|
|
|
} |
392
|
|
|
// If we're not supposed to check that the dev server is actually running, just assume it is |
393
|
|
|
if (!$this->checkDevServer) { |
394
|
|
|
return true; |
395
|
|
|
} |
396
|
|
|
// Check to see if the dev server is actually running by pinging it |
397
|
|
|
$url = $this->createUrl($this->devServerInternal, self::VITE_CLIENT); |
398
|
|
|
|
399
|
|
|
return !($this->fetch($url) === null); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Invalidate all of the Vite caches |
404
|
|
|
*/ |
|
|
|
|
405
|
|
|
public function invalidateCaches() |
406
|
|
|
{ |
407
|
|
|
$cache = Craft::$app->getCache(); |
408
|
|
|
TagDependency::invalidate($cache, self::CACHE_TAG . $this->cacheKeySuffix); |
409
|
|
|
Craft::info('All Vite caches cleared', __METHOD__); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
// Protected Methods |
413
|
|
|
// ========================================================================= |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Inject the error entry point JavaScript for auto-reloading of Twig error |
417
|
|
|
* pages |
418
|
|
|
*/ |
|
|
|
|
419
|
|
|
protected function injectErrorEntry() |
420
|
|
|
{ |
421
|
|
|
// If there's no error entry provided, return |
422
|
|
|
if (empty($this->errorEntry)) { |
423
|
|
|
return; |
424
|
|
|
} |
425
|
|
|
// If it's not a server error or a client error, return |
426
|
|
|
$response = Craft::$app->getResponse(); |
427
|
|
|
if (!($response->isServerError || $response->isClientError)) { |
428
|
|
|
return; |
429
|
|
|
} |
430
|
|
|
// If the dev server isn't running, return |
431
|
|
|
if (!$this->devServerRunning()) { |
432
|
|
|
return; |
433
|
|
|
} |
434
|
|
|
// Inject the errorEntry script tags to enable HMR on this page |
435
|
|
|
try { |
436
|
|
|
$errorEntry = $this->errorEntry; |
437
|
|
|
if (is_string($errorEntry)) { |
|
|
|
|
438
|
|
|
$errorEntry = [$errorEntry]; |
439
|
|
|
} |
440
|
|
|
foreach ($errorEntry as $entry) { |
441
|
|
|
$tag = $this->script($entry); |
442
|
|
|
if ($tag !== null) { |
443
|
|
|
echo $tag; |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
} catch (Throwable $e) { |
447
|
|
|
// That's okay, Vite will have already logged the error |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Return an array of tags from the manifest, for both modern and legacy builds |
453
|
|
|
* |
454
|
|
|
* @param string $path |
|
|
|
|
455
|
|
|
* @param bool $asyncCss |
|
|
|
|
456
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
457
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
458
|
|
|
* |
459
|
|
|
* @return array |
460
|
|
|
*/ |
461
|
|
|
protected function manifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): array |
462
|
|
|
{ |
463
|
|
|
// Get the modern tags for this $path |
464
|
|
|
$tags = $this->extractManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
465
|
|
|
// Look for a legacy version of this $path too |
466
|
|
|
$parts = pathinfo($path); |
467
|
|
|
$legacyPath = $parts['dirname'] |
468
|
|
|
. '/' |
469
|
|
|
. $parts['filename'] |
470
|
|
|
. self::LEGACY_EXTENSION |
471
|
|
|
. $parts['extension']; |
472
|
|
|
$legacyTags = $this->extractManifestTags($legacyPath, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
473
|
|
|
// Set a flag to indicate the some legacy gets were found |
474
|
|
|
$legacyPolyfillTags = []; |
475
|
|
|
if (!empty($legacyTags)) { |
476
|
|
|
$this->hasLegacyTags = true; |
477
|
|
|
} |
478
|
|
|
return array_merge( |
479
|
|
|
$legacyPolyfillTags, |
480
|
|
|
$tags, |
481
|
|
|
$legacyTags |
482
|
|
|
); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Return an array of data describing the script, module link, and CSS link tags for the |
487
|
|
|
* script from the manifest.json file |
488
|
|
|
* |
489
|
|
|
* @param string $path |
|
|
|
|
490
|
|
|
* @param bool $asyncCss |
|
|
|
|
491
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
492
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
493
|
|
|
* @param bool $legacy |
|
|
|
|
494
|
|
|
* |
495
|
|
|
* @return array |
496
|
|
|
*/ |
497
|
|
|
protected function extractManifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = [], bool $legacy = false): array |
498
|
|
|
{ |
499
|
|
|
$tags = []; |
500
|
|
|
// Grab the manifest |
501
|
|
|
$pathOrUrl = (string)Craft::parseEnv($this->manifestPath); |
502
|
|
|
$manifest = $this->fetch($pathOrUrl, [JsonHelper::class, 'decodeIfJson']); |
503
|
|
|
// If no manifest file is found, bail |
504
|
|
|
if ($manifest === null) { |
505
|
|
|
Craft::error('Manifest not found at ' . $this->manifestPath, __METHOD__); |
506
|
|
|
|
507
|
|
|
return []; |
508
|
|
|
} |
509
|
|
|
// Set the async CSS args |
510
|
|
|
$asyncCssOptions = []; |
511
|
|
|
if ($asyncCss) { |
512
|
|
|
$asyncCssOptions = [ |
513
|
|
|
'media' => 'print', |
514
|
|
|
'onload' => "this.media='all'", |
515
|
|
|
]; |
516
|
|
|
} |
517
|
|
|
// Set the script args |
518
|
|
|
$scriptOptions = [ |
519
|
|
|
'type' => 'module', |
520
|
|
|
'crossorigin' => true, |
521
|
|
|
]; |
522
|
|
|
if ($legacy) { |
523
|
|
|
$scriptOptions = [ |
524
|
|
|
'type' => 'nomodule', |
525
|
|
|
]; |
526
|
|
|
} |
527
|
|
|
// Iterate through the manifest |
528
|
|
|
/* @var array $manifest */ |
529
|
|
|
foreach ($manifest as $manifestKey => $entry) { |
|
|
|
|
530
|
|
|
// If it's not an entry, skip it |
531
|
|
|
if (!isset($entry['isEntry']) || !$entry['isEntry']) { |
532
|
|
|
continue; |
533
|
|
|
} |
534
|
|
|
// If there's no file, skip it |
535
|
|
|
if (!isset($entry['file'])) { |
536
|
|
|
continue; |
537
|
|
|
} |
538
|
|
|
// If the $path isn't in the $manifestKey, skip it |
539
|
|
|
if (strpos($path, $manifestKey) === false) { |
540
|
|
|
continue; |
541
|
|
|
} |
542
|
|
|
// Include the entry script |
543
|
|
|
$tags[] = [ |
544
|
|
|
'type' => 'file', |
545
|
|
|
'url' => $this->createUrl($this->serverPublic, $entry['file']), |
546
|
|
|
'options' => array_merge($scriptOptions, $scriptTagAttrs) |
547
|
|
|
]; |
548
|
|
|
// Include any CSS tags |
549
|
|
|
$cssFiles = []; |
550
|
|
|
$this->extractCssFiles($manifest, $manifestKey, $cssFiles); |
|
|
|
|
551
|
|
|
foreach ($cssFiles as $cssFile) { |
552
|
|
|
$tags[] = [ |
553
|
|
|
'type' => 'css', |
554
|
|
|
'url' => $this->createUrl($this->serverPublic, $cssFile), |
555
|
|
|
'options' => array_merge([ |
|
|
|
|
556
|
|
|
'rel' => 'stylesheet', |
557
|
|
|
], $asyncCssOptions, $cssTagAttrs) |
|
|
|
|
558
|
|
|
]; |
559
|
|
|
} |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
return $tags; |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* Extract any CSS files from entries recursively |
567
|
|
|
* |
568
|
|
|
* @param array $manifest |
|
|
|
|
569
|
|
|
* @param string $manifestKey |
|
|
|
|
570
|
|
|
* @param array $cssFiles |
|
|
|
|
571
|
|
|
* |
572
|
|
|
* @return array |
573
|
|
|
*/ |
574
|
|
|
protected function extractCssFiles(array $manifest, string $manifestKey, array &$cssFiles): array |
575
|
|
|
{ |
576
|
|
|
$entry = $manifest[$manifestKey] ?? null; |
577
|
|
|
if (!$entry) { |
578
|
|
|
return []; |
579
|
|
|
} |
580
|
|
|
$cssFiles = array_merge($cssFiles, $entry['css'] ?? []); |
581
|
|
|
$imports = array_merge($entry['imports'] ?? [], $entry['dynamicImport'] ?? []); |
582
|
|
|
foreach ($imports as $import) { |
583
|
|
|
$this->extractCssFiles($manifest, $import, $cssFiles); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
return $cssFiles; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Combine a path with a URL to create a URL |
591
|
|
|
* |
592
|
|
|
* @param string $url |
|
|
|
|
593
|
|
|
* @param string $path |
|
|
|
|
594
|
|
|
* |
595
|
|
|
* @return string |
596
|
|
|
*/ |
597
|
|
|
protected function createUrl(string $url, string $path): string |
598
|
|
|
{ |
599
|
|
|
$url = (string)Craft::parseEnv($url); |
600
|
|
|
return rtrim($url, '/') . '/' . trim($path, '/'); |
601
|
|
|
} |
602
|
|
|
} |
603
|
|
|
|