1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace nystudio107\imageoptimize\variables; |
4
|
|
|
|
5
|
|
|
use nystudio107\imageoptimize\helpers\Manifest as ManifestHelper; |
6
|
|
|
use nystudio107\imageoptimize\assetbundles\imageoptimize\ImageOptimizeAsset; |
7
|
|
|
|
8
|
|
|
use Craft; |
9
|
|
|
use craft\helpers\Template; |
10
|
|
|
|
11
|
|
|
class ManifestVariable |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
// Protected Static Properties |
14
|
|
|
// ========================================================================= |
15
|
|
|
|
16
|
|
|
protected static $config = [ |
17
|
|
|
// If `devMode` is on, use webpack-dev-server to all for HMR (hot module reloading) |
18
|
|
|
'useDevServer' => false, |
19
|
|
|
// Manifest names |
20
|
|
|
'manifest' => [ |
21
|
|
|
'legacy' => 'manifest-legacy.json', |
22
|
|
|
'modern' => 'manifest.json', |
23
|
|
|
], |
24
|
|
|
// Public server config |
25
|
|
|
'server' => [ |
26
|
|
|
'manifestPath' => '/', |
27
|
|
|
'publicPath' => '/', |
28
|
|
|
], |
29
|
|
|
// webpack-dev-server config |
30
|
|
|
'devServer' => [ |
31
|
|
|
'manifestPath' => 'http://127.0.0.1:8080', |
32
|
|
|
'publicPath' => '/', |
33
|
|
|
], |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
// Public Methods |
37
|
|
|
// ========================================================================= |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ManifestVariable constructor. |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
ManifestHelper::invalidateCaches(); |
45
|
|
|
$bundle = new ImageOptimizeAsset(); |
46
|
|
|
self::$config['server']['manifestPath'] = Craft::getAlias($bundle->sourcePath); |
47
|
|
|
$useDevServer = getenv('NYS_PLUGIN_DEVSERVER'); |
48
|
|
|
if ($useDevServer !== false) { |
49
|
|
|
self::$config['useDevServer'] = (bool)$useDevServer; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
|
|
|
|
54
|
|
|
* @param string $moduleName |
|
|
|
|
55
|
|
|
* @param bool $async |
|
|
|
|
56
|
|
|
* @param null|array $config |
|
|
|
|
57
|
|
|
* |
58
|
|
|
* @return \Twig_Markup |
59
|
|
|
* @throws \yii\web\NotFoundHttpException |
60
|
|
|
*/ |
61
|
|
|
public function includeCssModule(string $moduleName, bool $async = false, $config = null): \Twig_Markup |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return Template::raw( |
64
|
|
|
ManifestHelper::getCssModuleTags(self::$config, $moduleName, $async) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the CSS file in $path wrapped in <style></style> tags |
70
|
|
|
* |
71
|
|
|
* @param string $path |
|
|
|
|
72
|
|
|
* |
73
|
|
|
* @return \Twig_Markup |
74
|
|
|
*/ |
75
|
|
|
public function includeInlineCssTags(string $path): \Twig_Markup |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return Template::raw( |
78
|
|
|
ManifestHelper::getCssRelPreloadPolyfill() |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns the uglified loadCSS rel=preload Polyfill as per: |
84
|
|
|
* https://github.com/filamentgroup/loadCSS#how-to-use-loadcss-recommended-example |
85
|
|
|
* |
86
|
|
|
* @return \Twig_Markup |
87
|
|
|
*/ |
88
|
|
|
public static function includeCssRelPreloadPolyfill(): \Twig_Markup |
89
|
|
|
{ |
90
|
|
|
return Template::raw( |
91
|
|
|
ManifestHelper::getCssRelPreloadPolyfill() |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
|
|
|
|
96
|
|
|
* @param string $moduleName |
|
|
|
|
97
|
|
|
* @param bool $async |
|
|
|
|
98
|
|
|
* @param null|array $config |
|
|
|
|
99
|
|
|
* |
100
|
|
|
* @return null|\Twig_Markup |
101
|
|
|
* @throws \yii\web\NotFoundHttpException |
102
|
|
|
*/ |
103
|
|
|
public function includeJsModule(string $moduleName, bool $async = false, $config = null) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
return Template::raw( |
106
|
|
|
ManifestHelper::getJsModuleTags(self::$config, $moduleName, $async) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the URI to a module |
112
|
|
|
* |
113
|
|
|
* @param string $moduleName |
|
|
|
|
114
|
|
|
* @param string $type |
|
|
|
|
115
|
|
|
* @param null $config |
|
|
|
|
116
|
|
|
* |
117
|
|
|
* @return null|\Twig_Markup |
118
|
|
|
* @throws \yii\web\NotFoundHttpException |
119
|
|
|
*/ |
120
|
|
|
public function getModuleUri(string $moduleName, string $type = 'modern', $config = null) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
return Template::raw( |
123
|
|
|
ManifestHelper::getModule(self::$config, $moduleName, $type) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Include the Safari 10.1 nomodule fix JavaScript |
129
|
|
|
* |
130
|
|
|
* @return \Twig_Markup |
131
|
|
|
*/ |
132
|
|
|
public function includeSafariNomoduleFix(): \Twig_Markup |
133
|
|
|
{ |
134
|
|
|
return Template::raw( |
135
|
|
|
ManifestHelper::getSafariNomoduleFix() |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the contents of a file from a URI path |
141
|
|
|
* |
142
|
|
|
* @param string $path |
|
|
|
|
143
|
|
|
* |
144
|
|
|
* @return \Twig_Markup |
145
|
|
|
*/ |
146
|
|
|
public function includeFile(string $path): \Twig_Markup |
147
|
|
|
{ |
148
|
|
|
return Template::raw( |
149
|
|
|
ManifestHelper::getFile($path) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns the contents of a file from the $fileName in the manifest |
155
|
|
|
* |
156
|
|
|
* @param string $fileName |
|
|
|
|
157
|
|
|
* @param string $type |
|
|
|
|
158
|
|
|
* @param null $config |
|
|
|
|
159
|
|
|
* |
160
|
|
|
* @return \Twig_Markup |
161
|
|
|
*/ |
162
|
|
|
public function includeFileFromManifest(string $fileName, string $type = 'legacy', $config = null): \Twig_Markup |
163
|
|
|
{ |
164
|
|
|
return Template::raw( |
165
|
|
|
ManifestHelper::getFileFromManifest($fileName, $type, $config) |
|
|
|
|
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|