1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace nystudio107\webperf\variables; |
4
|
|
|
|
5
|
|
|
use nystudio107\webperf\assetbundles\webperf\WebperfAsset; |
6
|
|
|
use nystudio107\webperf\helpers\Manifest as ManifestHelper; |
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.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 WebperfAsset(); |
46
|
|
|
$baseAssetsUrl = Craft::$app->assetManager->getPublishedUrl( |
47
|
|
|
$bundle->sourcePath, |
48
|
|
|
true |
49
|
|
|
); |
50
|
|
|
self::$config['server']['manifestPath'] = Craft::getAlias($bundle->sourcePath); |
51
|
|
|
self::$config['server']['publicPath'] = $baseAssetsUrl; |
52
|
|
|
$useDevServer = getenv('NYS_PLUGIN_DEVSERVER'); |
53
|
|
|
if ($useDevServer !== false) { |
54
|
|
|
self::$config['useDevServer'] = (bool)$useDevServer; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* @param string $moduleName |
|
|
|
|
60
|
|
|
* @param bool $async |
|
|
|
|
61
|
|
|
* @param null|array $config |
|
|
|
|
62
|
|
|
* |
63
|
|
|
* @return \Twig_Markup |
64
|
|
|
* @throws \yii\web\NotFoundHttpException |
65
|
|
|
*/ |
66
|
|
|
public function includeCssModule(string $moduleName, bool $async = false, $config = null): \Twig_Markup |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return Template::raw( |
69
|
|
|
ManifestHelper::getCssModuleTags(self::$config, $moduleName, $async) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the CSS file in $path wrapped in <style></style> tags |
75
|
|
|
* |
76
|
|
|
* @param string $path |
|
|
|
|
77
|
|
|
* |
78
|
|
|
* @return \Twig_Markup |
79
|
|
|
*/ |
80
|
|
|
public function includeInlineCssTags(string $path): \Twig_Markup |
81
|
|
|
{ |
82
|
|
|
return Template::raw( |
83
|
|
|
ManifestHelper::getCssInlineTags($path) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the uglified loadCSS rel=preload Polyfill as per: |
89
|
|
|
* https://github.com/filamentgroup/loadCSS#how-to-use-loadcss-recommended-example |
90
|
|
|
* |
91
|
|
|
* @return \Twig_Markup |
92
|
|
|
*/ |
93
|
|
|
public static function includeCssRelPreloadPolyfill(): \Twig_Markup |
94
|
|
|
{ |
95
|
|
|
return Template::raw( |
96
|
|
|
ManifestHelper::getCssRelPreloadPolyfill() |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
|
|
|
|
101
|
|
|
* @param string $moduleName |
|
|
|
|
102
|
|
|
* @param bool $async |
|
|
|
|
103
|
|
|
* @param null|array $config |
|
|
|
|
104
|
|
|
* |
105
|
|
|
* @return null|\Twig_Markup |
106
|
|
|
* @throws \yii\web\NotFoundHttpException |
107
|
|
|
*/ |
108
|
|
|
public function includeJsModule(string $moduleName, bool $async = false, $config = null) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
return Template::raw( |
111
|
|
|
ManifestHelper::getJsModuleTags(self::$config, $moduleName, $async) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return the URI to a module |
117
|
|
|
* |
118
|
|
|
* @param string $moduleName |
|
|
|
|
119
|
|
|
* @param string $type |
|
|
|
|
120
|
|
|
* @param null $config |
|
|
|
|
121
|
|
|
* |
122
|
|
|
* @return null|\Twig_Markup |
123
|
|
|
* @throws \yii\web\NotFoundHttpException |
124
|
|
|
*/ |
125
|
|
|
public function getModuleUri(string $moduleName, string $type = 'modern', $config = null) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
return Template::raw( |
128
|
|
|
ManifestHelper::getModule(self::$config, $moduleName, $type) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Include the Safari 10.1 nomodule fix JavaScript |
134
|
|
|
* |
135
|
|
|
* @return \Twig_Markup |
136
|
|
|
*/ |
137
|
|
|
public function includeSafariNomoduleFix(): \Twig_Markup |
138
|
|
|
{ |
139
|
|
|
return Template::raw( |
140
|
|
|
ManifestHelper::getSafariNomoduleFix() |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns the contents of a file from a URI path |
146
|
|
|
* |
147
|
|
|
* @param string $path |
|
|
|
|
148
|
|
|
* |
149
|
|
|
* @return \Twig_Markup |
150
|
|
|
*/ |
151
|
|
|
public function includeFile(string $path): \Twig_Markup |
152
|
|
|
{ |
153
|
|
|
return Template::raw( |
154
|
|
|
ManifestHelper::getFile($path) |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns the contents of a file from the $fileName in the manifest |
160
|
|
|
* |
161
|
|
|
* @param string $fileName |
|
|
|
|
162
|
|
|
* @param string $type |
|
|
|
|
163
|
|
|
* @param null $config |
|
|
|
|
164
|
|
|
* |
165
|
|
|
* @return \Twig_Markup |
166
|
|
|
*/ |
167
|
|
|
public function includeFileFromManifest(string $fileName, string $type = 'legacy', $config = null): \Twig_Markup |
168
|
|
|
{ |
169
|
|
|
return Template::raw( |
170
|
|
|
ManifestHelper::getFileFromManifest($config, $fileName, $type) |
|
|
|
|
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|