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\transcoder\helpers; |
13
|
|
|
|
14
|
|
|
use nystudio107\transcoder\assetbundles\transcoder\TranscoderAsset; |
15
|
|
|
|
16
|
|
|
use Craft; |
17
|
|
|
use craft\helpers\Json as JsonHelper; |
18
|
|
|
use craft\helpers\UrlHelper; |
19
|
|
|
|
20
|
|
|
use yii\base\Exception; |
21
|
|
|
use yii\base\InvalidConfigException; |
22
|
|
|
use yii\caching\TagDependency; |
23
|
|
|
use yii\web\NotFoundHttpException; |
24
|
|
|
|
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @author nystudio107 |
|
|
|
|
27
|
|
|
* @package Twigpack |
|
|
|
|
28
|
|
|
* @since 1.0.0 |
|
|
|
|
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
class Manifest |
31
|
|
|
{ |
32
|
|
|
// Constants |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
const ASSET_CLASS = TranscoderAsset::class; |
36
|
|
|
|
37
|
|
|
const CACHE_KEY = 'twigpack-' . self::ASSET_CLASS; |
38
|
|
|
const CACHE_TAG = 'twigpack-' . self::ASSET_CLASS; |
39
|
|
|
|
40
|
|
|
const DEVMODE_CACHE_DURATION = 1; |
41
|
|
|
|
42
|
|
|
const SUPPRESS_ERRORS_FOR_MODULES = [ |
43
|
|
|
'styles.js', |
44
|
|
|
'commons.js', |
45
|
|
|
'vendors.js', |
46
|
|
|
'vendors.css', |
47
|
|
|
'styles.css', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
// Protected Static Properties |
51
|
|
|
// ========================================================================= |
52
|
|
|
|
53
|
|
|
protected static $config = [ |
54
|
|
|
// If `devMode` is on, use webpack-dev-server to all for HMR (hot module reloading) |
55
|
|
|
'useDevServer' => false, |
56
|
|
|
// Manifest names |
57
|
|
|
'manifest' => [ |
58
|
|
|
'legacy' => 'manifest.json', |
59
|
|
|
'modern' => 'manifest.json', |
60
|
|
|
], |
61
|
|
|
// Public server config |
62
|
|
|
'server' => [ |
63
|
|
|
'manifestPath' => '/', |
64
|
|
|
'publicPath' => '/', |
65
|
|
|
], |
66
|
|
|
// webpack-dev-server config |
67
|
|
|
'devServer' => [ |
68
|
|
|
'manifestPath' => 'http://127.0.0.1:8080', |
69
|
|
|
'publicPath' => '/', |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/** |
|
|
|
|
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected static $files; |
77
|
|
|
|
78
|
|
|
/** |
|
|
|
|
79
|
|
|
* @var bool |
80
|
|
|
*/ |
81
|
|
|
protected static $isHot = false; |
82
|
|
|
|
83
|
|
|
// Public Static Methods |
84
|
|
|
// ========================================================================= |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Simulate a static constructor |
88
|
|
|
* |
89
|
|
|
* ManifestVariable constructor. |
90
|
|
|
* @noinspection MagicMethodsValidityInspection |
|
|
|
|
91
|
|
|
*/ |
|
|
|
|
92
|
|
|
public static function __constructStatic() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
self::invalidateCaches(); |
95
|
|
|
$assetClass = self::ASSET_CLASS; |
96
|
|
|
$bundle = new $assetClass; |
97
|
|
|
$baseAssetsUrl = Craft::$app->assetManager->getPublishedUrl( |
98
|
|
|
$bundle->sourcePath, |
99
|
|
|
true |
|
|
|
|
100
|
|
|
); |
101
|
|
|
self::$config['server']['manifestPath'] = Craft::getAlias($bundle->sourcePath); |
102
|
|
|
self::$config['server']['publicPath'] = $baseAssetsUrl; |
103
|
|
|
$useDevServer = getenv('NYS_PLUGIN_DEVSERVER'); |
104
|
|
|
if ($useDevServer !== false) { |
105
|
|
|
self::$config['useDevServer'] = (bool)$useDevServer; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the passed in JS modules from the manifest, and register them in the current Craft view |
111
|
|
|
* |
112
|
|
|
* @param array $modules |
|
|
|
|
113
|
|
|
* @throws NotFoundHttpException |
|
|
|
|
114
|
|
|
* @throws InvalidConfigException |
|
|
|
|
115
|
|
|
*/ |
|
|
|
|
116
|
|
|
public static function registerJsModules(array $modules) |
117
|
|
|
{ |
118
|
|
|
$view = Craft::$app->getView(); |
119
|
|
|
foreach($modules as $module) { |
|
|
|
|
120
|
|
|
$jsModule = self::getModule(self::$config, $module, 'modern'); |
121
|
|
|
if ($jsModule) { |
122
|
|
|
$view->registerJsFile($jsModule, [ |
|
|
|
|
123
|
|
|
'depends' => self::ASSET_CLASS |
124
|
|
|
]); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the passed in CS modules from the manifest, and register them in the current Craft view |
131
|
|
|
* |
132
|
|
|
* @param array $modules |
|
|
|
|
133
|
|
|
* @throws NotFoundHttpException |
|
|
|
|
134
|
|
|
* @throws InvalidConfigException |
|
|
|
|
135
|
|
|
*/ |
|
|
|
|
136
|
|
|
public static function registerCssModules(array $modules) |
137
|
|
|
{ |
138
|
|
|
$view = Craft::$app->getView(); |
139
|
|
|
foreach($modules as $module) { |
|
|
|
|
140
|
|
|
$cssModule = self::getModule(self::$config, $module, 'legacy'); |
141
|
|
|
if ($cssModule) { |
142
|
|
|
$view->registerCssFile($cssModule, [ |
|
|
|
|
143
|
|
|
'depends' => self::ASSET_CLASS |
144
|
|
|
]); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the passed in JS module from the manifest, then output a `<script src="">` tag for it in the HTML |
151
|
|
|
* |
152
|
|
|
* @param string $moduleName |
|
|
|
|
153
|
|
|
* @param bool $async |
|
|
|
|
154
|
|
|
* |
155
|
|
|
* @return null|string |
156
|
|
|
* @throws NotFoundHttpException |
157
|
|
|
*/ |
158
|
|
|
public static function includeJsModule(string $moduleName, bool $async) |
159
|
|
|
{ |
160
|
|
|
$legacyModule = self::getModule(self::$config, $moduleName, 'legacy'); |
161
|
|
|
if ($legacyModule === null) { |
162
|
|
|
return ''; |
163
|
|
|
} |
164
|
|
|
if ($async) { |
165
|
|
|
$modernModule = self::getModule(self::$config, $moduleName, 'modern'); |
166
|
|
|
if ($modernModule === null) { |
167
|
|
|
return ''; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
$lines = []; |
171
|
|
|
if ($async) { |
172
|
|
|
$lines[] = "<script type=\"module\" src=\"{$modernModule}\"></script>"; |
|
|
|
|
173
|
|
|
$lines[] = "<script nomodule src=\"{$legacyModule}\"></script>"; |
174
|
|
|
} else { |
175
|
|
|
$lines[] = "<script src=\"{$legacyModule}\"></script>"; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return implode("\r\n", $lines); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the passed in CS module from the manifest, then output a `<link>` tag for it in the HTML |
183
|
|
|
* |
184
|
|
|
* @param string $moduleName |
|
|
|
|
185
|
|
|
* @param bool $async |
|
|
|
|
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
* @throws NotFoundHttpException |
189
|
|
|
*/ |
190
|
|
|
public static function includeCssModule(string $moduleName, bool $async): string |
191
|
|
|
{ |
192
|
|
|
$legacyModule = self::getModule(self::$config, $moduleName, 'legacy', true); |
193
|
|
|
if ($legacyModule === null) { |
194
|
|
|
return ''; |
195
|
|
|
} |
196
|
|
|
$lines = []; |
197
|
|
|
if ($async) { |
198
|
|
|
$lines[] = "<link rel=\"preload\" href=\"{$legacyModule}\" as=\"style\" onload=\"this.onload=null;this.rel='stylesheet'\" />"; |
199
|
|
|
$lines[] = "<noscript><link rel=\"stylesheet\" href=\"{$legacyModule}\"></noscript>"; |
200
|
|
|
} else { |
201
|
|
|
$lines[] = "<link rel=\"stylesheet\" href=\"{$legacyModule}\" />"; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return implode("\r\n", $lines); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// Protected Static Methods |
208
|
|
|
// ========================================================================= |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Return the URI to a module |
212
|
|
|
* |
213
|
|
|
* @param array $config |
|
|
|
|
214
|
|
|
* @param string $moduleName |
|
|
|
|
215
|
|
|
* @param string $type |
|
|
|
|
216
|
|
|
* @param bool $soft |
|
|
|
|
217
|
|
|
* |
218
|
|
|
* @return null|string |
219
|
|
|
* @throws NotFoundHttpException |
220
|
|
|
*/ |
221
|
|
|
protected static function getModule(array $config, string $moduleName, string $type = 'modern', bool $soft = false) |
222
|
|
|
{ |
223
|
|
|
// Get the module entry |
224
|
|
|
$module = self::getModuleEntry($config, $moduleName, $type, $soft); |
225
|
|
|
if ($module !== null) { |
226
|
|
|
$prefix = self::$isHot |
227
|
|
|
? $config['devServer']['publicPath'] |
228
|
|
|
: $config['server']['publicPath']; |
229
|
|
|
// If the module isn't a full URL, prefix it |
230
|
|
|
if (!UrlHelper::isAbsoluteUrl($module)) { |
231
|
|
|
$module = self::combinePaths($prefix, $module); |
232
|
|
|
} |
233
|
|
|
// Resolve any aliases |
234
|
|
|
$alias = Craft::getAlias($module, false); |
235
|
|
|
if ($alias) { |
236
|
|
|
$module = $alias; |
237
|
|
|
} |
238
|
|
|
// Make sure it's a full URL |
239
|
|
|
if (!UrlHelper::isAbsoluteUrl($module) && !is_file($module)) { |
|
|
|
|
240
|
|
|
try { |
241
|
|
|
$module = UrlHelper::siteUrl($module); |
|
|
|
|
242
|
|
|
} catch (Exception $e) { |
243
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $module; |
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Return a module's raw entry from the manifest |
253
|
|
|
* |
254
|
|
|
* @param array $config |
|
|
|
|
255
|
|
|
* @param string $moduleName |
|
|
|
|
256
|
|
|
* @param string $type |
|
|
|
|
257
|
|
|
* @param bool $soft |
|
|
|
|
258
|
|
|
* |
259
|
|
|
* @return null|string |
260
|
|
|
* @throws NotFoundHttpException |
261
|
|
|
*/ |
262
|
|
|
protected static function getModuleEntry(array $config, string $moduleName, string $type = 'modern', bool $soft = false) |
263
|
|
|
{ |
264
|
|
|
$module = null; |
265
|
|
|
// Get the manifest file |
266
|
|
|
$manifest = self::getManifestFile($config, $type); |
267
|
|
|
if ($manifest !== null) { |
268
|
|
|
// Make sure it exists in the manifest |
269
|
|
|
if (empty($manifest[$moduleName])) { |
270
|
|
|
// Don't report errors for any files in SUPPRESS_ERRORS_FOR_MODULES |
271
|
|
|
if (!in_array($moduleName, self::SUPPRESS_ERRORS_FOR_MODULES)) { |
272
|
|
|
self::reportError(Craft::t( |
|
|
|
|
273
|
|
|
'transcoder', |
274
|
|
|
'Module does not exist in the manifest: {moduleName}', |
275
|
|
|
['moduleName' => $moduleName] |
276
|
|
|
), $soft); |
|
|
|
|
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return null; |
280
|
|
|
} |
281
|
|
|
$module = $manifest[$moduleName]; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $module; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Return a JSON-decoded manifest file |
289
|
|
|
* |
290
|
|
|
* @param array $config |
|
|
|
|
291
|
|
|
* @param string $type |
|
|
|
|
292
|
|
|
* |
293
|
|
|
* @return null|array |
294
|
|
|
* @throws NotFoundHttpException |
295
|
|
|
*/ |
296
|
|
|
protected static function getManifestFile(array $config, string $type = 'modern') |
297
|
|
|
{ |
298
|
|
|
$manifest = null; |
299
|
|
|
// Determine whether we should use the devServer for HMR or not |
300
|
|
|
$devMode = Craft::$app->getConfig()->getGeneral()->devMode; |
301
|
|
|
self::$isHot = ($devMode && $config['useDevServer']); |
302
|
|
|
// Try to get the manifest |
303
|
|
|
while ($manifest === null) { |
304
|
|
|
$manifestPath = self::$isHot |
305
|
|
|
? $config['devServer']['manifestPath'] |
306
|
|
|
: $config['server']['manifestPath']; |
307
|
|
|
// Normalize the path |
308
|
|
|
$path = self::combinePaths($manifestPath, $config['manifest'][$type]); |
309
|
|
|
$manifest = self::getJsonFile($path); |
310
|
|
|
// If the manifest isn't found, and it was hot, fall back on non-hot |
311
|
|
|
if ($manifest === null) { |
312
|
|
|
// We couldn't find a manifest; throw an error |
313
|
|
|
self::reportError(Craft::t( |
|
|
|
|
314
|
|
|
'transcoder', |
315
|
|
|
'Manifest file not found at: {manifestPath}', |
316
|
|
|
['manifestPath' => $manifestPath] |
317
|
|
|
), true); |
|
|
|
|
318
|
|
|
if (self::$isHot) { |
319
|
|
|
// Try again, but not with home module replacement |
320
|
|
|
self::$isHot = false; |
321
|
|
|
} else { |
322
|
|
|
// Give up and return null |
323
|
|
|
return null; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return $manifest; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Return the contents of a JSON file from a URI path |
333
|
|
|
* |
334
|
|
|
* @param string $path |
|
|
|
|
335
|
|
|
* |
336
|
|
|
* @return null|array |
337
|
|
|
*/ |
338
|
|
|
protected static function getJsonFile(string $path) |
339
|
|
|
{ |
340
|
|
|
return self::getFileFromUri($path, [JsonHelper::class, 'decodeIfJson']); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Invalidate all of the manifest caches |
345
|
|
|
*/ |
|
|
|
|
346
|
|
|
public static function invalidateCaches() |
347
|
|
|
{ |
348
|
|
|
$cache = Craft::$app->getCache(); |
349
|
|
|
TagDependency::invalidate($cache, self::CACHE_TAG); |
350
|
|
|
Craft::info('All manifest caches cleared', __METHOD__); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Return the contents of a file from a URI path |
355
|
|
|
* |
356
|
|
|
* @param string $path |
|
|
|
|
357
|
|
|
* @param callable|null $callback |
|
|
|
|
358
|
|
|
* |
359
|
|
|
* @return null|mixed |
360
|
|
|
*/ |
361
|
|
|
protected static function getFileFromUri(string $path, callable $callback = null) |
362
|
|
|
{ |
363
|
|
|
// Resolve any aliases |
364
|
|
|
$alias = Craft::getAlias($path, false); |
365
|
|
|
if ($alias) { |
366
|
|
|
$path = (string)$alias; |
367
|
|
|
} |
368
|
|
|
// Make sure it's a full URL |
369
|
|
|
if (!UrlHelper::isAbsoluteUrl($path) && !is_file($path)) { |
370
|
|
|
try { |
371
|
|
|
$path = UrlHelper::siteUrl($path); |
372
|
|
|
} catch (Exception $e) { |
373
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return self::getFileContents($path, $callback); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Return the contents of a file from the passed in path |
382
|
|
|
* |
383
|
|
|
* @param string $path |
|
|
|
|
384
|
|
|
* @param callable $callback |
|
|
|
|
385
|
|
|
* |
386
|
|
|
* @return null|mixed |
387
|
|
|
*/ |
388
|
|
|
protected static function getFileContents(string $path, callable $callback = null) |
389
|
|
|
{ |
390
|
|
|
// Return the memoized manifest if it exists |
391
|
|
|
if (!empty(self::$files[$path])) { |
392
|
|
|
return self::$files[$path]; |
393
|
|
|
} |
394
|
|
|
// Create the dependency tags |
395
|
|
|
$dependency = new TagDependency([ |
|
|
|
|
396
|
|
|
'tags' => [ |
397
|
|
|
self::CACHE_TAG, |
398
|
|
|
self::CACHE_TAG.$path, |
399
|
|
|
], |
400
|
|
|
]); |
|
|
|
|
401
|
|
|
// Set the cache duration based on devMode |
402
|
|
|
$cacheDuration = Craft::$app->getConfig()->getGeneral()->devMode |
403
|
|
|
? self::DEVMODE_CACHE_DURATION |
404
|
|
|
: null; |
405
|
|
|
// Get the result from the cache, or parse the file |
406
|
|
|
$cache = Craft::$app->getCache(); |
407
|
|
|
$file = $cache->getOrSet( |
408
|
|
|
self::CACHE_KEY.$path, |
409
|
|
|
function () use ($path, $callback) { |
410
|
|
|
$result = null; |
411
|
|
|
$contents = @file_get_contents($path); |
412
|
|
|
if ($contents) { |
413
|
|
|
$result = $contents; |
414
|
|
|
if ($callback) { |
415
|
|
|
$result = $callback($result); |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
return $result; |
420
|
|
|
}, |
421
|
|
|
$cacheDuration, |
422
|
|
|
$dependency |
423
|
|
|
); |
424
|
|
|
self::$files[$path] = $file; |
425
|
|
|
|
426
|
|
|
return $file; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Combined the passed in paths, whether file system or URL |
431
|
|
|
* |
432
|
|
|
* @param string ...$paths |
|
|
|
|
433
|
|
|
* |
434
|
|
|
* @return string |
435
|
|
|
*/ |
436
|
|
|
protected static function combinePaths(string ...$paths): string |
437
|
|
|
{ |
438
|
|
|
$last_key = count($paths) - 1; |
439
|
|
|
array_walk($paths, function (&$val, $key) use ($last_key) { |
|
|
|
|
440
|
|
|
switch ($key) { |
441
|
|
|
case 0: |
|
|
|
|
442
|
|
|
$val = rtrim($val, '/ '); |
443
|
|
|
break; |
444
|
|
|
case $last_key: |
|
|
|
|
445
|
|
|
$val = ltrim($val, '/ '); |
446
|
|
|
break; |
447
|
|
|
default: |
|
|
|
|
448
|
|
|
$val = trim($val, '/ '); |
449
|
|
|
break; |
450
|
|
|
} |
451
|
|
|
}); |
|
|
|
|
452
|
|
|
|
453
|
|
|
$first = array_shift($paths); |
454
|
|
|
$last = array_pop($paths); |
455
|
|
|
$paths = array_filter($paths); |
456
|
|
|
array_unshift($paths, $first); |
457
|
|
|
$paths[] = $last; |
458
|
|
|
|
459
|
|
|
return implode('/', $paths); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
|
|
|
|
463
|
|
|
* @param string $error |
|
|
|
|
464
|
|
|
* @param bool $soft |
|
|
|
|
465
|
|
|
* |
466
|
|
|
* @throws NotFoundHttpException |
467
|
|
|
*/ |
|
|
|
|
468
|
|
|
protected static function reportError(string $error, $soft = false) |
469
|
|
|
{ |
470
|
|
|
$devMode = Craft::$app->getConfig()->getGeneral()->devMode; |
471
|
|
|
if ($devMode && !$soft) { |
472
|
|
|
throw new NotFoundHttpException($error); |
473
|
|
|
} |
474
|
|
|
Craft::error($error, __METHOD__); |
475
|
|
|
} |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
// Simulate a static constructor |
479
|
|
|
Manifest::__constructStatic(); |
480
|
|
|
|