Skip to content

Asset Bundling

David Ciulla edited this page Nov 7, 2026 · 1 revision

Asset Bundling

Laravex uses Vite to bundle frontend assets — JavaScript, CSS, and any other resources declared in vite.config.js. The integration mirrors Laravel's first-party Vite plugin: a @vite Blade directive switches automatically between HMR dev-server URLs and production manifest paths.


Project Structure

laravex/
├── resources/
│   ├── css/
│   │   └── app.css        # main stylesheet entry point
│   └── js/
│       └── app.js         # main JavaScript entry point
├── public/
│   ├── build/             # compiled assets (committed or deployed)
│   │   ├── app.css
│   │   └── app.js
│   └── hot                # written by `vite --watch`; signals dev mode
└── vite.config.js

Configuration

vite.config.js at the project root:

// vite.config.js
export default {
  build: {
    outDir: "public/build",
    rollupOptions: {
      input: ["resources/css/app.css", "resources/js/app.js"],
    },
  },
};
Option Value
build.outDir public/build — compiled files land here
rollupOptions.input Entry points fed to Rollup during vite build

Add additional entry points to the input array as needed (e.g. page-specific JS, TypeScript files).


Running Vite

Development (HMR)

npx vite
# or
npm run dev

Vite starts its dev server and writes public/hot containing the base URL (e.g. http://localhost:5173). While public/hot is present, the @vite directive emits HMR-aware <script type="module"> and <link rel="stylesheet"> tags pointing at the dev server.

Production build

npx vite build
# or
npm run build

Compiles and writes fingerprinted assets to public/build/. Removes (or never creates) public/hot, so the @vite directive falls back to static public/build/ paths.


The @vite Directive

Include your entry points in the layout's <head>:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  @vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body>
  @yield('content')
</body>
</html>

Development output (when public/hot contains http://localhost:5173):

<link rel="stylesheet" href="http://localhost:5173/resources/css/app.css">
<script type="module" src="http://localhost:5173/resources/js/app.js"></script>

Production output (no public/hot file):

<link rel="stylesheet" href="/build/css/app.css">
<script type="module" src="/build/app.js"></script>

Asset detection is based on file extension: .css files emit <link> tags; everything else emits <script type="module"> tags.


How It Works

The Laravex.Vite.tags/1 function (called by the @vite Blade directive) checks for public/hot at startup:

  1. Hot file present — reads the URL from the file, prepends it to each asset path, and emits HMR tags.
  2. No hot file — strips the resources/ prefix from each asset path and emits static /build/… tags.
# Equivalent Elixir call (usually done via @vite in a template)
Laravex.Vite.tags(["resources/css/app.css", "resources/js/app.js"])

Entry Points

resources/js/app.js is the JavaScript bootstrap:

// resources/js/app.js
console.log("Laravex assets loaded");
// import Alpine from 'alpinejs';
// import './bootstrap';

resources/css/app.css is the global stylesheet:

/* resources/css/app.css */
body {
  margin: 0;
}

Replace or extend these files as your application grows.


npm Scripts (recommended)

Add convenience scripts to package.json:

{
  "scripts": {
    "dev":   "vite",
    "build": "vite build"
  },
  "devDependencies": {
    "vite": "^5.0.0"
  }
}

Related

Clone this wiki locally