ViteHelper
in package
Helper class for Vite asset loading.
Provides methods for loading Vite-bundled assets in both development mode (with HMR) and production mode (manifest-based).
Tags
Table of Contents
Properties
- $manifest : array<string, mixed>|null
- Cached manifest data.
Methods
- assets() : string
- Generate HTML tags for Vite assets.
- criticalCss() : string
- Get critical CSS for initial render.
- getManifest() : array<string, mixed>|null
- Get the Vite manifest file contents.
- isDevServerRunning() : bool
- Check if Vite development server is running.
- resetCache() : void
- Reset the cached manifest (useful for testing).
- shouldUse() : bool
- Determine whether to use Vite assets or legacy assets.
Properties
$manifest
Cached manifest data.
private
static array<string, mixed>|null
$manifest
= null
Methods
assets()
Generate HTML tags for Vite assets.
public
static assets([string $entry = 'js/main.ts' ][, bool $asyncCss = true ]) : string
In development mode, loads assets from Vite dev server with HMR. In production mode, loads assets from the manifest file.
Parameters
- $entry : string = 'js/main.ts'
-
The entry point path (e.g., 'js/main.ts')
- $asyncCss : bool = true
-
Whether to load CSS asynchronously (non-render-blocking)
Return values
string —HTML script and link tags
criticalCss()
Get critical CSS for initial render.
public
static criticalCss() : string
This returns inline CSS that should be included in the
to prevent Flash of Unstyled Content (FOUC) while the main CSS loads async.Return values
string —Inline critical CSS wrapped in <style> tags
getManifest()
Get the Vite manifest file contents.
public
static getManifest() : array<string, mixed>|null
Return values
array<string, mixed>|null —Manifest array or null if not found
isDevServerRunning()
Check if Vite development server is running.
public
static isDevServerRunning() : bool
Return values
bool —True if dev server is detected and responding
resetCache()
Reset the cached manifest (useful for testing).
public
static resetCache() : void
shouldUse()
Determine whether to use Vite assets or legacy assets.
public
static shouldUse() : bool
Checks the LWT_ASSET_MODE environment variable:
- 'vite': Always use Vite assets
- 'legacy': Always use legacy PHP-minified assets
- 'auto' or unset: Use Vite if manifest exists, otherwise legacy
Return values
bool —True if Vite assets should be used