Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/litestar-org/litestar-vite
Vite Plugin for Litestar
https://github.com/litestar-org/litestar-vite
litestar litestar-api litestar-framework vite vitejs
Last synced: 3 months ago
JSON representation
Vite Plugin for Litestar
- Host: GitHub
- URL: https://github.com/litestar-org/litestar-vite
- Owner: litestar-org
- License: mit
- Created: 2023-10-04T03:39:08.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-10T16:47:29.000Z (4 months ago)
- Last Synced: 2024-10-29T20:56:34.467Z (3 months ago)
- Topics: litestar, litestar-api, litestar-framework, vite, vitejs
- Language: Python
- Homepage:
- Size: 771 KB
- Stars: 16
- Watchers: 2
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.rst
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Litestar Vite
> [!IMPORTANT]
> This plugin currently contains minimal features and is a work-in-progress## Installation
```shell
pip install litestar-vite
```## Usage
Here is a basic application that demonstrates how to use the plugin.
```python
from __future__ import annotationsfrom 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, VitePluginclass 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`:
```html
{{ vite_hmr() }} {{ vite('resources/main.ts') }}
```
### 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:
```shell
❯ 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.
```shell
❯ 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 detailsfound 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
```shell
❯ litestar assets serve
Using Litestar app from app:app
Starting Vite build process ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────> build
> vite buildvite 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
```shell
❯ litestar assets build
Using Litestar app from app:app
Starting Vite build process ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────> build
> vite buildvite v5.0.7 building for production...
✓ 0 modules transformed.
```