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\vite\services; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\Component; |
15
|
|
|
use craft\helpers\Html; |
16
|
|
|
use nystudio107\pluginvite\helpers\FileHelper; |
17
|
|
|
use nystudio107\pluginvite\helpers\ManifestHelper; |
18
|
|
|
use nystudio107\vite\models\Settings; |
19
|
|
|
use nystudio107\vite\Vite; |
20
|
|
|
use Twig\Error\LoaderError; |
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package Vite |
|
|
|
|
25
|
|
|
* @since 1.0.5 |
|
|
|
|
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class Helper extends Component |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Returns the Critical CSS file for $template wrapped in <style></style> |
31
|
|
|
* tags |
32
|
|
|
* |
33
|
|
|
* @param null|string $name |
|
|
|
|
34
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
* @throws LoaderError |
38
|
|
|
*/ |
39
|
|
|
public function getCriticalCssTags(?string $name = null, array $attributes = []): string |
40
|
|
|
{ |
41
|
|
|
// Resolve the template name |
42
|
|
|
$template = Craft::$app->getView()->resolveTemplate($name ?? Vite::$templateName ?? ''); |
43
|
|
|
if ($template) { |
44
|
|
|
/** @var Settings $settings */ |
|
|
|
|
45
|
|
|
$settings = Vite::$plugin->getSettings(); |
46
|
|
|
$name = FileHelper::createUrl( |
47
|
|
|
pathinfo($template, PATHINFO_DIRNAME), |
|
|
|
|
48
|
|
|
pathinfo($template, PATHINFO_FILENAME) |
|
|
|
|
49
|
|
|
); |
50
|
|
|
$dirPrefix = 'templates/'; |
51
|
|
|
if (defined('CRAFT_TEMPLATES_PATH')) { |
52
|
|
|
$dirPrefix = CRAFT_TEMPLATES_PATH; |
53
|
|
|
} |
54
|
|
|
$name = strstr($name, $dirPrefix); |
55
|
|
|
$pos = strpos($name, $dirPrefix); |
56
|
|
|
if ($pos !== false) { |
57
|
|
|
$name = (string)substr_replace($name, '', $pos, strlen($dirPrefix)); |
58
|
|
|
} |
59
|
|
|
$path = FileHelper::createUrl( |
60
|
|
|
$settings->criticalPath, |
|
|
|
|
61
|
|
|
$name |
|
|
|
|
62
|
|
|
) . $settings->criticalSuffix; |
|
|
|
|
63
|
|
|
|
64
|
|
|
return $this->getCssInlineTags($path, $attributes); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return ''; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the passed in CSS file at the specified file system path or URL, wrapped in |
72
|
|
|
* <style></style> tags |
73
|
|
|
* |
74
|
|
|
* @param string $path |
|
|
|
|
75
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getCssInlineTags(string $path, array $attributes = []): string |
80
|
|
|
{ |
81
|
|
|
/** @var Settings $settings */ |
|
|
|
|
82
|
|
|
$settings = Vite::$plugin->getSettings(); |
83
|
|
|
$result = FileHelper::fetch($path, null, $settings->cacheKeySuffix); |
84
|
|
|
if ($result) { |
85
|
|
|
$config = []; |
86
|
|
|
|
87
|
|
|
return Html::style($result, array_merge($config, $attributes)); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return ''; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return the hash value for the first CSS file bundled with the module specified via $path |
95
|
|
|
* |
96
|
|
|
* @param $path |
|
|
|
|
97
|
|
|
* @return string |
|
|
|
|
98
|
|
|
*/ |
99
|
|
|
public function getCssHash($path): string |
100
|
|
|
{ |
101
|
|
|
/** @var Settings $settings */ |
|
|
|
|
102
|
|
|
$settings = Vite::$plugin->getSettings(); |
103
|
|
|
ManifestHelper::fetchManifest($settings->manifestPath); |
104
|
|
|
$tags = ManifestHelper::manifestTags($path, false); |
105
|
|
|
foreach ($tags as $tag) { |
106
|
|
|
if (!empty($tag) && $tag['type'] === 'css') { |
107
|
|
|
// Extract only the Hash Value |
108
|
|
|
$modulePath = pathinfo($tag['url']); |
109
|
|
|
$moduleFilename = $modulePath['filename']; |
110
|
|
|
$hashPos = strpos($moduleFilename, '.') ?: strlen($moduleFilename); |
111
|
|
|
$moduleHash = substr($moduleFilename, $hashPos + 1); |
112
|
|
|
// Vite 5 now uses a `-` to separate the version hash, so account for that as well |
113
|
|
|
if (empty($moduleHash) && str_contains($moduleFilename, '-')) { |
114
|
|
|
$moduleHash = substr($moduleFilename, strpos($moduleFilename, '-') + 1); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $moduleHash; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return ''; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|