Canonical Webteam Flask-Vite integration
A Flask extension that integrates with Vite, enabling use of Vite's dev server and static builds with as little configuration as possible.
Features
- easy to configure
- simple API
- supports custom Vite configurations
- supports multiple JS entry points
- supports all Vite-compatible JS frameworks
- supports all Vite-compatible stylesheet languages
- hot reloading in development mode
modulepreloadhints for JS chunks in production mode- injects
nonceattributes intoscriptandlinktags automatically (ifflask.request.CSP_NONCEis set)
How to use the extension
Install
To install this extension as a requirement in your project, you can use PIP:
pip install canonicalwebteam.flask-vite
Update your Vite configuration
Set build.manifest to true in your Vite config.
Configure
The extension reads the following values from the Flask app.config object:
VITE_MODE: "development" | "production"- type of environment in which the Vite integration will run. Defaults to"production".VITE_PORT: int- port where Vite's dev server is running. Defaults to5173.VITE_OUTDIR: str- file system path where the Vite output is expected; the path can be absolute or relative to the Flask app's root directory. Defaults to"static/dist".VITE_MANIFEST_PATH: str: Path to the Vite manifest file, relative toVITE_OUTDIR. Defaults to.vite/manifest.json(the standard for Vite v5, v6, and v7). For vite v4 and below the path ismanifest.jsonVITE_REACT: bool- only needed for React projects; set toTrueto inject React HMR dev tools in dev mode. Defaults toFalse.
Note: the extension does NOT manage the state of the Vite dev server nor produce the static builds, you are responsible for running the appropriate Vite commands before initializing the extension.
Initialize
from canonicalwebteam.flask_vite import FlaskVite
vite = FlaskVite()
vite.init_app(app)
Import resources
The extension adds a new template global function named vite_import. To include a script or stylesheet, simply invoke this template function passing the path to the file you want to import, relative to the app's root directory.
Example
To import a script whose path is < flask app directory >/js/app/main.ts:
{ vite_import("js/app/main.ts") }
Note: when importing a file via
vite_import, make sure it's also declared as an entry point in your Vite config (underbuild.rollupOptions.input); if this isn't the case, the import will break in production mode. This is because Vite's build won't contain the file you're trying to import, sovite_importwill throw aManifestContentExceptionwhen attempting to resolve the filesystem path.
Minimal usage example
# app.py
# ...
app.config["VITE_MODE"] = "development" if app.debug else "production"
FlaskVite(app)
#...
<!-- templates/index.html -->
<head>
{ vite_import("path/to/source/styles.scss") }
</head>
<body>
{ vite_import("path/to/source/file.tsx") }
</body>
For a more fleshed-out example check the example directory.
Development
The package leverages poetry for dependency management.
To set up the virtual env and install dependencies, run:
poetry install
Testing
Unit tests can be run using:
poetry run python3 -m unittest discover tests
Metadata
Release files for canonicalwebteam.flask-vite 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| canonicalwebteam_flask_vite-0.6.0.tar.gz | 8.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| canonicalwebteam_flask_vite-0.6.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.5 kB
Release files / canonicalwebteam_flask_vite-0.6.0.tar.gz
| Download URL | canonicalwebteam_flask_vite-0.6.0.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
383b0e790419473dbab13e1b9306788b8e8af6f917501f52763943a634764144
|
|
BLAKE2b-256 checksum How to use checksums |
67d6431763938016f61ad9ff81230c3fdae2e76f08e52f9116fac1b3f9e86aa7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / canonicalwebteam_flask_vite-0.6.0-py3-none-any.whl
| Download URL | canonicalwebteam_flask_vite-0.6.0-py3-none-any.whl |
|---|---|
| Size | 12.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ed675723f315bff0bcac0721f3aae0280bd2380c6cc3cf052831f7935980cc76
|
|
BLAKE2b-256 checksum How to use checksums |
2b0a579349b53b2dc5a4a1040bb931a404ce3e386d7d7a45485e384e2db4f090
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|