Skip to content

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.

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"]

ProcessWire\Vite merges configuration from three layers in order of increasing priority:

  1. Module Configuration (Modules -> Configure -> Vite): Stored in ProcessWire’s module settings (rootPath, rootUrl, buildDirectory, hotFile, manifest, integrity, nonce).
  2. Site Config (setting('vite', [...])): Defined in site/config.php or site/init.php for environment-specific overrides without touching the database.
  3. Fluent Per-Call Overrides (use*()): Chained on vite() or vite()->copy() for a single request or isolated build.
  • Hot Mode: When the configured hotFile exists on disk (default site/templates/hot or {rootPath}/{hotFile}), isRunningHot() returns true. Vite reads 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 hot file exists, Vite looks 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 multiple vite() 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 in preloadedAssets()).
  • 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 $visitedImports map so traversal never enters an infinite loop.

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.

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.

Next steps