Browse Source

+ hmr assets from webpack encore dev-server supports

tags/1.0.0 1.0.0
dimti 2 years ago
parent
commit
a35faa558c
  1. 1
      Plugin.php
  2. 16
      classes/ManifestReader.php
  3. 21
      classes/TwigFilters.php
  4. 4
      components/Manifest.php

1
Plugin.php

@ -102,6 +102,7 @@ class Plugin extends PluginBase
],
'functions' => [
'manifest' => [TwigFilters::class, 'manifest'],
'hmrAssets' => [TwigFilters::class, 'hmrAssets'],
],
];
}

16
classes/ManifestReader.php

@ -1,8 +1,11 @@
<?php namespace Wpstudio\AssetsManifest\Classes;
class ManifestReader
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
class ManifestReader implements Arrayable
{
private $manifest = null;
private Collection $manifest;
/**
* @param string $manifestPath
@ -18,11 +21,16 @@ class ManifestReader
throw new AssetsManifestException('Not readable: ' . $manifestPath);
}
$this->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;
}
}

21
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 '<link rel="stylesheet" href="' . $assetsFullPath . '">' . PHP_EOL;
} else {
echo '<script src="' . $assetsFullPath . '"></script>' . PHP_EOL;
}
}
}
}
}
}

4
components/Manifest.php

@ -1,6 +1,7 @@
<?php namespace Wpstudio\Assetsmanifest\Components;
use Cms\Classes\ComponentBase;
use Cms\Classes\Theme;
use Wpstudio\AssetsManifest\Classes\ManifestReader;
class Manifest extends ComponentBase
@ -26,6 +27,7 @@ class Manifest extends ComponentBase
'validationPattern' => '^[^/].*/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

Loading…
Cancel
Save