Skip to main content

A Flask extension that integrates with Vite

Project description

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
  • modulepreload hints for JS chunks in production mode

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

Configure

The extension parses the following values from the Flask app.config object:

  • VITE_MODE: "development" | "production" - type of environment in which the Vite integration will run
  • VITE_PORT: int - port where Vite's dev server is running
  • 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

Import

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: all files imported via vite_import must be declared as entry points in your Vite config; if this isn't the case, the Vite build command will not process the file you're trying to import, so the extension will NOT work in production mode.

Minimal usage example

# app.py
app = Flask()

app.config["VITE_MODE"] = "development" if app.debug else "production"
app.config["VITE_PORT"] = 9999
app.config["VITE_OUTDIR"] = "static/dist"

vite = FlaskVite()
vite.init_app(app)

# ...
<!-- templates/base.html -->
<head>
  { vite_import("path/to/source/styles.scss") }
</head>
<body>
  <!-- ... -->
  { vite_import("path/to/source/file.tsx") }
</body>

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

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

canonicalwebteam_flask_vite-0.1.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

canonicalwebteam_flask_vite-0.1.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file canonicalwebteam_flask_vite-0.1.0.tar.gz.

File metadata

File hashes

Hashes for canonicalwebteam_flask_vite-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a6e649e824661a1af246b563b064eb546c19529c38ed319733cb09a0f2d9dc1b
MD5 d897da8adfe9f0ea809f38cfa97ceed8
BLAKE2b-256 9ee32125ece81704b8ff37a0cb82ca6c3c6a36f1c95ec003d0ff8ded99216800

See more details on using hashes here.

File details

Details for the file canonicalwebteam_flask_vite-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for canonicalwebteam_flask_vite-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 193174096cbfcd126d71e7406d3de2de4354076adb18be890778535be8f0f30f
MD5 c294d0fee12951896dc299ceda9b48c6
BLAKE2b-256 816e60de3ed33f554ad911cb373763136def8a60d60612028d88905270503a55

See more details on using hashes here.

Supported by

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