millancore / vite-manifest
PHP Vite Manifest Manager
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/millancore/vite-manifest
Requires
- php: ^8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.87
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.3
README
A PHP library for managing Vite asset manifests and generating HTML tags for JavaScript and CSS assets.
This library was created inspired by the Laravel Vite implementation, providing a similar API for PHP applications to integrate Vite-built assets.
Requirements
- PHP 8.2 or higher
Installation
Install the library via Composer:
composer require millancore/vite-manifest
Usage
Basic Usage
Create a new ViteManager
instance and invoke it with your entry points:
use Millancore\Vite\ViteManager; $vite = new ViteManager(); $vite->setBuildDirectory('public/build'); // Optional, defaults to 'build' echo $vite(['resources/js/app.js', 'resources/css/app.css']);
This will generate the necessary <script>
, <link>
, and <link rel="preload">
tags for your assets.
Configuration
Build Directory
Set the build directory where Vite outputs your assets:
$vite->setBuildDirectory('dist');
Manifest Filename
Customize the manifest filename (defaults to manifest.json
):
$vite->setManifestFilename('vite-manifest.json');
Hot Module Replacement (HMR)
For development with HMR, set the hot file path:
$vite->setHotFile('public/hot');
Content Security Policy (CSP) Nonce
Add a nonce for CSP:
$vite->setNonce('your-nonce-value');
Subresource Integrity
Enable SRI with a key:
$vite->setIntegrityKey('your-integrity-key');
Custom Resolvers
Add custom resolvers for scripts, styles, or preloads:
$vite->addScriptResolver(function ($url, $chunk, $manifest) { // Custom script tag logic return '<script src="' . $url . '" defer></script>'; }); $vite->addStyleResolver(function ($url, $chunk, $manifest) { // Custom style tag logic return '<link rel="stylesheet" href="' . $url . '">'; }); $vite->addPreloadResolver(function ($url, $chunk, $manifest) { // Custom preload tag logic return '<link rel="preload" href="' . $url . '" as="script">'; });
Asset Path Resolver
Customize how asset paths are resolved:
$vite->setAssetPathResolver(function ($path) { return 'https://cdn.example.com/' . $path; });
Features
- Automatic generation of HTML tags for Vite-built assets
- Support for Hot Module Replacement (HMR) in development
- Content Security Policy (CSP) nonce support
- Subresource Integrity (SRI) support
- Customizable asset path resolution
- Extensible tag generation with resolvers
- PSR-4 autoloading
License
This library is licensed under the MIT License. See the LICENSE file for details.