djvite
v1.5.0
Published
Integrates Vite resources into a Django web site.
Readme
DjVite
Integrates Vite resources into a Django web site.
Web requests are first served through vite dev server, then either proxified to django dev server or served directly.
This simulates a nginx proxy and wsgi server.
How to use
Django side
- Add
djviteto yourINSTALLED_APPSdjango config and define your static directories.
# settings.py
INSTALLED_APPS = [
...
'djvite',
]
STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / 'static'
STATICFILES_DIRS = ['dist']- Load the
djviteplugin into your templates. - Inject any script or link from vite into your template:
{% vite hotreload %}enables vite hot module reload in dev mode{% vite '/src/main.js' %}for module{% vite '/src/style.css' %}for asset- Add any attributes to the
vitetag and it will be added to the final tags. - Specifiy multiple sources within one
vitetag, separate them with spaces.
<!-- myapp/templates/index.html -->
<html>{% load djvite %}
<heead>
<title>My title</title>
{% vite hotreload '/src/main.js' %}
</head>
...
</html>Notes:
You can use the get_nginx_config command to generate a working nginx static configuration.
Vite side
- Add
DjVitePluginto yourvite.config.jsfile:
// vite.config.js
import { defineConfig } from 'vite'
import DjVitePlugin from 'djvite'
export default defineConfig({
plugins: [
DjVitePlugin({verbose: true}),
],
})Configuration
In django settings:
DJVITEdict, with the following keys:DEV_MODE(boolean, defaultTrue)
WhenFalse, resources are resolved using thevite-manifest.jsonfile that list bundle files. This file is generated usingvite build.MODULE_EXTS(extension list, default ot['.js'])
Use this to provide other extensions to be served as module, for instance['.js', '.ts', '.jsx', '.tsx']in Typescript React application.VITE_MANIFEST_PATH(Path | str, default tovite.manifest.json)
Location to search for the Vite manifest. Used whenDEV_MODEisFalse.
In vite config plugin:
optionsobject forDjVitePlugin.verbosedefault tofalse.djangoPortdefault toDJANGO_PORTenvironment variable or8000if not defined.djangoTemplatesGlobdefault to the globbing pattern**/templates.manifestPathdefault tovite.manifest.json.
