class Vite implements Htmlable

Traits

Constants

protected NONCE_CONTEXT_KEY

The Content Security Policy nonce context key.

protected ENTRY_POINTS_CONTEXT_KEY

The entry points context key.

protected PRELOADED_ASSETS_CONTEXT_KEY

The preloaded assets context key.

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected string|false $integrityKey

The key to check for integrity hashes within the manifest.

protected string|null $hotFile

The path to the "hot" file.

protected string $buildDirectory

The path to the build directory.

protected string $manifestFilename

The name of the manifest file.

protected $assetPathResolver

The custom asset path resolver.

protected array $scriptTagAttributesResolvers

The script tag attributes resolvers.

protected array $styleTagAttributesResolvers

The style tag attributes resolvers.

protected array $preloadTagAttributesResolvers

The preload tag attributes resolvers.

static protected array $manifests

The cached manifest files.

protected ViteFonts|null $fonts

The ViteFonts instance.

protected string $fontsManifestFilename

The name of the font manifest file.

protected string|null $prefetchStrategy

The prefetching strategy (waterfall or aggressive) to use.

protected int $prefetchConcurrently

The number of assets to load concurrently when using the "waterfall" strategy.

protected string $prefetchEvent

The name of the event that should trigger prefetching. The event must be dispatched on the window.

Methods

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

array
preloadedAssets()

Get the preloaded assets.

void
setPreloadedAssets(array $preloadedAssets)

No description

string|null
cspNonce()

Get the Content Security Policy nonce applied to all generated tags.

string
useCspNonce(string|null $nonce = null)

Generate or set a Content Security Policy nonce to apply to all generated tags.

useIntegrityKey(string|false $key)

Use the given key to detect integrity hashes in the manifest.

withEntryPoints(array $entryPoints)

Set the Vite entry points.

mergeEntryPoints(array $entryPoints)

Merge additional Vite entry points with the current set.

useManifestFilename(string $filename)

Set the filename for the manifest file.

createAssetPathsUsing(callable|null $resolver)

Resolve asset paths using the provided resolver.

string
hotFile()

Get the Vite "hot" file path.

useHotFile(string $path)

Set the Vite "hot" file path.

useBuildDirectory(string $path)

Set the Vite build directory.

useScriptTagAttributes(callable|array $attributes)

Use the given callback to resolve attributes for script tags.

useStyleTagAttributes(callable|array $attributes)

Use the given callback to resolve attributes for style tags.

usePreloadTagAttributes(callable|array|false $attributes)

Use the given callback to resolve attributes for preload tags.

prefetch(int|null $concurrency = null, string $event = 'load')

Eagerly prefetch assets.

useWaterfallPrefetching(int|null $concurrency = null)

Use the "waterfall" prefetching strategy.

useAggressivePrefetching()

Use the "aggressive" prefetching strategy.

usePrefetchStrategy(string|null $strategy, array $config = [])

Set the prefetching strategy.

__invoke(string|array $entrypoints, string|null $buildDirectory = null)

Generate Vite tags for an entrypoint.

string
makeTagForChunk(string|null $src, string $url, array|null $chunk, array|null $manifest)

Make tag for the given chunk.

string
makePreloadTagForChunk(string|null $src, string $url, array|null $chunk, array $manifest)

Make a preload tag for the given chunk.

array
resolveScriptTagAttributes(string|null $src, string $url, array|null $chunk, array|null $manifest)

Resolve the attributes for the chunks generated script tag.

array
resolveStylesheetTagAttributes(string|null $src, string $url, array|null $chunk, array|null $manifest)

Resolve the attributes for the chunks generated stylesheet tag.

array|false
resolvePreloadTagAttributes(string|null $src, string $url, array|null $chunk, array $manifest)

Resolve the attributes for the chunks generated preload tag.

string
makeScriptTagWithAttributes(string $url, array $attributes)

Generate a script tag with attributes for the given URL.

string
makeStylesheetTagWithAttributes(string $url, array $attributes)

Generate a link tag with attributes for the given URL.

bool
isCssPath(string $path)

Determine whether the given path is a CSS file.

array
parseAttributes(array $attributes)

Parse the attributes into key="value" strings.

HtmlString|null
reactRefresh()

Generate React refresh runtime script.

string
hotAsset(string $asset)

Get the path to a given asset when running in HMR mode.

string|null
devServerUrl()

Get the URL of the running Vite development server.

string
asset(string $asset, string|null $buildDirectory = null)

Get the URL for an asset.

string
content(string $asset, string|null $buildDirectory = null)

Get the content of a given asset.

string
assetPath(string $path, bool|null $secure = null)

Generate an asset path for the application.

string
publicPath(string $path)

Generate a public path for an asset.

array
manifest(string $buildDirectory)

Get the manifest file for the given build directory.

string
manifestPath(string $buildDirectory)

Get the path to the manifest file for the given build directory.

string|null
manifestHash(string|null $buildDirectory = null)

Get a unique hash representing the current manifest, or null if there is no manifest.

array
resolveImports(array $manifest, array $chunk, array $seen = [])

Recursively resolve all imports for the given chunk.

array
chunk(array $manifest, string $file)

Get the chunk for the given entry point / asset.

nonceAttribute()

Get the nonce attribute for the prefetch script tags.

fonts(array|string|null $aliases = null)

Render font preload links and inline styles.

string
renderFontPreloads(array $preloads)

Render preload link tags for font entries.

array|false
resolveFontPreloadAttributes(string $url, array $preload)

Resolve the attributes for a font preload tag.

string
renderFontStyle(array $manifest, array|null $aliases)

Render the inline style block for the font manifest.

viteFonts()

Get the ViteFonts instance.

useFontsManifestFilename(string $filename)

Set the font manifest filename.

bool
isRunningHot()

Determine if the HMR server is running.

string
toHtml()

Get the Vite tag content as a string of HTML.

static void
flush()

Flush all Vite state.

Details

in Macroable at line 30
static void macro(string $name, callable|object $macro)

Register a custom macro.

Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 44
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 61
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 72
static void flushMacros()

Flush the existing macros.

Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 83
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 108
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 112
array preloadedAssets()

Get the preloaded assets.

Return Value

array

at line 117
protected void setPreloadedAssets(array $preloadedAssets)

No description

Parameters

array $preloadedAssets

Return Value

void

at line 125
string|null cspNonce()

Get the Content Security Policy nonce applied to all generated tags.

Return Value

string|null

at line 133
string useCspNonce(string|null $nonce = null)

Generate or set a Content Security Policy nonce to apply to all generated tags.

Parameters

string|null $nonce

Return Value

string

at line 149
Vite useIntegrityKey(string|false $key)

Use the given key to detect integrity hashes in the manifest.

The key is stored on the shared Vite instance and affects every subsequent asset render by this worker.

Parameters

string|false $key

Return Value

Vite

at line 162
Vite withEntryPoints(array $entryPoints)

Set the Vite entry points.

Parameters

array $entryPoints

Return Value

Vite

at line 174
Vite mergeEntryPoints(array $entryPoints)

Merge additional Vite entry points with the current set.

Parameters

array $entryPoints

Return Value

Vite

at line 192
Vite useManifestFilename(string $filename)

Set the filename for the manifest file.

The filename is stored on the shared Vite instance and affects every subsequent manifest lookup by this worker.

Parameters

string $filename

Return Value

Vite

at line 208
Vite createAssetPathsUsing(callable|null $resolver)

Resolve asset paths using the provided resolver.

The resolver is stored on the shared Vite instance and affects every subsequent asset path generated by this worker.

Parameters

callable|null $resolver

Return Value

Vite

at line 219
string hotFile()

Get the Vite "hot" file path.

Return Value

string

at line 232
Vite useHotFile(string $path)

Set the Vite "hot" file path.

The path is stored on the shared Vite instance and affects every subsequent asset render by this worker.

Parameters

string $path

Return Value

Vite

at line 248
Vite useBuildDirectory(string $path)

Set the Vite build directory.

The directory is stored on the shared Vite instance and affects every subsequent asset render by this worker.

Parameters

string $path

Return Value

Vite

at line 265
Vite useScriptTagAttributes(callable|array $attributes)

Use the given callback to resolve attributes for script tags.

The resolver persists on the shared Vite instance and affects every subsequent asset render by this worker.

Parameters

callable|array $attributes

Return Value

Vite

at line 286
Vite useStyleTagAttributes(callable|array $attributes)

Use the given callback to resolve attributes for style tags.

The resolver persists on the shared Vite instance and affects every subsequent asset render by this worker.

Parameters

callable|array $attributes

Return Value

Vite

at line 307
Vite usePreloadTagAttributes(callable|array|false $attributes)

Use the given callback to resolve attributes for preload tags.

The resolver persists on the shared Vite instance and affects every subsequent asset render by this worker.

Parameters

callable|array|false $attributes

Return Value

Vite

at line 327
Vite prefetch(int|null $concurrency = null, string $event = 'load')

Eagerly prefetch assets.

The strategy is stored on the shared Vite instance and affects every subsequent asset render by this worker.

Parameters

int|null $concurrency
string $event

Return Value

Vite

at line 345
Vite useWaterfallPrefetching(int|null $concurrency = null)

Use the "waterfall" prefetching strategy.

The strategy is stored on the shared Vite instance and affects every subsequent asset render by this worker.

Parameters

int|null $concurrency

Return Value

Vite

at line 361
Vite useAggressivePrefetching()

Use the "aggressive" prefetching strategy.

The strategy is stored on the shared Vite instance and affects every subsequent asset render by this worker.

Return Value

Vite

at line 377
Vite usePrefetchStrategy(string|null $strategy, array $config = [])

Set the prefetching strategy.

The strategy is stored on the shared Vite instance and affects every subsequent asset render by this worker.

Parameters

string|null $strategy
array $config

Return Value

Vite

Exceptions

Exception

at line 392
HtmlString __invoke(string|array $entrypoints, string|null $buildDirectory = null)

Generate Vite tags for an entrypoint.

Parameters

string|array $entrypoints
string|null $buildDirectory

Return Value

HtmlString

at line 610
protected string makeTagForChunk(string|null $src, string $url, array|null $chunk, array|null $manifest)

Make tag for the given chunk.

Parameters

string|null $src
string $url
array|null $chunk
array|null $manifest

Return Value

string

at line 639
protected string makePreloadTagForChunk(string|null $src, string $url, array|null $chunk, array $manifest)

Make a preload tag for the given chunk.

Parameters

string|null $src
string $url
array|null $chunk
array $manifest

Return Value

string

at line 659
protected array resolveScriptTagAttributes(string|null $src, string $url, array|null $chunk, array|null $manifest)

Resolve the attributes for the chunks generated script tag.

Parameters

string|null $src
string $url
array|null $chunk
array|null $manifest

Return Value

array

at line 675
protected array resolveStylesheetTagAttributes(string|null $src, string $url, array|null $chunk, array|null $manifest)

Resolve the attributes for the chunks generated stylesheet tag.

Parameters

string|null $src
string $url
array|null $chunk
array|null $manifest

Return Value

array

at line 691
protected array|false resolvePreloadTagAttributes(string|null $src, string $url, array|null $chunk, array $manifest)

Resolve the attributes for the chunks generated preload tag.

Parameters

string|null $src
string $url
array|null $chunk
array $manifest

Return Value

array|false

at line 727
protected string makeScriptTagWithAttributes(string $url, array $attributes)

Generate a script tag with attributes for the given URL.

Parameters

string $url
array $attributes

Return Value

string

at line 741
protected string makeStylesheetTagWithAttributes(string $url, array $attributes)

Generate a link tag with attributes for the given URL.

Parameters

string $url
array $attributes

Return Value

string

at line 755
protected bool isCssPath(string $path)

Determine whether the given path is a CSS file.

Parameters

string $path

Return Value

bool

at line 763
protected array parseAttributes(array $attributes)

Parse the attributes into key="value" strings.

Parameters

array $attributes

Return Value

array

at line 776
HtmlString|null reactRefresh()

Generate React refresh runtime script.

Return Value

HtmlString|null

at line 806
protected string hotAsset(string $asset)

Get the path to a given asset when running in HMR mode.

Parameters

string $asset

Return Value

string

at line 817
string|null devServerUrl()

Get the URL of the running Vite development server.

Return Value

string|null

at line 836
string asset(string $asset, string|null $buildDirectory = null)

Get the URL for an asset.

Parameters

string $asset
string|null $buildDirectory

Return Value

string

at line 854
string content(string $asset, string|null $buildDirectory = null)

Get the content of a given asset.

Parameters

string $asset
string|null $buildDirectory

Return Value

string

Exceptions

ViteException

at line 878
protected string assetPath(string $path, bool|null $secure = null)

Generate an asset path for the application.

Parameters

string $path
bool|null $secure

Return Value

string

at line 886
protected string publicPath(string $path)

Generate a public path for an asset.

Parameters

string $path

Return Value

string

at line 896
protected array manifest(string $buildDirectory)

Get the manifest file for the given build directory.

Parameters

string $buildDirectory

Return Value

array

Exceptions

ViteException

at line 930
protected string manifestPath(string $buildDirectory)

Get the path to the manifest file for the given build directory.

Parameters

string $buildDirectory

Return Value

string

at line 938
string|null manifestHash(string|null $buildDirectory = null)

Get a unique hash representing the current manifest, or null if there is no manifest.

Parameters

string|null $buildDirectory

Return Value

string|null

at line 956
protected array resolveImports(array $manifest, array $chunk, array $seen = [])

Recursively resolve all imports for the given chunk.

Parameters

array $manifest
array $chunk
array $seen

Return Value

array

at line 982
protected array chunk(array $manifest, string $file)

Get the chunk for the given entry point / asset.

Parameters

array $manifest
string $file

Return Value

array

Exceptions

ViteException

at line 994
protected HtmlString nonceAttribute()

Get the nonce attribute for the prefetch script tags.

Return Value

HtmlString

at line 1010
HtmlString fonts(array|string|null $aliases = null)

Render font preload links and inline styles.

Parameters

array|string|null $aliases

Return Value

HtmlString

Exceptions

ViteException

at line 1052
protected string renderFontPreloads(array $preloads)

Render preload link tags for font entries.

Parameters

array $preloads

Return Value

string

at line 1088
protected array|false resolveFontPreloadAttributes(string $url, array $preload)

Resolve the attributes for a font preload tag.

Parameters

string $url
array $preload

Return Value

array|false

at line 1116
protected string renderFontStyle(array $manifest, array|null $aliases)

Render the inline style block for the font manifest.

Parameters

array $manifest
array|null $aliases

Return Value

string

at line 1136
protected ViteFonts viteFonts()

Get the ViteFonts instance.

Return Value

ViteFonts

at line 1149
Vite useFontsManifestFilename(string $filename)

Set the font manifest filename.

The filename is stored on the shared Vite instance and affects every subsequent font manifest lookup by this worker.

Parameters

string $filename

Return Value

Vite

at line 1160
bool isRunningHot()

Determine if the HMR server is running.

Return Value

bool

at line 1168
string toHtml()

Get the Vite tag content as a string of HTML.

Return Value

string

at line 1181
static void flush()

Flush all Vite state.

Clears worker-wide Vite manifests and request-local context used by subsequent asset rendering.

Return Value

void