|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Twigpack plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Twigpack is the conduit between Twig and webpack, with manifest.json & |
|
6
|
|
|
* webpack-dev-server HMR support |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace nystudio107\twigpack\variables; |
|
13
|
|
|
|
|
14
|
|
|
use craft\helpers\Template; |
|
15
|
|
|
use nystudio107\twigpack\Twigpack; |
|
16
|
|
|
use Twig\Error\LoaderError; |
|
17
|
|
|
use Twig\Markup; |
|
18
|
|
|
use yii\web\NotFoundHttpException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
|
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
22
|
|
|
* @package Twigpack |
|
|
|
|
|
|
23
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
|
|
25
|
|
|
class ManifestVariable |
|
26
|
|
|
{ |
|
27
|
|
|
// Public Methods |
|
28
|
|
|
// ========================================================================= |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns the uglified loadCSS rel=preload Polyfill as per: |
|
32
|
|
|
* https://github.com/filamentgroup/loadCSS#how-to-use-loadcss-recommended-example |
|
33
|
|
|
* |
|
34
|
|
|
* @return Markup |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function includeCssRelPreloadPolyfill(): Markup |
|
37
|
|
|
{ |
|
38
|
|
|
return Template::raw( |
|
39
|
|
|
Twigpack::$plugin->manifest->getCssRelPreloadPolyfill() |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
|
|
|
|
|
44
|
|
|
* @param string $moduleName |
|
|
|
|
|
|
45
|
|
|
* @param bool $async |
|
|
|
|
|
|
46
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
|
|
47
|
|
|
* |
|
48
|
|
|
* @return Markup |
|
49
|
|
|
* @throws NotFoundHttpException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function includeCssModule(string $moduleName, bool $async = false, array $attributes = []): Markup |
|
52
|
|
|
{ |
|
53
|
|
|
return Template::raw( |
|
54
|
|
|
Twigpack::$plugin->manifest->getCssModuleTags($moduleName, $async, null, $attributes) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns the CSS file in $path wrapped in <style></style> tags |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $path |
|
|
|
|
|
|
62
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
|
|
63
|
|
|
* |
|
64
|
|
|
* @return Markup |
|
65
|
|
|
*/ |
|
66
|
|
|
public function includeInlineCssTags(string $path, array $attributes = []): Markup |
|
67
|
|
|
{ |
|
68
|
|
|
return Template::raw( |
|
69
|
|
|
Twigpack::$plugin->manifest->getCssInlineTags($path, $attributes) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns the Critical CSS file for $template wrapped in <style></style> |
|
75
|
|
|
* tags |
|
76
|
|
|
* |
|
77
|
|
|
* @param null|string $name |
|
|
|
|
|
|
78
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
|
|
79
|
|
|
* |
|
80
|
|
|
* @return Markup |
|
81
|
|
|
* @throws LoaderError |
|
82
|
|
|
*/ |
|
83
|
|
|
public function includeCriticalCssTags(?string $name = null, array $attributes = []): Markup |
|
84
|
|
|
{ |
|
85
|
|
|
return Template::raw( |
|
86
|
|
|
Twigpack::$plugin->manifest->getCriticalCssTags($name, null, $attributes) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
|
|
|
|
|
91
|
|
|
* @param string $moduleName |
|
|
|
|
|
|
92
|
|
|
* @param bool $async |
|
|
|
|
|
|
93
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
|
|
94
|
|
|
* |
|
95
|
|
|
* @return Markup |
|
96
|
|
|
* @throws NotFoundHttpException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function includeJsModule(string $moduleName, bool $async = false, array $attributes = []): Markup |
|
99
|
|
|
{ |
|
100
|
|
|
return Template::raw( |
|
101
|
|
|
Twigpack::$plugin->manifest->getJsModuleTags($moduleName, $async, null, $attributes) |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Return the URI to a module |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $moduleName |
|
|
|
|
|
|
109
|
|
|
* @param string $type |
|
|
|
|
|
|
110
|
|
|
* @param null $config |
|
|
|
|
|
|
111
|
|
|
* |
|
112
|
|
|
* @return Markup |
|
113
|
|
|
* @throws NotFoundHttpException |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getModuleUri(string $moduleName, string $type = 'modern', $config = null): Markup |
|
116
|
|
|
{ |
|
117
|
|
|
return Template::raw( |
|
118
|
|
|
Twigpack::$plugin->manifest->getModule($moduleName, $type, $config) |
|
|
|
|
|
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Return the HASH value from a module |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $moduleName |
|
|
|
|
|
|
126
|
|
|
* @param string $type |
|
|
|
|
|
|
127
|
|
|
* @param null $config |
|
|
|
|
|
|
128
|
|
|
* |
|
129
|
|
|
* @return Markup |
|
130
|
|
|
* @throws NotFoundHttpException |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getModuleHash(string $moduleName, string $type = 'modern', $config = null): Markup |
|
133
|
|
|
{ |
|
134
|
|
|
return Template::raw( |
|
135
|
|
|
Twigpack::$plugin->manifest->getModuleHash($moduleName, $type, $config) |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Include the Safari 10.1 nomodule fix JavaScript |
|
141
|
|
|
* |
|
142
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
143
|
|
|
* |
|
144
|
|
|
* @return Markup |
|
145
|
|
|
*/ |
|
146
|
|
|
public function includeSafariNomoduleFix(array $attributes = []): Markup |
|
147
|
|
|
{ |
|
148
|
|
|
return Template::raw( |
|
149
|
|
|
Twigpack::$plugin->manifest->getSafariNomoduleFix($attributes) |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns the contents of a file from a URI path |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $path |
|
|
|
|
|
|
157
|
|
|
* |
|
158
|
|
|
* @return Markup |
|
159
|
|
|
*/ |
|
160
|
|
|
public function includeFile(string $path): Markup |
|
161
|
|
|
{ |
|
162
|
|
|
return Template::raw( |
|
163
|
|
|
Twigpack::$plugin->manifest->getFile($path) |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Returns the contents of a file from the $fileName in the manifest |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $fileName |
|
|
|
|
|
|
171
|
|
|
* @param string $type |
|
|
|
|
|
|
172
|
|
|
* @param null $config |
|
|
|
|
|
|
173
|
|
|
* |
|
174
|
|
|
* @return Markup |
|
175
|
|
|
*/ |
|
176
|
|
|
public function includeFileFromManifest(string $fileName, string $type = 'legacy', $config = null): Markup |
|
177
|
|
|
{ |
|
178
|
|
|
return Template::raw( |
|
179
|
|
|
Twigpack::$plugin->manifest->getFileFromManifest($fileName, $type, $config) |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|