Skip to main content

Litestar Vite

[!IMPORTANT] This plugin currently contains minimal features and is a work-in-progress

Installation

pip install litestar-vite

Usage

Here is a basic application that demonstrates how to use the plugin.

from __future__ import annotations

from pathlib import Path

from litestar import Controller, get, Litestar
from litestar.response import Template
from litestar.status_codes import HTTP_200_OK
from litestar_vite import ViteConfig, VitePlugin

class WebController(Controller):

    opt = {"exclude_from_auth": True}
    include_in_schema = False

    @get(["/", "/{path:str}"],status_code=HTTP_200_OK)
    async def index(self) -> Template:
        return Template(template_name="index.html.j2")


vite = VitePlugin(config=ViteConfig(template_dir='templates/'))
app = Litestar(plugins=[vite], route_handlers=[WebController])

Create a template to serve the application in ./templates/index.html.h2:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <!--IE compatibility-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0"
    />
  </head>

  <body>
    <div id="app"></div>
    {{ vite_hmr() }} {{ vite('resources/main.ts') }}
  </body>
</html>

Template initialization (Optional)

This is a command to help initialize Vite for your project. This is generally only needed a single time. You may also manually configure Vite and skip this step.

to initialize a Vite configuration:

❯ litestar assets init
Using Litestar app from app:app
Initializing Vite ──────────────────────────────────────────────────────────────────────────────────────────
Do you intend to use Litestar with any SSR framework? [y/n]: n
INFO - 2023-12-11 12:33:41,455 - root - commands - Writing vite.config.ts
INFO - 2023-12-11 12:33:41,456 - root - commands - Writing package.json
INFO - 2023-12-11 12:33:41,456 - root - commands - Writing tsconfig.json

Install Javascript/Typescript Packages

Install the packages required for development:

Note This is equivalent to the the npm install by default. This command is configurable.

❯ litestar assets install
Using Litestar app from app:app
Starting Vite package installation process ──────────────────────────────────────────────────────────────────────────────────────────

added 25 packages, and audited 26 packages in 1s


5 packages are looking for funding
  run `npm fund` for details


found 0 vulnerabilities

Development

To automatically start and stop the Vite instance with the Litestar application, you can enable the use_server_lifespan hooks in the ViteConfig.

Alternately, to start the development server manually, you can run the following

❯ litestar assets serve
Using Litestar app from app:app
Starting Vite build process ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

> build
> vite build


vite v5.0.7 building for production...

✓ 0 modules transformed.

Note This is equivalent to the the npm run dev command when hot_reload is enabled. Otherwise it is equivalent to npm run build -- --watch. This command is configurable.

Building for Production

❯ litestar assets build
Using Litestar app from app:app
Starting Vite build process ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

> build
> vite build


vite v5.0.7 building for production...

✓ 0 modules transformed.

Metadata

Release files for litestar-vite 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for litestar-vite 0.2.2
File Size Uploaded
litestar_vite-0.2.2.tar.gz 193.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for litestar-vite 0.2.2
File Interpreter ABI Platform
litestar_vite-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 225.6 kB

Release files / litestar_vite-0.2.2.tar.gz

Download URL litestar_vite-0.2.2.tar.gz
Size 193.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a64b48ad6de4545b29d2497d72f2468ffebcb42e9fa8643d00d00ba621b93286
BLAKE2b-256 checksum
How to use checksums
216d65b0bfbddabc700ea1c9168c33ceea47fa2fcf207b8045283490570e1b45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / litestar_vite-0.2.2-py3-none-any.whl

Download URL litestar_vite-0.2.2-py3-none-any.whl
Size 31.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0cd498618bca0738b80af155fd66079808e94e9e7ea7c45bd72e7bad86b338d6
BLAKE2b-256 checksum
How to use checksums
63d8ced5caf4305a1671b562c7a62d03d9e0a58a636d0b2a7477162205428693
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

0.32.0

2 release files

0.31.0

2 release files

0.30.1

2 release files

0.30.0

2 release files

0.29.0

2 release files

0.28.0

2 release files

0.27.0

2 release files

0.25.0

2 release files

0.22.1

2 release files

0.22.0

2 release files

0.21.1

2 release files

0.21.0

2 release files

0.20.0

2 release files

0.18.2

2 release files

0.18.0

2 release files

0.17.0

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.1

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.9.0

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.22

2 release files

0.1.21

2 release files

0.1.20

2 release files

0.1.19

2 release files

0.1.16

2 release files

0.1.15

2 release files

0.1.14

2 release files

0.1.13

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page