Vite front end dev environment integration

v0.2.13 2024-04-22 14:09 UTC

README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

Vite front end dev environment integration

Zest provides a simplified and opinionated PHP oriented entry point to the Vite development environment.

Get news and updates on the DecodeLabs blog.

Installation

Install via Composer:

composer require decodelabs/zest

Usage

Zest aims to provide a simple automated way to integrate the Vite dev server into your PHP application.

All terminal commands assume you have Effigy installed and working.

cd my-project
effigy zest init

This command will initialise a Vite config file, install everything you initially need in a package.json file and run the dev server. ctrl+c to quit the server.

From then on:

# Run the dev server
effigy zest dev

# Or build the static files
effigy zest build

Build will trigger automatically when the dev server is closed.

View consumption

To make use of Zest, you will need to consume the generated assets from the manifest in your views. As it stands, there are no pre-built view adapters (there are many different view libraries out there!!), however you can adapt the one you use like this:

use DecodeLabs\Genesis;
use DecodeLabs\Zest\Manifest;

class ViewPlugin {

    public function apply(View $view): void {
        $manifest = Manifest::load(
            Genesis::$hub->getApplicationPath() . '/my-theme/manifest.json'
        );

        foreach ($manifest->getCssData() as $file => $attr) {
            /**
             * @var string $file - path to file
             * @var array $attr - array of tag attributes
             */
            $view->addCss($file, $attr);
        }

        foreach ($manifest->getHeadJsData() as $file => $attr) {
            $view->addHeadJs($file, $attr);
        }

        foreach ($manifest->getBodyJsData() as $file => $attr) {
            $view->addFootJs($file, $attr);
        }

        if ($manifest->isHot()) {
            $view->addBodyClass('zest-dev preload');
        }
    }
}

Licensing

Zest is licensed under the MIT License. See LICENSE for the full license text.