+ vitejs support

This commit is contained in:
2022-07-17 14:34:49 +03:00
parent a35faa558c
commit 0c7f82ab7e
11 changed files with 402 additions and 78 deletions

View File

@ -1,28 +1,43 @@
<?php namespace Wpstudio\AssetsManifest\Classes;
use Wpstudio\Assetsmanifest\Components\Manifest;
use Wpstudio\AssetsManifest\Classes\Bundlers\Bundler;
use Wpstudio\AssetsManifest\Classes\Bundlers\ViteBundler;
use Wpstudio\AssetsManifest\Classes\Bundlers\WebpackEncoreBundler;
class TwigFilters
{
public static function manifest($assetName): string
/**
* @param string $assetName
* @return string
* @throws AssetsManifestException
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public static function encoreAsset(string $assetName): string
{
$manifest = app()->get(Manifest::class);
$bundler = app()->get(Bundler::class);
assert($bundler instanceof Bundler);
$bundler->validateBundlerType(Bundler::BUNDLER_WEBPACK_ENCORE);
assert($bundler instanceof WebpackEncoreBundler);
assert($manifest instanceof Manifest);
return $manifest->getManifestReader()->get($assetName);
return $bundler->getEntrypoint($assetName);
}
public static function hmrAssets()
{
if (\Config::get('app.debug')) {
$manifest = app()->get(Manifest::class);
assert($manifest instanceof Manifest);
$bundler = app()->get(Bundler::class);
assert($bundler instanceof Bundler);
$bundler->validateBundlerType(Bundler::BUNDLER_WEBPACK_ENCORE);
assert($bundler instanceof WebpackEncoreBundler);
$manifestReader = app()->get(ManifestReader::class);
assert($bundler instanceof ManifestReader);
$startsWithSubstring = 'vendors-node_modules';
foreach ($manifest->getManifestReader()->toArray() as $assetName => $assetsFullPath) {
foreach ($manifestReader->toArray() as $assetName => $assetsFullPath) {
if (starts_with($assetName, $startsWithSubstring)) {
if (ends_with($assetName, '.css')) {
echo '<link rel="stylesheet" href="' . $assetsFullPath . '">' . PHP_EOL;
@ -33,4 +48,48 @@ class TwigFilters
}
}
}
/**
* @param string $entrypointName
* @return string
* @throws AssetsManifestException
*/
public static function viteEntrypointStyles(string $entrypointName): string
{
$bundler = app()->get(Bundler::class);
assert($bundler instanceof Bundler);
$bundler->validateBundlerType(Bundler::BUNDLER_VITE);
assert($bundler instanceof ViteBundler);
return $bundler->getEntrypointStylesheets($entrypointName);
}
/**
* @param string $entrypointName
* @return string
* @throws AssetsManifestException
*/
public static function viteEntrypointAssets(string $entrypointName): string
{
$bundler = app()->get(Bundler::class);
assert($bundler instanceof Bundler);
$bundler->validateBundlerType(Bundler::BUNDLER_VITE);
assert($bundler instanceof ViteBundler);
return $bundler->getEntrypointAssets($entrypointName);
}
public static function viteDevClientScriptTag()
{
$bundler = app()->get(Bundler::class);
assert($bundler instanceof Bundler);
$bundler->validateBundlerType(Bundler::BUNDLER_VITE);
assert($bundler instanceof ViteBundler);
if ($bundler->isViteDevEnabled()) {
return $bundler->getViteDevClientScriptTag();
}
return '';
}
}