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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for canonicalwebteam_flask_vite-0.2.1.tar.gz
Algorithm Hash digest
SHA256 14491cfb0af5e4413ca02b94188ace485ad651f9834fcefa708f2ec2c2bc14ad
MD5 dfcedbca84102cff21dd51f43b1d0350
BLAKE2b-256 e6ef2ec28375cae80e9a9a15a8273d31f2ba0df6fab5a95d7ef021c523e63d06

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for canonicalwebteam_flask_vite-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0a99da9b297025e6f6f530875db503ea313f251951053ded788d9d41f9e8bf9c
MD5 84d81e358ea38c47b23b81bc20b8638c
BLAKE2b-256 17a5ada22ab8d53edb66b99b1da0fc4277b89ac6e9355dff4f51fdc66f6e080c

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