Vite
Production Assets
Vite can render asset tags in both Blade and Razor:
@Vite.Assets("resources/js/app.js", "resources/css/app.css")The block above is Razor (.cshtml). In a Blade (.blade.html) layout, write {{ Vite.Assets("resources/js/app.js", "resources/css/app.css") }}.
Configure Vite to emit a manifest and assets under wwwroot/build:
// vite.config.mjs
import { defineConfig } from "vite";
export default defineConfig(({ command }) => ({
base: command === "serve" ? "/" : "/build/",
build: {
outDir: "wwwroot/build",
manifest: true,
rollupOptions: {
input: ["resources/js/app.js", "resources/css/app.css"],
},
},
}));Install Vite with npm install --save-dev vite, then run npx vite build. Vite.Assets reads wwwroot/build/.vite/manifest.json, includes imported chunks and their CSS, and deduplicates tags within one call. JavaScript receives module script tags; CSS receives stylesheet tags; imported JavaScript receives module preload links.
Vite.Asset("resources/js/app.js") returns only the asset URL. URLs include the request's PathBase; set Vite's base to match the deployed asset URL prefix when hosting under a subpath. Missing manifests or entry names produce explicit errors.
Development Server
Run npx vite --host localhost --port 5173 --strictPort. Put its URL in wwwroot/hot:
http://localhost:5173In the ASP.NET Core Development environment, the hot file makes Vite.Assets use that server and include /@vite/client. Configure Vite's allowed CORS origin to match the application's development URL when they run on different origins.
Remove the hot file when switching back to built assets. Production ignores hot files and always uses the manifest. Hot files accept only HTTP(S) URLs.
Configuration
Optional settings in config/Vite.cs:
public class ViteConfig : ConfigFile
{
public override object Values() => new
{
build_directory = "build",
hot_file = "wwwroot/hot",
};
}build_directory is relative to the web root; hot_file is absolute or relative to the application content root. Entry names and manifest asset paths must be relative and cannot contain path traversal.
The template's Tailwind CLI build remains available. Applications opting into Vite supply their Vite configuration and frontend dependencies.
Complete Example: Opt Into Vite
In a Blog template application, install the bundler and Tailwind's Vite plugin:
npm install --save-dev vite @tailwindcss/viteUse this complete configuration instead of the simpler plain-CSS configuration above:
// vite.config.mjs
import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig(({ command }) => ({
base: command === "serve" ? "/" : "/build/",
plugins: [tailwindcss()],
server: {
host: "localhost",
port: 5173,
strictPort: true,
cors: { origin: "http://localhost:5000" },
},
build: {
outDir: "wwwroot/build",
emptyOutDir: true,
manifest: true,
rollupOptions: { input: ["resources/js/app.js", "resources/css/app.css"] },
},
}));// resources/js/app.js
import Alpine from "alpinejs";
window.Alpine = Alpine;
Alpine.start();Keep the template's resources/css/app.css. In resources/views/layouts/app.blade.html, replace the old app.css link and alpine.js script with the following, keeping Live before Alpine:
{{ Live.Scripts() }}
{{ Vite.Assets("resources/js/app.js", "resources/css/app.css") }}Keep only one Live.Scripts() call if the layout already contains it. Replace package.json's dev and build scripts with "dev": "vite" and "build": "vite build"; preserve its other fields.
Run npm run build, then dotblaze serve. Expected: hashed files plus .vite/manifest.json under wwwroot/build, styled pages, and the template's Alpine counter working. For HMR, run npm run dev in another terminal, write http://localhost:5173 into wwwroot/hot, and set ASPNETCORE_ENVIRONMENT=Development. Match server.cors.origin to the actual application origin. Remove wwwroot/hot when returning to built assets.