From a35faa558cda5599f40e214abbcc65f86e3af8f1 Mon Sep 17 00:00:00 2001 From: dimti Date: Sat, 19 Mar 2022 19:01:54 +0300 Subject: [PATCH] + hmr assets from webpack encore dev-server supports --- Plugin.php | 1 + classes/ManifestReader.php | 16 ++++++++++++---- classes/TwigFilters.php | 21 +++++++++++++++++++++ components/Manifest.php | 4 +++- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Plugin.php b/Plugin.php index 22cec54..b28b534 100644 --- a/Plugin.php +++ b/Plugin.php @@ -102,6 +102,7 @@ class Plugin extends PluginBase ], 'functions' => [ 'manifest' => [TwigFilters::class, 'manifest'], + 'hmrAssets' => [TwigFilters::class, 'hmrAssets'], ], ]; } diff --git a/classes/ManifestReader.php b/classes/ManifestReader.php index 96c9ddc..4250802 100644 --- a/classes/ManifestReader.php +++ b/classes/ManifestReader.php @@ -1,8 +1,11 @@ manifest = json_decode(file_get_contents($manifestPath)); + $this->manifest = collect(json_decode(file_get_contents($manifestPath))); } public function get(string $assetName) { - return $this->manifest->$assetName; + return $this->manifest->offsetGet($assetName); + } + + public function toArray() + { + return $this->manifest; } } diff --git a/classes/TwigFilters.php b/classes/TwigFilters.php index acd8f72..8b6c369 100644 --- a/classes/TwigFilters.php +++ b/classes/TwigFilters.php @@ -12,4 +12,25 @@ class TwigFilters return $manifest->getManifestReader()->get($assetName); } + + public static function hmrAssets() + { + if (\Config::get('app.debug')) { + $manifest = app()->get(Manifest::class); + + assert($manifest instanceof Manifest); + + $startsWithSubstring = 'vendors-node_modules'; + + foreach ($manifest->getManifestReader()->toArray() as $assetName => $assetsFullPath) { + if (starts_with($assetName, $startsWithSubstring)) { + if (ends_with($assetName, '.css')) { + echo '' . PHP_EOL; + } else { + echo '' . PHP_EOL; + } + } + } + } + } } diff --git a/components/Manifest.php b/components/Manifest.php index 16b8ced..e7df347 100644 --- a/components/Manifest.php +++ b/components/Manifest.php @@ -1,6 +1,7 @@ '^[^/].*/manifest.json', 'validationMessage' => self::LANG_PREFIX . 'properties.path.validationMessage', 'placeholder' => 'assets/build/manifest.json', + 'default' => 'assets/build/manifest.json', 'showExternalParam' => false, 'required' => true, ] @@ -49,7 +51,7 @@ class Manifest extends ComponentBase */ private function prepareVars(): void { - $this->manifestReader = new ManifestReader(base_path($this->property('path'))); + $this->manifestReader = new ManifestReader(Theme::getActiveTheme()->getPath() . '/' . $this->property('path')); } public function getManifestReader(): ManifestReader