Created
September 16, 2022 07:06
-
-
Save Semdevmaster/a8caa74e8aacf6063877d06eeb86d576 to your computer and use it in GitHub Desktop.
ZoomX plugin for assets manifest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Plugins; | |
class AssetsManifest extends \Zoomx\Elements\Plugin | |
{ | |
public static $events = [ | |
'OnZoomxInit' => 0, | |
]; | |
public $disabled = false; | |
public function OnZoomxInit(): void | |
{ | |
$assets_version = $this->modx->getOption('assets_version', null, 'v1', true); | |
$manifest_path = "assets/" . $assets_version . "/assets.json"; | |
$development = $this->modx->getOption('development', null, false, true); | |
$development_path = $this->modx->getOption('server_protocol') | |
. '://' | |
. $this->modx->getOption('http_host') | |
. ':' | |
. (getenv('DEVELOPMENT_PORT', true) ?: '3000') | |
. '/'; | |
if (file_exists($manifest_path)) { | |
try { | |
$manifest_array = json_decode( | |
file_get_contents($manifest_path), | |
true, | |
512, | |
JSON_THROW_ON_ERROR | |
); | |
} catch (\JsonException $e) { | |
$this->modx->log(1, 'Unable to parse manifest with assets'); | |
$this->modx->log(1, $e); | |
} | |
if (isset($manifest_array)) { | |
$this->modx->setPlaceholder('manifest', $manifest_array); | |
} | |
} | |
$this->modx->setPlaceholder('development_path', $development_path); | |
if ($development) { | |
$this->modx->regClientHTMLBlock( | |
'<script type="module" src="' . $development_path . '@vite/client' . '"></script>' | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment