|
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\vite\variables; |
|
12
|
|
|
|
|
13
|
|
|
use craft\base\Component; |
|
14
|
|
|
use nystudio107\pluginvite\variables\ViteVariableInterface; |
|
15
|
|
|
use nystudio107\pluginvite\variables\ViteVariableTrait; |
|
16
|
|
|
|
|
17
|
|
|
use craft\helpers\Template; |
|
18
|
|
|
|
|
19
|
|
|
use Twig\Markup; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
|
|
|
|
|
22
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
23
|
|
|
* @package Vite |
|
|
|
|
|
|
24
|
|
|
* @since 1.0.5 |
|
|
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
|
|
26
|
|
|
class Helper extends Component |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Returns the Critical CSS file for $template wrapped in <style></style> |
|
30
|
|
|
* tags |
|
31
|
|
|
* |
|
32
|
|
|
* @param null|string $name |
|
|
|
|
|
|
33
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
|
|
34
|
|
|
* |
|
35
|
|
|
* @return Markup |
|
36
|
|
|
* @throws \Twig\Error\LoaderError |
|
37
|
|
|
*/ |
|
38
|
|
|
public function includeCriticalCssTags($name = null, array $attributes = []): Markup |
|
39
|
|
|
{ |
|
40
|
|
|
// Resolve the template name |
|
41
|
|
|
$template = Craft::$app->getView()->resolveTemplate($name ?? Twigpack::$templateName ?? ''); |
|
|
|
|
|
|
42
|
|
|
if ($template) { |
|
43
|
|
|
$name = self::combinePaths( |
|
|
|
|
|
|
44
|
|
|
pathinfo($template, PATHINFO_DIRNAME), |
|
|
|
|
|
|
45
|
|
|
pathinfo($template, PATHINFO_FILENAME) |
|
46
|
|
|
); |
|
47
|
|
|
$dirPrefix = 'templates/'; |
|
48
|
|
|
if (defined('CRAFT_TEMPLATES_PATH')) { |
|
49
|
|
|
$dirPrefix = CRAFT_TEMPLATES_PATH; |
|
50
|
|
|
} |
|
51
|
|
|
$name = strstr($name, $dirPrefix); |
|
52
|
|
|
$name = (string)str_replace($dirPrefix, '', $name); |
|
53
|
|
|
$path = self::combinePaths( |
|
54
|
|
|
$config['localFiles']['basePath'], |
|
|
|
|
|
|
55
|
|
|
$config['localFiles']['criticalPrefix'], |
|
|
|
|
|
|
56
|
|
|
$name |
|
|
|
|
|
|
57
|
|
|
) . $config['localFiles']['criticalSuffix']; |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
return self::getCssInlineTags($path, $attributes); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return ''; |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
return Template::raw( |
|
|
|
|
|
|
65
|
|
|
Twigpack::$plugin->manifest->getCriticalCssTags($name, null, $attributes) ?? '' |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Combined the passed in paths, whether file system or URL |
|
71
|
|
|
* |
|
72
|
|
|
* @param string ...$paths |
|
|
|
|
|
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function combinePaths(string ...$paths): string |
|
77
|
|
|
{ |
|
78
|
|
|
$last_key = count($paths) - 1; |
|
79
|
|
|
array_walk($paths, function (&$val, $key) use ($last_key) { |
|
|
|
|
|
|
80
|
|
|
switch ($key) { |
|
81
|
|
|
case 0: |
|
|
|
|
|
|
82
|
|
|
$val = rtrim($val, '/ '); |
|
83
|
|
|
break; |
|
84
|
|
|
case $last_key: |
|
|
|
|
|
|
85
|
|
|
$val = ltrim($val, '/ '); |
|
86
|
|
|
break; |
|
87
|
|
|
default: |
|
|
|
|
|
|
88
|
|
|
$val = trim($val, '/ '); |
|
89
|
|
|
break; |
|
90
|
|
|
} |
|
91
|
|
|
}); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
$first = array_shift($paths); |
|
94
|
|
|
$last = array_pop($paths); |
|
95
|
|
|
$paths = array_filter($paths); |
|
96
|
|
|
array_unshift($paths, $first); |
|
97
|
|
|
$paths[] = $last; |
|
98
|
|
|
|
|
99
|
|
|
return implode('/', $paths); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|