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\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\web\View; |
18
|
|
|
use nystudio107\pluginvite\helpers\FileHelper; |
19
|
|
|
use nystudio107\pluginvite\helpers\ManifestHelper; |
20
|
|
|
use Throwable; |
21
|
|
|
use yii\base\InvalidConfigException; |
22
|
|
|
use yii\caching\TagDependency; |
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @author nystudio107 |
|
|
|
|
26
|
|
|
* @package Vite |
|
|
|
|
27
|
|
|
* @since 1.0.0 |
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
class ViteService extends Component |
30
|
|
|
{ |
31
|
|
|
// Constants |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
protected const VITE_CLIENT = '@vite/client'; |
35
|
|
|
protected const VITE_DEVSERVER_PING = '__vite_ping'; |
36
|
|
|
protected const LEGACY_POLYFILLS = 'vite/legacy-polyfills'; |
37
|
|
|
|
38
|
|
|
// Public Properties |
39
|
|
|
// ========================================================================= |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @var bool Should the dev server be used for? |
43
|
|
|
*/ |
44
|
|
|
public bool $useDevServer = false; |
45
|
|
|
|
46
|
|
|
/** |
|
|
|
|
47
|
|
|
* @var string File system path (or URL) to the Vite-built manifest.json |
48
|
|
|
*/ |
49
|
|
|
public string $manifestPath = ''; |
50
|
|
|
|
51
|
|
|
/** |
|
|
|
|
52
|
|
|
* @var string The public URL to the dev server (what appears in `<script src="">` tags |
53
|
|
|
*/ |
54
|
|
|
public string $devServerPublic = ''; |
55
|
|
|
|
56
|
|
|
/** |
|
|
|
|
57
|
|
|
* @var string The public URL to use when not using the dev server |
58
|
|
|
*/ |
59
|
|
|
public string $serverPublic = ''; |
60
|
|
|
|
61
|
|
|
/** |
|
|
|
|
62
|
|
|
* @var array|string The JavaScript entry from the manifest.json to inject on Twig error pages |
63
|
|
|
* This can be a string or an array of strings |
64
|
|
|
*/ |
65
|
|
|
public array|string $errorEntry = ''; |
66
|
|
|
|
67
|
|
|
/** |
|
|
|
|
68
|
|
|
* @var string String to be appended to the cache key |
69
|
|
|
*/ |
70
|
|
|
public string $cacheKeySuffix = ''; |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @var string The internal URL to the dev server, when accessed from the environment in which PHP is executing |
74
|
|
|
* This can be the same as `$devServerPublic`, but may be different in containerized or VM setups. |
75
|
|
|
* ONLY used if $checkDevServer = true |
76
|
|
|
*/ |
77
|
|
|
public string $devServerInternal = ''; |
78
|
|
|
|
79
|
|
|
/** |
|
|
|
|
80
|
|
|
* @var bool Should we check for the presence of the dev server by pinging $devServerInternal to make sure it's running? |
81
|
|
|
*/ |
82
|
|
|
public bool $checkDevServer = false; |
83
|
|
|
|
84
|
|
|
/** |
|
|
|
|
85
|
|
|
* @var bool Whether the react-refresh-shim should be included |
86
|
|
|
*/ |
87
|
|
|
public bool $includeReactRefreshShim = false; |
88
|
|
|
|
89
|
|
|
/** |
|
|
|
|
90
|
|
|
* @var bool Whether the modulepreload-polyfill shim should be included |
91
|
|
|
*/ |
92
|
|
|
public bool $includeModulePreloadShim = true; |
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* @var bool Whether an onload handler should be added to <script> tags to fire a custom event when the script has loaded |
96
|
|
|
*/ |
97
|
|
|
public bool $includeScriptOnloadHandler = true; |
98
|
|
|
|
99
|
|
|
// Protected Properties |
100
|
|
|
// ========================================================================= |
101
|
|
|
|
102
|
|
|
/** |
|
|
|
|
103
|
|
|
* @var bool Whether the manifest shims has been included yet or not |
104
|
|
|
*/ |
105
|
|
|
protected bool $manifestShimsIncluded = false; |
106
|
|
|
|
107
|
|
|
/** |
|
|
|
|
108
|
|
|
* @var bool Whether the dev server shims has been included yet or not |
109
|
|
|
*/ |
110
|
|
|
protected bool $devServerShimsIncluded = false; |
111
|
|
|
|
112
|
|
|
/** |
|
|
|
|
113
|
|
|
* @var bool Cached status of whether the devServer is running or not |
114
|
|
|
*/ |
115
|
|
|
protected ?bool $devServerRunningCached = null; |
116
|
|
|
|
117
|
|
|
// Public Methods |
118
|
|
|
// ========================================================================= |
119
|
|
|
|
120
|
|
|
/** |
|
|
|
|
121
|
|
|
* @inheritDoc |
122
|
|
|
*/ |
|
|
|
|
123
|
|
|
public function init(): void |
124
|
|
|
{ |
125
|
|
|
parent::init(); |
126
|
|
|
// Do nothing for console requests |
127
|
|
|
$request = Craft::$app->getRequest(); |
128
|
|
|
if ($request->getIsConsoleRequest()) { |
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
// Our component is lazily loaded, so the View will be instantiated by now |
132
|
|
|
if ($this->devServerRunning() && Craft::$app->getConfig()->getGeneral()->devMode) { |
133
|
|
|
Craft::$app->getView()->on(View::EVENT_END_BODY, [$this, 'injectErrorEntry']); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or |
139
|
|
|
* extracting it from the manifest.json file |
140
|
|
|
* |
141
|
|
|
* @param string $path |
|
|
|
|
142
|
|
|
* @param bool $asyncCss |
|
|
|
|
143
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
144
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
* @throws InvalidConfigException |
148
|
|
|
*/ |
149
|
|
|
public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): void |
150
|
|
|
{ |
151
|
|
|
// Filter out empty attributes, but preserve boolean values |
152
|
|
|
$preserveBools = fn($value) => is_bool($value) || !empty($value); |
153
|
|
|
$scriptTagAttrs = array_filter($scriptTagAttrs, $preserveBools); |
154
|
|
|
$cssTagAttrs = array_filter($cssTagAttrs, $preserveBools); |
155
|
|
|
|
156
|
|
|
if ($this->devServerRunning()) { |
157
|
|
|
$this->devServerRegister($path, $scriptTagAttrs); |
158
|
|
|
|
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->manifestRegister($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Determine whether the Vite dev server is running |
167
|
|
|
* |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
public function devServerRunning(): bool |
171
|
|
|
{ |
172
|
|
|
if ($this->devServerRunningCached !== null) { |
173
|
|
|
return $this->devServerRunningCached; |
174
|
|
|
} |
175
|
|
|
// If the dev server is turned off via config, say it's not running |
176
|
|
|
if (!$this->useDevServer) { |
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
// If we're not supposed to check that the dev server is actually running, just assume it is |
180
|
|
|
if (!$this->checkDevServer) { |
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
// Check to see if the dev server is actually running by pinging it |
184
|
|
|
$url = FileHelper::createUrl($this->devServerInternal, self::VITE_DEVSERVER_PING); |
185
|
|
|
$response = FileHelper::fetchResponse($url); |
186
|
|
|
$this->devServerRunningCached = false; |
187
|
|
|
// Status code of 200 or 404 means the dev server is running |
188
|
|
|
if ($response) { |
|
|
|
|
189
|
|
|
$statusCode = $response->getStatusCode(); |
190
|
|
|
$this->devServerRunningCached = $statusCode === 200 || $statusCode === 404; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this->devServerRunningCached; |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Return the contents of a local file (via path) or remote file (via URL), |
198
|
|
|
* or null if the file doesn't exist or couldn't be fetched |
199
|
|
|
* Yii2 aliases and/or environment variables may be used |
200
|
|
|
* |
201
|
|
|
* @param string $pathOrUrl |
|
|
|
|
202
|
|
|
* @param callable|null $callback |
|
|
|
|
203
|
|
|
* |
204
|
|
|
* @return string|array|null |
205
|
|
|
*/ |
206
|
|
|
public function fetch(string $pathOrUrl, callable $callback = null): string|array|null |
207
|
|
|
{ |
208
|
|
|
// If the devServer is running, and this is a URL, swap in the devServerInternal URL |
209
|
|
|
if ($this->devServerRunning()) { |
210
|
|
|
if (!filter_var($pathOrUrl, FILTER_VALIDATE_URL) === false) { |
211
|
|
|
$devServerInternalUrl = str_replace($this->devServerPublic, '', $pathOrUrl); |
212
|
|
|
$devServerInternalUrl = FileHelper::createUrl($this->devServerInternal, $devServerInternalUrl); |
213
|
|
|
// If we get a result from the $devServerInternalUrl, return it |
214
|
|
|
if ($devServerInternalResult = FileHelper::fetch($devServerInternalUrl, $callback, $this->cacheKeySuffix)) { |
215
|
|
|
return $devServerInternalResult; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return FileHelper::fetch($pathOrUrl, $callback, $this->cacheKeySuffix); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Register the script tag to the Craft View to load the script from the Vite dev server |
225
|
|
|
* |
226
|
|
|
* @param string $path |
|
|
|
|
227
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
228
|
|
|
* |
229
|
|
|
* @return void |
230
|
|
|
* @throws InvalidConfigException |
231
|
|
|
*/ |
232
|
|
|
public function devServerRegister(string $path, array $scriptTagAttrs = []): void |
233
|
|
|
{ |
234
|
|
|
$view = Craft::$app->getView(); |
235
|
|
|
// Include any dev server shims |
236
|
|
|
if (!$this->devServerShimsIncluded) { |
237
|
|
|
// Include the react-refresh-shim |
238
|
|
|
if ($this->includeReactRefreshShim) { |
239
|
|
|
$script = FileHelper::fetchScript('react-refresh-shim.min.js', $this->cacheKeySuffix); |
240
|
|
|
// Replace the hard-coded dev server URL with whatever they have theirs set to |
241
|
|
|
$script = str_replace( |
242
|
|
|
'http://localhost:3000/', |
243
|
|
|
rtrim($this->devServerPublic, '/') . '/', |
244
|
|
|
$script |
245
|
|
|
); |
246
|
|
|
$view->registerScript( |
247
|
|
|
$script, |
248
|
|
|
$view::POS_HEAD, |
249
|
|
|
[ |
250
|
|
|
'type' => 'module', |
251
|
|
|
], |
252
|
|
|
'REACT_REFRESH_SHIM' |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
$this->devServerShimsIncluded = true; |
256
|
|
|
} |
257
|
|
|
// Include the entry script |
258
|
|
|
$url = FileHelper::createUrl($this->devServerPublic, $path); |
259
|
|
|
$view->registerJsFile( |
260
|
|
|
$url, |
261
|
|
|
array_merge(['type' => 'module'], $scriptTagAttrs), |
262
|
|
|
md5($url . JsonHelper::encode($scriptTagAttrs)) |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Register the script, module link, and CSS link tags to the Craft View for the script from the manifest.json file |
268
|
|
|
* |
269
|
|
|
* @param string $path |
|
|
|
|
270
|
|
|
* @param bool $asyncCss |
|
|
|
|
271
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
272
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
273
|
|
|
* |
274
|
|
|
* @return void |
275
|
|
|
* @throws InvalidConfigException |
276
|
|
|
*/ |
277
|
|
|
public function manifestRegister(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): void |
278
|
|
|
{ |
279
|
|
|
$view = Craft::$app->getView(); |
280
|
|
|
ManifestHelper::fetchManifest($this->manifestPath); |
281
|
|
|
$tags = ManifestHelper::manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
282
|
|
|
$legacyTags = ManifestHelper::legacyManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
283
|
|
|
// Include any manifest shims |
284
|
|
|
if (!$this->manifestShimsIncluded) { |
285
|
|
|
// Handle the modulepreload-polyfill shim |
286
|
|
|
if ($this->includeModulePreloadShim) { |
287
|
|
|
$view->registerScript( |
288
|
|
|
FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix), |
289
|
|
|
$view::POS_HEAD, |
290
|
|
|
['type' => 'module'], |
291
|
|
|
'MODULEPRELOAD_POLYFILL' |
292
|
|
|
); |
293
|
|
|
} |
294
|
|
|
// Handle any legacy polyfills |
295
|
|
|
if (!empty($legacyTags)) { |
296
|
|
|
$view->registerScript( |
297
|
|
|
FileHelper::fetchScript('safari-nomodule-fix.min.js', $this->cacheKeySuffix), |
298
|
|
|
$view::POS_HEAD, |
299
|
|
|
[], |
300
|
|
|
'SAFARI_NOMODULE_FIX' |
301
|
|
|
); |
302
|
|
|
$legacyPolyfillTags = ManifestHelper::extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
303
|
|
|
$tags = array_merge($legacyPolyfillTags, $tags); |
304
|
|
|
} |
305
|
|
|
$this->manifestShimsIncluded = true; |
306
|
|
|
} |
307
|
|
|
$this->manifestRegisterTags($tags, $legacyTags); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Return the URL for the given entry |
312
|
|
|
* |
313
|
|
|
* @param string $path |
|
|
|
|
314
|
|
|
* |
315
|
|
|
* @return string |
316
|
|
|
*/ |
317
|
|
|
public function entry(string $path): string |
318
|
|
|
{ |
319
|
|
|
ManifestHelper::fetchManifest($this->manifestPath); |
320
|
|
|
$entry = ManifestHelper::extractEntry($path); |
321
|
|
|
|
322
|
|
|
return FileHelper::createUrl($this->serverPublic, $entry); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Return the integrity hash (or an empty string if not present) for the given entry |
327
|
|
|
* |
328
|
|
|
* @param string $path |
|
|
|
|
329
|
|
|
* |
330
|
|
|
* @return string |
331
|
|
|
*/ |
332
|
|
|
public function integrity(string $path): string |
333
|
|
|
{ |
334
|
|
|
ManifestHelper::fetchManifest($this->manifestPath); |
335
|
|
|
|
336
|
|
|
return ManifestHelper::extractIntegrity($path); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
|
|
|
|
340
|
|
|
* Return the URL for the given asset |
341
|
|
|
* |
342
|
|
|
* @param string $path |
|
|
|
|
343
|
|
|
* |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
|
|
public function asset(string $path, bool $public = false): string |
347
|
|
|
{ |
348
|
|
|
if ($this->devServerRunning()) { |
349
|
|
|
return $this->devServerAsset($path); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
if ($public) { |
353
|
|
|
return $this->publicAsset($path); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return $this->manifestAsset($path); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Return the URL for the asset from the Vite dev server |
361
|
|
|
* |
362
|
|
|
* @param string $path |
|
|
|
|
363
|
|
|
* |
364
|
|
|
* @return string |
365
|
|
|
*/ |
366
|
|
|
public function devServerAsset(string $path): string |
367
|
|
|
{ |
368
|
|
|
// Return a URL to the given asset |
369
|
|
|
return FileHelper::createUrl($this->devServerPublic, $path); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Return the URL for the asset from the public Vite folder |
374
|
|
|
* |
375
|
|
|
* @param string $path |
|
|
|
|
376
|
|
|
* |
377
|
|
|
* @return string |
378
|
|
|
*/ |
379
|
|
|
public function publicAsset(string $path): string |
380
|
|
|
{ |
381
|
|
|
// Return a URL to the given asset |
382
|
|
|
return FileHelper::createUrl($this->serverPublic, $path); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Return the URL for the asset from the manifest.json file |
387
|
|
|
* |
388
|
|
|
* @param string $path |
|
|
|
|
389
|
|
|
* |
390
|
|
|
* @return string |
391
|
|
|
*/ |
392
|
|
|
public function manifestAsset(string $path): string |
393
|
|
|
{ |
394
|
|
|
ManifestHelper::fetchManifest($this->manifestPath); |
395
|
|
|
$assets = ManifestHelper::extractAssetFiles(); |
396
|
|
|
// Get just the file name |
397
|
|
|
$assetKeyParts = explode('/', $path); |
398
|
|
|
$assetKey = end($assetKeyParts); |
399
|
|
|
foreach ($assets as $key => $value) { |
400
|
|
|
if ($key === $assetKey) { |
401
|
|
|
return FileHelper::createUrl($this->serverPublic, $value); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
// With Vite 3.x or later, the assets are also included as top-level entries in the |
406
|
|
|
// manifest, so check there, too |
407
|
|
|
$entry = ManifestHelper::extractEntry($path); |
408
|
|
|
|
409
|
|
|
return $entry === '' ? '' : FileHelper::createUrl($this->serverPublic, $entry); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Invalidate all of the Vite caches |
414
|
|
|
*/ |
|
|
|
|
415
|
|
|
public function invalidateCaches(): void |
416
|
|
|
{ |
417
|
|
|
$cache = Craft::$app->getCache(); |
418
|
|
|
TagDependency::invalidate($cache, FileHelper::CACHE_TAG . $this->cacheKeySuffix); |
419
|
|
|
Craft::info('All Vite caches cleared', __METHOD__); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Return the appropriate tags to load the Vite script, either via the dev server or |
424
|
|
|
* extracting it from the manifest.json file |
425
|
|
|
* |
426
|
|
|
* @param string $path |
|
|
|
|
427
|
|
|
* @param bool $asyncCss |
|
|
|
|
428
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
429
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
430
|
|
|
* |
431
|
|
|
* @return string |
432
|
|
|
*/ |
433
|
|
|
public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string |
434
|
|
|
{ |
435
|
|
|
// Filter out empty attributes, but preserve boolean values |
436
|
|
|
$preserveBools = fn($value) => is_bool($value) || !empty($value); |
437
|
|
|
$scriptTagAttrs = array_filter($scriptTagAttrs, $preserveBools); |
438
|
|
|
$cssTagAttrs = array_filter($cssTagAttrs, $preserveBools); |
439
|
|
|
|
440
|
|
|
if ($this->devServerRunning()) { |
441
|
|
|
return $this->devServerScript($path, $scriptTagAttrs); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
return $this->manifestScript($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Return the script tag to load the script from the Vite dev server |
449
|
|
|
* |
450
|
|
|
* @param string $path |
|
|
|
|
451
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
452
|
|
|
* |
453
|
|
|
* @return string |
454
|
|
|
*/ |
455
|
|
|
public function devServerScript(string $path, array $scriptTagAttrs = []): string |
456
|
|
|
{ |
457
|
|
|
$lines = []; |
458
|
|
|
// Include any dev server shims |
459
|
|
|
if (!$this->devServerShimsIncluded) { |
460
|
|
|
// Include the react-refresh-shim |
461
|
|
|
if ($this->includeReactRefreshShim) { |
462
|
|
|
$script = FileHelper::fetchScript('react-refresh-shim.min.js', $this->cacheKeySuffix); |
463
|
|
|
// Replace the hard-coded dev server URL with whatever they have theirs set to |
464
|
|
|
$script = str_replace( |
465
|
|
|
'http://localhost:3000/', |
466
|
|
|
rtrim($this->devServerPublic, '/') . '/', |
467
|
|
|
$script |
468
|
|
|
); |
469
|
|
|
$lines[] = HtmlHelper::script( |
470
|
|
|
$script, |
471
|
|
|
[ |
472
|
|
|
'type' => 'module', |
473
|
|
|
] |
474
|
|
|
); |
475
|
|
|
} |
476
|
|
|
$this->devServerShimsIncluded = true; |
477
|
|
|
} |
478
|
|
|
// Include the entry script |
479
|
|
|
$url = FileHelper::createUrl($this->devServerPublic, $path); |
480
|
|
|
$lines[] = HtmlHelper::jsFile($url, array_merge([ |
|
|
|
|
481
|
|
|
'type' => 'module', |
482
|
|
|
], $scriptTagAttrs)); |
|
|
|
|
483
|
|
|
|
484
|
|
|
return implode("\r\n", $lines); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Return the script, module link, and CSS link tags for the script from the manifest.json file |
489
|
|
|
* |
490
|
|
|
* @param string $path |
|
|
|
|
491
|
|
|
* @param bool $asyncCss |
|
|
|
|
492
|
|
|
* @param array $scriptTagAttrs |
|
|
|
|
493
|
|
|
* @param array $cssTagAttrs |
|
|
|
|
494
|
|
|
* |
495
|
|
|
* @return string |
496
|
|
|
*/ |
497
|
|
|
public function manifestScript(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string |
498
|
|
|
{ |
499
|
|
|
$lines = []; |
500
|
|
|
ManifestHelper::fetchManifest($this->manifestPath); |
501
|
|
|
$tags = ManifestHelper::manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
502
|
|
|
$legacyTags = ManifestHelper::legacyManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
503
|
|
|
// Include any manifest shims |
504
|
|
|
if (!$this->manifestShimsIncluded) { |
505
|
|
|
// Handle the modulepreload-polyfill shim |
506
|
|
|
if ($this->includeModulePreloadShim) { |
507
|
|
|
$lines[] = HtmlHelper::script( |
508
|
|
|
FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix), |
509
|
|
|
['type' => 'module'] |
510
|
|
|
); |
511
|
|
|
} |
512
|
|
|
// Handle any legacy polyfills |
513
|
|
|
if (!empty($legacyTags)) { |
514
|
|
|
$lines[] = HtmlHelper::script( |
515
|
|
|
FileHelper::fetchScript('safari-nomodule-fix.min.js', $this->cacheKeySuffix), |
516
|
|
|
[] |
517
|
|
|
); |
518
|
|
|
$legacyPolyfillTags = ManifestHelper::extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
519
|
|
|
$tags = array_merge($legacyPolyfillTags, $tags); |
520
|
|
|
} |
521
|
|
|
$this->manifestShimsIncluded = true; |
522
|
|
|
} |
523
|
|
|
$lines = array_merge($lines, $this->manifestScriptTags($tags, $legacyTags)); |
524
|
|
|
|
525
|
|
|
return implode("\r\n", $lines); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
// Protected Methods |
529
|
|
|
// ========================================================================= |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Iterate through all the tags, and register them |
533
|
|
|
* |
534
|
|
|
* @param array $tags |
|
|
|
|
535
|
|
|
* @param array $legacyTags |
|
|
|
|
536
|
|
|
* @throws InvalidConfigException |
|
|
|
|
537
|
|
|
*/ |
|
|
|
|
538
|
|
|
protected function manifestRegisterTags(array $tags, array $legacyTags): void |
539
|
|
|
{ |
540
|
|
|
$view = Craft::$app->getView(); |
541
|
|
|
foreach (array_merge($tags, $legacyTags) as $tag) { |
542
|
|
|
if (!empty($tag)) { |
543
|
|
|
$url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
544
|
|
|
switch ($tag['type']) { |
545
|
|
|
case 'file': |
|
|
|
|
546
|
|
|
if (!$this->includeScriptOnloadHandler) { |
|
|
|
|
547
|
|
|
unset($tag['options']['onload']); |
548
|
|
|
} |
|
|
|
|
549
|
|
|
$view->registerJsFile( |
550
|
|
|
$url, |
551
|
|
|
$tag['options'], |
552
|
|
|
md5($url . JsonHelper::encode($tag['options'])) |
553
|
|
|
); |
554
|
|
|
break; |
555
|
|
|
case 'css': |
|
|
|
|
556
|
|
|
$view->registerCssFile( |
557
|
|
|
$url, |
558
|
|
|
$tag['options'] |
559
|
|
|
); |
560
|
|
|
break; |
561
|
|
|
case 'import': |
|
|
|
|
562
|
|
|
$view->registerLinkTag( |
563
|
|
|
array_filter([ |
|
|
|
|
564
|
|
|
'crossorigin' => $tag['crossorigin'], |
565
|
|
|
'href' => $url, |
566
|
|
|
'rel' => 'modulepreload', |
567
|
|
|
'integrity' => $tag['integrity'] ?? '', |
568
|
|
|
]), |
|
|
|
|
569
|
|
|
md5($url) |
570
|
|
|
); |
571
|
|
|
break; |
572
|
|
|
default: |
|
|
|
|
573
|
|
|
break; |
574
|
|
|
} |
575
|
|
|
} |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Inject the error entry point JavaScript for auto-reloading of Twig error |
581
|
|
|
* pages |
582
|
|
|
*/ |
|
|
|
|
583
|
|
|
protected function injectErrorEntry(): void |
584
|
|
|
{ |
585
|
|
|
// If there's no error entry provided, return |
586
|
|
|
if (empty($this->errorEntry)) { |
587
|
|
|
return; |
588
|
|
|
} |
589
|
|
|
// If it's not a server error or a client error, return |
590
|
|
|
$response = Craft::$app->getResponse(); |
591
|
|
|
if (!($response->isServerError || $response->isClientError)) { |
592
|
|
|
return; |
593
|
|
|
} |
594
|
|
|
// If the dev server isn't running, return |
595
|
|
|
if (!$this->devServerRunning()) { |
596
|
|
|
return; |
597
|
|
|
} |
598
|
|
|
// Inject the errorEntry script tags to enable HMR on this page |
599
|
|
|
try { |
600
|
|
|
$errorEntry = $this->errorEntry; |
601
|
|
|
if (is_string($errorEntry)) { |
602
|
|
|
$errorEntry = [$errorEntry]; |
603
|
|
|
} |
604
|
|
|
foreach ($errorEntry as $entry) { |
605
|
|
|
$tag = $this->script($entry); |
606
|
|
|
if ($tag !== null) { |
607
|
|
|
echo $tag; |
608
|
|
|
} |
609
|
|
|
} |
610
|
|
|
} catch (Throwable $e) { |
611
|
|
|
// That's okay, Vite will have already logged the error |
612
|
|
|
} |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* Iterate through all the tags, and return them |
617
|
|
|
* |
618
|
|
|
* @param array $tags |
|
|
|
|
619
|
|
|
* @param array $legacyTags |
|
|
|
|
620
|
|
|
* @return array |
|
|
|
|
621
|
|
|
*/ |
622
|
|
|
protected function manifestScriptTags(array $tags, array $legacyTags): array |
623
|
|
|
{ |
624
|
|
|
$lines = []; |
625
|
|
|
foreach (array_merge($tags, $legacyTags) as $tag) { |
626
|
|
|
if (!empty($tag)) { |
627
|
|
|
$url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
628
|
|
|
switch ($tag['type']) { |
629
|
|
|
case 'file': |
|
|
|
|
630
|
|
|
if (!$this->includeScriptOnloadHandler) { |
|
|
|
|
631
|
|
|
unset($tag['options']['onload']); |
632
|
|
|
} |
|
|
|
|
633
|
|
|
$lines[] = HtmlHelper::jsFile($url, $tag['options']); |
634
|
|
|
break; |
635
|
|
|
case 'css': |
|
|
|
|
636
|
|
|
$lines[] = HtmlHelper::cssFile($url, $tag['options']); |
637
|
|
|
break; |
638
|
|
|
case 'import': |
|
|
|
|
639
|
|
|
$lines[] = HtmlHelper::tag('link', '', array_filter([ |
|
|
|
|
640
|
|
|
'crossorigin' => $tag['crossorigin'], |
641
|
|
|
'href' => $url, |
642
|
|
|
'rel' => 'modulepreload', |
643
|
|
|
'integrity' => $tag['integrity'] ?? '', |
644
|
|
|
])); |
|
|
|
|
645
|
|
|
break; |
646
|
|
|
default: |
|
|
|
|
647
|
|
|
break; |
648
|
|
|
} |
649
|
|
|
} |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
return $lines; |
653
|
|
|
} |
654
|
|
|
} |
655
|
|
|
|