Configuration, HMR & Multi-Build
Vite supports three configuration tiers, fluent tag customization, static asset URL lookups, and isolated multi-build instances for complex ProcessWire applications.
Configuring via site/config.php
Section titled “Configuring via site/config.php”Use ProcessWire’s setting('vite', [...]) helper in site/config.php or site/init.php to override module defaults per environment:
setting('vite', [ 'rootPath' => $config->paths->templates, 'rootUrl' => $config->urls->templates, 'buildDirectory' => 'build', 'hotFile' => 'hot', 'manifest' => 'manifest.json', 'integrity' => 'integrity', 'nonce' => null,]);Fluent Per-Call Overrides
Section titled “Fluent Per-Call Overrides”Every configuration option has a matching fluent setter on Totoglu\Vite\Vite:
$tags = $this->wire()->vite ->copy() ->useRootPath($this->wire()->config->paths->templates) ->useRootUrl($this->wire()->config->urls->templates) ->useBuildDirectory('dist/charter') ->useHotFile('hot-charter') ->useManifest('manifest.json') ->withEntries([ 'resources/ts/charter-search.ts', '@resources/ts/templates/yacht.ts', ]);
echo $tags;Enabling React Fast Refresh (HMR)
Section titled “Enabling React Fast Refresh (HMR)”When your Vite build uses @vitejs/plugin-react, call withReactRefresh() (or emit $vite->reactRefresh() before your entry tags) so React Fast Refresh initializes cleanly in hot mode and outputs nothing in production:
echo vite() ->copy() ->withReactRefresh() ->withEntries(['resources/tsx/booking-widget.tsx']);Resolving Static Assets (asset() and url())
Section titled “Resolving Static Assets (asset() and url())”Need the hashed URL for an image, font, or SVG referenced inside a PHP template (such as an Open Graph image or favicon)?
$heroUrl = $this->wire()->vite->asset('resources/images/aegean-gulet-hero.webp');- In hot mode,
asset()returns{devServerOrigin}/{entry}. - In production mode,
asset()resolves the hashed file path from the Vite manifest and prefixes it withrootUrlandbuildDirectory. - Pass a second
$buildDirectoryargument toasset('resources/img/logo.svg', 'build-admin')to resolve against an alternate build directory without mutating the singleton.
Customizing HTML Tag Attributes
Section titled “Customizing HTML Tag Attributes”Use useScriptTagAttributes(), useStyleTagAttributes(), and usePreloadTagAttributes() with either a static associative array or a callback (string $src, string $url, ?array $chunk, ?array $manifest): array:
$headTags = $this->wire()->vite ->copy() ->useScriptTagAttributes([ 'data-turbo-track' => 'reload', 'defer' => true, ]) ->useStyleTagAttributes([ 'media' => 'screen', ]) ->usePreloadTagAttributes(static function (string $src, string $url): array { return ['crossorigin' => 'anonymous']; }) ->withEntries(['resources/ts/app.ts']);Running Multiple Independent Builds
Section titled “Running Multiple Independent Builds”ProcessWire projects often combine a site frontend build with one or more companion module builds. Always call copy() so each build keeps its own rootPath, rootUrl, buildDirectory, and hotFile:
$moduleAssets = $this->wire()->vite ->copy() ->useRootPath(__DIR__) ->useRootUrl($this->wire()->config->urls->siteModules . 'MyModule/') ->useBuildDirectory('dist') ->useHotFile('hot') ->withEntries(['resources/ts/widget.ts']);