Skip to main content

Vite plugin for Litestar

Project description

Litestar Vite

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.template.config import TemplateConfig
from litestar.contrib.jinja import JinjaTemplateEngine
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")

template_config = TemplateConfig(engine=JinjaTemplateEngine(directory='templates/'))
vite = VitePlugin(config=ViteConfig())
app = Litestar(plugins=[vite], template_config=template_config, 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

litestar_vite-0.13.2.tar.gz (61.1 kB view details)

Uploaded Source

Built Distribution

litestar_vite-0.13.2-py3-none-any.whl (34.9 kB view details)

Uploaded Python 3

File details

Details for the file litestar_vite-0.13.2.tar.gz.

File metadata

  • Download URL: litestar_vite-0.13.2.tar.gz
  • Upload date:
  • Size: 61.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for litestar_vite-0.13.2.tar.gz
Algorithm Hash digest
SHA256 b7bf6b861955580f22a584a10e1cbf048b3a15989cc172908ee76ba934bc4540
MD5 8f9db321c7ee6fb4b15d61877116c71b
BLAKE2b-256 6c6f450287a0c9c47d72938fcb7bf003021a662b373d2d0aba51f7fa9be2604b

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_vite-0.13.2.tar.gz:

Publisher: publish.yml on litestar-org/litestar-vite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file litestar_vite-0.13.2-py3-none-any.whl.

File metadata

  • Download URL: litestar_vite-0.13.2-py3-none-any.whl
  • Upload date:
  • Size: 34.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for litestar_vite-0.13.2-py3-none-any.whl
Algorithm Hash digest
SHA256 726aa191ad8851c848d80902f7ca25e5c8db08369d8c5b6c20a4e4a1abd0abbe
MD5 306562cbc3b65711720909bc3e887ba9
BLAKE2b-256 5117bcc7b1c1d79c3bc61a30cf6db9323955b52e6045d14b78897efb81eb81ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_vite-0.13.2-py3-none-any.whl:

Publisher: publish.yml on litestar-org/litestar-vite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page