s2b/vite-asset-collector

View on GitHub
Classes/ViewHelpers/Asset/ViteViewHelper.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace Praetorius\ViteAssetCollector\ViewHelpers\Asset;

use Praetorius\ViteAssetCollector\Exception\ViteException;
use Praetorius\ViteAssetCollector\Service\ViteService;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
 * This ViewHelper adds frontend assets generated by vite to
 * TYPO3's asset collector
 */
final class ViteViewHelper extends AbstractViewHelper
{
    protected ViteService $viteService;

    /**
     * @var RenderingContext
     */
    protected $renderingContext;

    public function initializeArguments(): void
    {
        $this->registerArgument(
            'manifest',
            'string',
            'Path to vite manifest file; if omitted, default manifest from extension configuration will be used instead.'
        );
        $this->registerArgument(
            'entry',
            'string',
            'Name of entrypoint that should be included; can be omitted if manifest file exists and only contains one entrypoint'
        );
        $this->registerArgument('devTagAttributes', 'array', 'Additional attributes for dev server script tags.', false, []);
        $this->registerArgument('scriptTagAttributes', 'array', 'Additional attributes for script tags.', false, []);
        $this->registerArgument('addCss', 'boolean', 'Define whether css assets should be included.', false, true);
        $this->registerArgument('cssTagAttributes', 'array', 'Additional attributes for css link tags.', false, []);
        $this->registerArgument(
            'priority',
            'boolean',
            'Define whether the assets should be included before other assets.',
            false,
            false
        );
        $this->registerArgument('useNonce', 'bool', 'Whether to use the global nonce value', false, false);
    }

    public function render(): string
    {
        $assetOptions = [
            'priority' => $this->arguments['priority'],
            'useNonce' => $this->arguments['useNonce'],
        ];

        $manifest = $this->getManifest();

        $entry = $this->arguments['entry'];
        $entry ??= $this->viteService->determineEntrypointFromManifest($manifest);

        if ($this->viteService->useDevServer()) {
            $this->viteService->addAssetsFromDevServer(
                $this->viteService->determineDevServer($this->renderingContext->getRequest()),
                $entry,
                $assetOptions,
                $this->arguments['devTagAttributes']
            );
        } else {
            $this->viteService->addAssetsFromManifest(
                $manifest,
                $entry,
                $this->arguments['addCss'],
                $assetOptions,
                $this->arguments['scriptTagAttributes'],
                $this->arguments['cssTagAttributes']
            );
        }
        return '';
    }

    private function getManifest(): string
    {
        $manifest = $this->arguments['manifest'] ?? $this->viteService->getDefaultManifestFile();

        if (!is_string($manifest) || $manifest === '') {
            throw new ViteException(
                sprintf(
                    'Unable to determine vite manifest from specified argument and default manifest: %s',
                    $manifest
                ),
                1684528724
            );
        }

        return $manifest;
    }

    public function injectViteService(ViteService $viteService): void
    {
        $this->viteService = $viteService;
    }
}