+ hmr assets from webpack encore dev-server supports
This commit is contained in:
@ -102,6 +102,7 @@ class Plugin extends PluginBase
|
|||||||
],
|
],
|
||||||
'functions' => [
|
'functions' => [
|
||||||
'manifest' => [TwigFilters::class, 'manifest'],
|
'manifest' => [TwigFilters::class, 'manifest'],
|
||||||
|
'hmrAssets' => [TwigFilters::class, 'hmrAssets'],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
<?php namespace Wpstudio\AssetsManifest\Classes;
|
<?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
|
* @param string $manifestPath
|
||||||
@ -18,11 +21,16 @@ class ManifestReader
|
|||||||
throw new AssetsManifestException('Not readable: ' . $manifestPath);
|
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)
|
public function get(string $assetName)
|
||||||
{
|
{
|
||||||
return $this->manifest->$assetName;
|
return $this->manifest->offsetGet($assetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toArray()
|
||||||
|
{
|
||||||
|
return $this->manifest;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,4 +12,25 @@ class TwigFilters
|
|||||||
|
|
||||||
return $manifest->getManifestReader()->get($assetName);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php namespace Wpstudio\Assetsmanifest\Components;
|
<?php namespace Wpstudio\Assetsmanifest\Components;
|
||||||
|
|
||||||
use Cms\Classes\ComponentBase;
|
use Cms\Classes\ComponentBase;
|
||||||
|
use Cms\Classes\Theme;
|
||||||
use Wpstudio\AssetsManifest\Classes\ManifestReader;
|
use Wpstudio\AssetsManifest\Classes\ManifestReader;
|
||||||
|
|
||||||
class Manifest extends ComponentBase
|
class Manifest extends ComponentBase
|
||||||
@ -26,6 +27,7 @@ class Manifest extends ComponentBase
|
|||||||
'validationPattern' => '^[^/].*/manifest.json',
|
'validationPattern' => '^[^/].*/manifest.json',
|
||||||
'validationMessage' => self::LANG_PREFIX . 'properties.path.validationMessage',
|
'validationMessage' => self::LANG_PREFIX . 'properties.path.validationMessage',
|
||||||
'placeholder' => 'assets/build/manifest.json',
|
'placeholder' => 'assets/build/manifest.json',
|
||||||
|
'default' => 'assets/build/manifest.json',
|
||||||
'showExternalParam' => false,
|
'showExternalParam' => false,
|
||||||
'required' => true,
|
'required' => true,
|
||||||
]
|
]
|
||||||
@ -49,7 +51,7 @@ class Manifest extends ComponentBase
|
|||||||
*/
|
*/
|
||||||
private function prepareVars(): void
|
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
|
public function getManifestReader(): ManifestReader
|
||||||
|
Reference in New Issue
Block a user