PHP code example of builtnoble / vite-php

1. Go to this page and download the library: Download builtnoble/vite-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

builtnoble / vite-php example snippets


use Builtnoble\VitePHP\ViteFactory;
use Random\RandomException;

// Basic usage (standalone):
// Build a configured Vite instance and invoke it with an array of asset paths.
// Passing 'nonce' => null requests a generated nonce (may throw RandomException).
try {
    $vite = ViteFactory::make([
        'hotfile' => '/tmp/vite-dev-server',
        'buildDir' => 'dist',
        'publicPath' => 'public',
        'manifestFilename' => 'manifest.json',
        'integrityKey' => 'my_integrity_key',
        'nonce' => null, // pass null to generate a random nonce
    ]);

    // invoke Vite with an array of asset paths; the invokable returns the rendered tags/HTML
    echo ($vite)([
        'resources/css/app.css',
        'resources/js/app.js',
    ]);
} catch (RandomException $e) {
    // handle nonce generation failure
    throw $e;
}

// Advanced: provide a custom creator and attribute resolvers
$customCreator = function (): Builtnoble\VitePHP\Vite {
    $v = new Builtnoble\VitePHP\Vite();
    $v->setBuildDir('custom-dist');
    return $v;
};

$vite = ViteFactory::make([
    'scriptTagAttributesResolvers' => [
        // resolver receives (string $path, ?array $manifestEntry) and returns attributes
        function (string $path, array $entry = null): array {
            return ['defer' => true];
        },
    ],
    'styleTagAttributesResolvers' => function (string $path, array $entry = null): array {
        return ['media' => 'all'];
    },
], $customCreator);

// invoke with specific assets
echo ($vite)([
    'resources/js/app.js',
]);
bash
composer