Vite Extension #
ViteExtension integrates Sugar templates with Vite asset tags.
Use it to render Vite entries directly from templates with s:vite, while keeping environment-specific behavior in PHP configuration. In development, it can emit dev-server tags (including @vite/client when enabled), and in production it resolves assets from your Vite manifest.
The extension is intentionally backend-oriented: you keep your normal Vite setup for bundling and dev server behavior, and Sugar focuses on resolving and outputting the correct tags at render time.
assetBaseUrl is required configuration for the extension.
Register the extension #
use Sugar\Core\Engine;
use Sugar\Extension\Vite\ViteExtension;
$engine = Engine::builder()
->withExtension(new ViteExtension(
mode: 'auto',
manifestPath: '/var/www/app/webroot/build/manifest.json',
assetBaseUrl: '/build/',
devServerUrl: 'http://localhost:5173',
injectClient: true,
))
->build();
Using s:vite #
Use the directive where you want Vite tags emitted:
<s-template s:vite="resources/assets/js/site.js" />
You can also apply it on regular elements:
<link s:vite="resources/assets/css/app.css" />
ViteDirective also supports custom-element syntax through element claiming:
<s-vite src="resources/assets/js/site.js" />
In this form, src is used as the directive expression and produces the same output as s:vite="...".
Runtime modes #
-
auto: Uses development behavior in debug mode and production behavior otherwise. -
dev: Always emits dev server tags. -
prod: Always resolves assets frommanifest.json.
Namespaces #
For multi-build setups — such as a plugin or theme with its own Vite build — you can register named namespaces. Each namespace is an independent ViteConfig with its own assetBaseUrl, manifestPath, and optionally a separate devServerUrl.
Register namespaces when creating the extension:
use Sugar\Core\Engine;
use Sugar\Extension\Vite\ViteConfig;
use Sugar\Extension\Vite\ViteExtension;
$engine = Engine::builder()
->withExtension(new ViteExtension(
assetBaseUrl: '/build/',
manifestPath: ROOT . '/webroot/build/.vite/manifest.json',
namespaces: [
'theme' => new ViteConfig(
assetBaseUrl: '/theme/build/',
manifestPath: ROOT . '/plugins/Theme/webroot/build/.vite/manifest.json',
devServerUrl: 'http://localhost:5174',
defaultEntry: 'resources/js/theme.ts',
),
],
))
->build();
Reference a namespace entry in templates using the @name/ prefix:
<s-template s:vite="'@theme/resources/js/theme.ts'" />
When a namespace has a defaultEntry configured, you can use the bare @name shorthand to load it without specifying an explicit path:
<s-template s:vite="'@theme'" />
Multiple entries from different namespaces can be combined in one directive:
<s-template s:vite="['resources/js/app.ts', '@theme/resources/js/theme.ts']" />
Each namespace resolves its assets independently: its own manifest in production, its own dev server URL in development, and its own @vite/client injection tracking.
When devServerUrl is omitted from a ViteConfig, the namespace falls back to the root extension devServerUrl.
Configuration options #
-
mode:auto,dev, orprod. -
manifestPath: Absolute filesystem path to the Vitemanifest.json. -
assetBaseUrl: Public URL prefix for built assets (required). -
devServerUrl: Vite development server origin. -
injectClient: Automatically inject@vite/clientin development mode. -
defaultEntry: Entry used whens:viteis used as a boolean directive. -
namespaces: Named namespace configurations as an array ofViteConfigobjects, keyed by namespace name.
ViteConfig options #
ViteConfig is used to configure a named namespace. All options mirror the top-level extension options and apply only to that namespace.
-
assetBaseUrl: Public URL prefix for built assets (required). -
manifestPath: Absolute filesystem path to the Vitemanifest.json. -
devServerUrl: Dev server origin for this namespace. Falls back to the rootdevServerUrlwhen omitted. -
injectClient: Whether to inject@vite/clientfor this namespace in development mode. Defaults totrue. -
defaultEntry: Default entry resolved when the namespace is referenced without an explicit path (e.g.'@theme'or'@theme/'). Throws at render time if the shorthand is used anddefaultEntryis not configured.
Production recommendation #
Set manifestPath and assetBaseUrl explicitly in production. This applies equally to each registered namespace.