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