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.2.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.2.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for canonicalwebteam_flask_vite-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ab017e326065374a7a3676da405997c7d872b50a3d3c325f02b62f09eda3f499
MD5 e800959065500a6afbbcec2a707625ad
BLAKE2b-256 45e1a44481cf791b5723cca3757aa6d39f3bd012755aaaed94dbbc00038c3ac3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for canonicalwebteam_flask_vite-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 55406013a60a0bdd34793d780a903c0a86ed85ae1d410d80ac32dd2f6d227ad1
MD5 b5fd4feb15f5fc06f0e485b57f8122c2
BLAKE2b-256 9c9212bdc421e224dea33a7d0987c940f110589b115fff3ac87e3f08a5a56185

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