Docs / The Basics / Asset Bundling (Vite)
Asset Bundling (Vite)
Introduction
Vite is a modern frontend build tool that provides an extremely fast development environment and bundles your code for production. When building applications with Laraflare, you will use Vite to bundle your application's CSS and Vue components into production-ready assets.
Laraflare has no Vite plugin and no template directive for loading assets: your screens are Vue components, and the page shell is the index.html file Vite uses as its entry (see frontend).
Installation & Setup
Note
A new Laraflare application already includes all of this scaffolding, so there is nothing to install or configure to get started. The sections below describe what is there.
Installing Node
You must ensure that Node.js and NPM are installed before running Vite:
node -v
npm -v
You can easily install the latest version of Node and NPM using simple graphical installers from the official Node website.
Installing Vite
In a fresh Laraflare application, you will find a package.json file in the root of your application's directory structure. The default package.json file already includes everything you need to get started using Vite, Vue and Tailwind. You may install your application's frontend dependencies via NPM:
npm install
Configuring Vite
Vite is configured via a vite.config.ts file in the root of your project. You are free to customize this file based on your needs, and you may also install any other plugins your application requires.
The file that ships with a new application loads the Vue and Tailwind plugins, serves everything under /build/, and builds index.html, the page shell, as its single entry point into public/build:
import path from 'node:path';
import tailwindcss from '@tailwindcss/vite';
import vue from '@vitejs/plugin-vue';
import { defineConfig } from 'vite';
export default defineConfig({
base: '/build/',
publicDir: false,
plugins: [
tailwindcss(),
vue(),
],
build: {
outDir: 'public/build',
emptyOutDir: true,
rollupOptions: {
input: path.resolve('index.html'),
},
},
});
When the build finishes, a small plugin in the same file moves the built index.html to public/index.html, where the Worker's static assets serve it for every navigation that matches no file.
Loading Your Scripts and Styles
Your Vite entry point is referenced by a plain script tag in the <head> of index.html. Vite rewrites it, and adds the compiled and versioned assets, including any imported CSS, when it builds the page:
<head>
<!-- ... -->
<script type="module" src="/resources/js/app.ts"></script>
</head>
Because the application is a single-page application, its CSS is imported from JavaScript, in resources/js/app.ts:
import '../css/app.css';
Running Vite
Locally, Valet starts the development process the first time you open your application at its .test address, so there is usually nothing to run. Without Valet, you may start it yourself via the dev command. It builds the page and its assets, serves the application, and rebuilds on every change.
Or, running the build command will version and bundle your application's assets and get them ready to go live (wrangler runs it when you deploy; see deployment):
# Build the assets and serve the application...
npm run dev
# Build and version the assets for production...
npm run build
Note
The development process runs
vite build --watchbesidenpx artisan serve, so the browser is served the built assets and does not use Vite's development server or Hot Module Replacement: reload the page to see a change.
Working With JavaScript
Aliases
By default, a Laraflare application provides a common alias to help you hit the ground running and conveniently import your application's assets:
{
'@': path.resolve('resources/js'),
}
You may overwrite the '@' alias by changing it in the vite.config.ts configuration file:
export default defineConfig({
// ...
resolve: {
alias: {
'@': path.resolve('resources/ts'),
},
},
});
Vue
Laraflare's screens are built with Vue, so the @vitejs/plugin-vue plugin is already installed and included in your vite.config.ts configuration file. There are a few additional options set when using the Vue plugin:
vue({
template: {
transformAssetUrls: {
// The Vue plugin will re-write asset URLs, when referenced
// in Single File Components, to point to the web server.
// Setting this to `null` leaves them to Vite instead.
base: null,
// The Vue plugin will parse absolute URLs and treat them
// as absolute paths to files on disk. Setting this to
// `false` will leave absolute URLs un-touched so they can
// reference assets in the public directory as expected.
includeAbsolute: false,
},
},
}),
Note
Laraflare has no React, Svelte or Inertia support, and no server-side rendering; see frontend.
URL Processing
When using Vite and referencing assets in your application's HTML, CSS, or JS, there are a couple of caveats to consider. First, if you reference assets with an absolute path, Vite will not include the asset in the build; therefore, you should ensure that the asset is available in your public directory.
When referencing relative asset paths, you should remember that the paths are relative to the file where they are referenced. Any assets referenced via a relative path will be re-written, versioned, and bundled by Vite.
Consider the following project structure:
public/
taylor.png
resources/
js/
pages/
Welcome.vue
images/
abigail.png
The following example demonstrates how Vite will treat relative and absolute URLs:
<!-- This asset is not handled by Vite and will not be included in the build -->
<img src="/taylor.png">
<!-- This asset will be re-written, versioned, and bundled by Vite -->
<img src="../../images/abigail.png">
Working With Stylesheets
All Laraflare applications already include Tailwind and a properly configured vite.config.ts file. So, you only need to start the development process or run the build command:
npm run dev
Your application's CSS may be placed within the resources/css/app.css file.
Environment Variables
You may inject environment variables into your JavaScript by prefixing them with VITE_ in your application's .env file:
VITE_SENTRY_DSN_PUBLIC=http://example.com
You may access injected environment variables via the import.meta.env object:
import.meta.env.VITE_SENTRY_DSN_PUBLIC
A new application's .env already sets VITE_APP_NAME, which vite.config.ts falls back from to APP_NAME and makes available to the screens and to the page's <title>.