Core Concepts
The Vite module is a lightweight, zero-dependency bridge between PHP templates and Vite’s build output. Understanding its five core primitives prevents common misconfigurations in dev, staging, and multi-build setups.
Asset Resolution Lifecycle
Section titled “Asset Resolution Lifecycle”flowchart TD
A["Template calls vite('src/app.ts')"] --> B{"hot file exists?"}
B -- Yes --> C["Read dev server URL from hot file"]
C --> D["Emit @vite/client + optional React Refresh"]
D --> E["Emit module script & stylesheet tags from dev server"]
B -- No --> F["Load & cache manifest.json (or .vite/manifest.json)"]
F --> G["Resolve entry & recursively walk imports[] and css[]"]
G --> H["Collect modulepreload hints, SRI hashes & CSP nonce"]
H --> I["Render ordered <link> and <script> HTML tags"]
1. Config Precedence Layers
Section titled “1. Config Precedence Layers”ProcessWire\Vite merges configuration from three layers in order of increasing priority:
- Module Configuration (Modules -> Configure -> Vite): Stored in ProcessWire’s module settings (
rootPath,rootUrl,buildDirectory,hotFile,manifest,integrity,nonce). - Site Config (
setting('vite', [...])): Defined insite/config.phporsite/init.phpfor environment-specific overrides without touching the database. - Fluent Per-Call Overrides (
use*()): Chained onvite()orvite()->copy()for a single request or isolated build.
2. Hot Mode (HMR) vs. Manifest Mode
Section titled “2. Hot Mode (HMR) vs. Manifest Mode”- Hot Mode: When the configured
hotFileexists on disk (defaultsite/templates/hotor{rootPath}/{hotFile}),isRunningHot()returnstrue.Vitereads the dev server origin URL from the file, strips any trailing slash, and emits tags pointing directly to the Vite dev server (including@vite/client). - Manifest Mode: When no
hotfile exists,Vitelooks for{rootPath}/{buildDirectory}/{manifest}first and falls back to{rootPath}/{buildDirectory}/.vite/{manifest}(Vite 5+ default). Parsed manifests are cached in memory per request so multiplevite()calls never re-read disk.
3. Transitive Chunk Traversal & Cycle Protection
Section titled “3. Transitive Chunk Traversal & Cycle Protection”When a production entry imports shared chunks that themselves import CSS files or secondary JS chunks, Vite walks the entire imports tree recursively:
- Every referenced JS chunk gets a
<link rel="modulepreload">tag (and is tracked inpreloadedAssets()). - Every CSS file on the entry or its transitive imports gets a
<link rel="stylesheet">tag, deduplicated across entries. - Circular chunk graphs are tracked in an internal
$visitedImportsmap so traversal never enters an infinite loop.
4. Optional @ Entry Prefix
Section titled “4. Optional @ Entry Prefix”Prefixing an entry path with @ tells Vite to skip the entry silently if it does not exist on disk (in hot mode) or in the manifest (in production mode):
echo vite([ 'resources/ts/app.ts', "@resources/ts/templates/{$page->template->name}.ts",]);If templates/yacht.ts exists, it is loaded; if templates/basic-page.ts does not exist, no exception is thrown.
5. Immutable Clones via copy()
Section titled “5. Immutable Clones via copy()”Calling $this->wire()->vite->copy() (or vite()->copy()) returns a fresh Totoglu\Vite\Vite instance seeded with the current configuration. Use copy() whenever you customize useBuildDirectory(), useHotFile(), useNonce(), or tag attributes for a secondary build so you never mutate the shared singleton.