Vite Integration

Litestar Vite provides seamless integration with Vite, a modern frontend build tool.

Installation

Install the package:

pip install litestar-vite

Note: If you do not have an existing node environment, you can use nodeenv to automatically configure one for you by using the litestar-vite[nodeenv] extras option.

Setup Options

There are two ways to set up Vite with Litestar:

2. Manual Setup

If you prefer more control, you can set up Vite manually:

  1. Initialize the frontend project:

npm init -y
npm install vite
npm install -D litestar-vite-plugin
  1. Create vite.config.js:

import { defineConfig } from 'vite'
import litestar from 'litestar-vite-plugin'

export default defineConfig({
    plugins: [
        litestar({
            input: {
                main: 'resources/js/main.js',
                styles: 'resources/css/styles.css'
            },
            reload: true
        })
    ]
})

Configuration

Litestar Configuration

Configure your Litestar application:

from litestar import Litestar
from litestar_vite import ViteConfig, VitePlugin

app = Litestar(
    plugins=[
        VitePlugin(
            config=ViteConfig(
                use_server_lifespan=True,    # Manage Vite server lifecycle
                dev_mode=True,               # Enable vite dev mode
                hot_reload=True,             # Enable HMR in development
            )
        )
    ]
)

Template Integration

Create templates that use Vite assets:

<!DOCTYPE html>
<html>
<head>
    {{ vite('resources/css/styles.css') }}
</head>
<body>
    <div id="app"></div>
    {{ vite('resources/js/main.js') }}
    {{ vite_hmr() }}
</body>
</html>

Development Workflow

Development Server

The litestar CLI is able to manage the Vite development process when using the use_server_lifespan option. When this is enabled, the CLI will automatically manage the Vite server lifecycle with the Litestar application. This command will automatically serve the the application in dev and production mode.

litestar run

However, if you would like to manage the Vite server lifecycle manually, you can use the following commands:

Note: You will likely need to disable the use_server_lifespan option in your ViteConfig if you are managing the Vite server lifecycle manually.

  1. Start the Vite development server using the CLI:

# Using the CLI
litestar assets serve
  1. Run your Litestar application:

litestar run

Production

Building Assets

Build your assets for production:

# Using the CLI
litestar assets build

# Or manually
npm run build

The build process will:

  1. Bundle and optimize all assets

  2. Generate a manifest file

  3. Output files to the bundle_dir

For more information about Inertia integration, refer to the Inertia documentation.