django-vite-kit 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_vite_kit-0.2.0/.gitignore +23 -0
- django_vite_kit-0.2.0/CHANGELOG.md +21 -0
- django_vite_kit-0.2.0/LICENSE +21 -0
- django_vite_kit-0.2.0/PKG-INFO +263 -0
- django_vite_kit-0.2.0/README.md +225 -0
- django_vite_kit-0.2.0/pyproject.toml +74 -0
- django_vite_kit-0.2.0/src/djvite/__init__.py +29 -0
- django_vite_kit-0.2.0/src/djvite/apps.py +31 -0
- django_vite_kit-0.2.0/src/djvite/checks.py +71 -0
- django_vite_kit-0.2.0/src/djvite/conf.py +114 -0
- django_vite_kit-0.2.0/src/djvite/management/__init__.py +0 -0
- django_vite_kit-0.2.0/src/djvite/management/commands/__init__.py +0 -0
- django_vite_kit-0.2.0/src/djvite/management/commands/vite.py +385 -0
- django_vite_kit-0.2.0/src/djvite/manifest.py +166 -0
- django_vite_kit-0.2.0/src/djvite/npm.py +152 -0
- django_vite_kit-0.2.0/src/djvite/presets.py +111 -0
- django_vite_kit-0.2.0/src/djvite/py.typed +0 -0
- django_vite_kit-0.2.0/src/djvite/registry.py +239 -0
- django_vite_kit-0.2.0/src/djvite/scaffolding.py +268 -0
- django_vite_kit-0.2.0/src/djvite/templates/djvite/tags/vite_assets.html +9 -0
- django_vite_kit-0.2.0/src/djvite/templates/djvite/tags/vite_entry.html +8 -0
- django_vite_kit-0.2.0/src/djvite/templatetags/__init__.py +0 -0
- django_vite_kit-0.2.0/src/djvite/templatetags/djvite.py +155 -0
- django_vite_kit-0.2.0/tests/__init__.py +0 -0
- django_vite_kit-0.2.0/tests/conftest.py +27 -0
- django_vite_kit-0.2.0/tests/test_checks.py +41 -0
- django_vite_kit-0.2.0/tests/test_command.py +134 -0
- django_vite_kit-0.2.0/tests/test_conf.py +50 -0
- django_vite_kit-0.2.0/tests/test_manifest.py +91 -0
- django_vite_kit-0.2.0/tests/test_npm.py +76 -0
- django_vite_kit-0.2.0/tests/test_registry_and_presets.py +75 -0
- django_vite_kit-0.2.0/tests/test_scaffolding.py +66 -0
- django_vite_kit-0.2.0/tests/test_templatetags.py +173 -0
- django_vite_kit-0.2.0/tests/test_typescript.py +97 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.egg-info/
|
|
6
|
+
dist/
|
|
7
|
+
build/
|
|
8
|
+
.eggs/
|
|
9
|
+
|
|
10
|
+
# Virtual environments
|
|
11
|
+
.venv/
|
|
12
|
+
venv/
|
|
13
|
+
|
|
14
|
+
# Testing / tooling
|
|
15
|
+
.pytest_cache/
|
|
16
|
+
.ruff_cache/
|
|
17
|
+
.coverage
|
|
18
|
+
htmlcov/
|
|
19
|
+
|
|
20
|
+
# OS / IDE
|
|
21
|
+
.DS_Store
|
|
22
|
+
.idea/
|
|
23
|
+
.vscode/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## [0.2.0] - 2026-12-06
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- Django system checks (`djvite.W001`, `djvite.E001`, `djvite.W003`) including `--deploy` validation for `DJVITE_DEV_MODE` and `manifest.json`.
|
|
9
|
+
- Automatic `<link rel="modulepreload">` tags for transitive Vite JS chunks in production builds.
|
|
10
|
+
- Content-Security-Policy (CSP) `nonce` support across `{% vite_assets %}`, `{% enable_vite %}`, `{% vite_entry %}`, and `{% vite_hmr_client %}` (auto-detects `request.csp_nonce` or accepts `nonce=...`).
|
|
11
|
+
- PEP 561 `py.typed` marker for downstream type checking.
|
|
12
|
+
- GitHub Actions CI and PyPI Trusted Publishing release workflows.
|
|
13
|
+
- First-class TypeScript support (`manage.py vite init --ts`, `manage.py vite typecheck`, `src/djvite.d.ts`).
|
|
14
|
+
- Built-in JS presets (`alpine`, `htmx`, `sortable`, `chartjs`, `lucide`) and `vite add`/`remove` plugin registry.
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
- CSS-only entrypoints in Vite `manifest.json` now render as `<link rel="stylesheet">` instead of `<script>` in production.
|
|
18
|
+
- Thread-safe manifest caching with automatic invalidation on Django `setting_changed` signals and top-level JSON object validation.
|
|
19
|
+
- Safe `format_html` rendering in `vite_hmr_client` and strict validation for JS/CSS module specifiers in `registry.py`.
|
|
20
|
+
- Full CDN `STATIC_URL` support (`https://...`) and regex-escaped cookie names in scaffolded `getCsrfToken`.
|
|
21
|
+
- Scaffolded `vite.config.js` now uses conditional `base` (`/` in dev, `STATIC_URL + prefix` in build) and `server.cors: true` so Vite dev server serves entrypoints cleanly to Django.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 djvite contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: django-vite-kit
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Django connector for Tailwind CSS v4 and modern JavaScript powered by Vite
|
|
5
|
+
Project-URL: Homepage, https://github.com/bbal10/django-vite-kit
|
|
6
|
+
Project-URL: Repository, https://github.com/bbal10/django-vite-kit
|
|
7
|
+
Project-URL: Issues, https://github.com/bbal10/django-vite-kit/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/bbal10/django-vite-kit/blob/main/CHANGELOG.md
|
|
9
|
+
Author: Ta3lem Team
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: django,frontend,javascript,tailwind,tailwindcss,vite
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Environment :: Web Environment
|
|
15
|
+
Classifier: Framework :: Django
|
|
16
|
+
Classifier: Framework :: Django :: 4.2
|
|
17
|
+
Classifier: Framework :: Django :: 5.0
|
|
18
|
+
Classifier: Framework :: Django :: 5.1
|
|
19
|
+
Classifier: Framework :: Django :: 5.2
|
|
20
|
+
Classifier: Framework :: Django :: 6.0
|
|
21
|
+
Classifier: Intended Audience :: Developers
|
|
22
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
23
|
+
Classifier: Operating System :: OS Independent
|
|
24
|
+
Classifier: Programming Language :: Python :: 3
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
28
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
29
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
30
|
+
Classifier: Typing :: Typed
|
|
31
|
+
Requires-Python: >=3.11
|
|
32
|
+
Requires-Dist: django>=4.2
|
|
33
|
+
Provides-Extra: dev
|
|
34
|
+
Requires-Dist: pytest-django>=4.8; extra == 'dev'
|
|
35
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
36
|
+
Requires-Dist: ruff>=0.8.0; extra == 'dev'
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
|
|
39
|
+
# djvite
|
|
40
|
+
|
|
41
|
+
A Django package that makes it effortless to install, configure, and use **JavaScript libraries** and **Tailwind CSS v4** in Django templates — powered by **Vite**.
|
|
42
|
+
|
|
43
|
+
## Why djvite?
|
|
44
|
+
|
|
45
|
+
Using npm JavaScript libraries in Django usually means fighting bundlers, wiring CSRF tokens, exposing globals, or writing unsafe inline `<script>` tags. `djvite` handles the bridge cleanly:
|
|
46
|
+
|
|
47
|
+
- **One-command JS library setup**: `manage.py vite add chart.js --expose Chart` or built-in presets (`manage.py vite preset alpine htmx`)
|
|
48
|
+
- **Zero-config Vite + Tailwind CSS v4 scaffolding**: `manage.py vite init`
|
|
49
|
+
- **Page-specific JS entrypoints**: `{% vite_entry 'src/dashboard.js' %}` so heavy libraries only load where needed
|
|
50
|
+
- **CSP-safe Django-to-JS data bridge**: `{% vite_data context_dict "chart-data" %}` + `getDjangoData("chart-data")`
|
|
51
|
+
- **Seamless Dev HMR & Production Manifests**: automatic hashed JS + transitive CSS resolution
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install django-vite-kit
|
|
57
|
+
# or with uv
|
|
58
|
+
uv add django-vite-kit
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Add `djvite` to `INSTALLED_APPS`:
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
INSTALLED_APPS = [
|
|
65
|
+
# ...
|
|
66
|
+
"djvite",
|
|
67
|
+
]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Quick Start
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
# 1. Scaffold Vite + Tailwind CSS v4 + djvite helpers (add --ts for TypeScript)
|
|
74
|
+
python manage.py vite init
|
|
75
|
+
# or for TypeScript:
|
|
76
|
+
python manage.py vite init --ts
|
|
77
|
+
|
|
78
|
+
# 2. Install base npm dependencies
|
|
79
|
+
python manage.py vite install
|
|
80
|
+
|
|
81
|
+
# 3. (Optional) Add popular JS libraries with one command
|
|
82
|
+
python manage.py vite preset alpine htmx
|
|
83
|
+
python manage.py vite add flatpickr --expose flatpickr --css flatpickr/dist/flatpickr.min.css
|
|
84
|
+
|
|
85
|
+
# 4. Run Vite dev server
|
|
86
|
+
python manage.py vite dev
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Include assets in your `base.html`:
|
|
90
|
+
|
|
91
|
+
```django
|
|
92
|
+
{% load djvite %}
|
|
93
|
+
<!DOCTYPE html>
|
|
94
|
+
<html>
|
|
95
|
+
<head>
|
|
96
|
+
{% vite_assets %}
|
|
97
|
+
</head>
|
|
98
|
+
<body>
|
|
99
|
+
{% block content %}{% endblock %}
|
|
100
|
+
{% block extra_js %}{% endblock %}
|
|
101
|
+
</body>
|
|
102
|
+
</html>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Using JavaScript Libraries Easily
|
|
106
|
+
|
|
107
|
+
### 1. Built-in Presets (`vite preset`)
|
|
108
|
+
|
|
109
|
+
List available presets:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
python manage.py vite preset --list
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Enable one or more presets (`alpine`, `htmx`, `sortable`, `chartjs`, `lucide`):
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
python manage.py vite preset alpine htmx lucide
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Each preset installs the required npm packages and generates ready-to-use wiring in `src/djvite.plugins.js` (e.g., automatic `X-CSRFToken` header for HTMX, `Alpine.start()` + plugins for Alpine.js, auto-render on HTMX swaps for Lucide).
|
|
122
|
+
|
|
123
|
+
### 2. Add or Remove Any npm Library (`vite add` / `vite remove`)
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
# Install and expose on window.Chart
|
|
127
|
+
python manage.py vite add chart.js --expose Chart --import-path chart.js/auto
|
|
128
|
+
|
|
129
|
+
# Install with accompanying CSS
|
|
130
|
+
python manage.py vite add flatpickr --expose flatpickr --css flatpickr/dist/flatpickr.min.css
|
|
131
|
+
|
|
132
|
+
# Install for direct ES module import only
|
|
133
|
+
python manage.py vite add dayjs
|
|
134
|
+
|
|
135
|
+
# Remove package (and unregister from djvite.plugins.js)
|
|
136
|
+
python manage.py vite remove flatpickr
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### 3. Page-Specific Entrypoints (`{% vite_entry %}`)
|
|
140
|
+
|
|
141
|
+
Keep your global bundle small by loading heavy libraries only on the pages that need them:
|
|
142
|
+
|
|
143
|
+
1. Add the entry in `vite.config.js`:
|
|
144
|
+
```js
|
|
145
|
+
rollupOptions: {
|
|
146
|
+
input: {
|
|
147
|
+
main: 'index.html',
|
|
148
|
+
dashboard: 'src/dashboard.js',
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
2. Load it in a specific Django template:
|
|
153
|
+
```django
|
|
154
|
+
{% extends "base.html" %}
|
|
155
|
+
{% load djvite %}
|
|
156
|
+
|
|
157
|
+
{% block extra_js %}
|
|
158
|
+
{% vite_entry 'src/dashboard.js' %}
|
|
159
|
+
{% endblock %}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### 4. Pass Django Data Safely to JavaScript (`{% vite_data %}`)
|
|
163
|
+
|
|
164
|
+
No inline `<script>` tags needed (100% Content-Security-Policy friendly):
|
|
165
|
+
|
|
166
|
+
```django
|
|
167
|
+
{% load djvite %}
|
|
168
|
+
{% vite_data chart_series "sales-chart-data" %}
|
|
169
|
+
<canvas id="sales-chart"></canvas>
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Read it in your Vite JS module:
|
|
173
|
+
|
|
174
|
+
```js
|
|
175
|
+
import { getDjangoData, getCsrfToken } from './djvite.js'
|
|
176
|
+
|
|
177
|
+
const series = getDjangoData('sales-chart-data', [])
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### 5. First-Class TypeScript Support
|
|
181
|
+
|
|
182
|
+
Initialize with `--ts` (or `--typescript`):
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
python manage.py vite init --ts
|
|
186
|
+
python manage.py vite install
|
|
187
|
+
python manage.py vite typecheck
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
When `tsconfig.json` or `src/index.ts` is present:
|
|
191
|
+
- `DJVITE_JS_ENTRYPOINT` automatically defaults to `src/index.ts`.
|
|
192
|
+
- `vite add` and `vite preset` write to `src/djvite.plugins.ts` and auto-generate `src/djvite.d.ts` with typed `Window` globals (`window.Alpine`, `window.htmx`, `window.Chart`, `window.djvite`, etc.).
|
|
193
|
+
- `getDjangoData<MyType>("element-id")` in `src/djvite.ts` supports full TypeScript generics.
|
|
194
|
+
|
|
195
|
+
## Configuration
|
|
196
|
+
|
|
197
|
+
All settings are optional. Every `DJVITE_*` setting falls back to its legacy `VITE_*` / `NPM_BIN_PATH` counterpart if not set.
|
|
198
|
+
|
|
199
|
+
| Setting | Fallback | Default | Description |
|
|
200
|
+
| --- | --- | --- | --- |
|
|
201
|
+
| `DJVITE_DEV_MODE` | `VITE_DEV_MODE` | `settings.DEBUG` | Use Vite dev server when `True`, or `manifest.json` when `False`. |
|
|
202
|
+
| `DJVITE_DEV_SERVER_URL` | `VITE_DEV_SERVER_URL` | `"http://localhost:5173"` | Base URL of the running Vite dev server. |
|
|
203
|
+
| `DJVITE_FRONTEND_DIR` | `VITE_FRONTEND_DIR` | `BASE_DIR / "frontend"` | Directory containing `package.json` and `vite.config.js`. |
|
|
204
|
+
| `DJVITE_STATIC_URL_PREFIX` | `VITE_STATIC_URL_PREFIX` | `"dist"` | Prefix under Django staticfiles for Vite build output. |
|
|
205
|
+
| `DJVITE_MANIFEST_PATH` | `VITE_MANIFEST_PATH` | `BASE_DIR / "static" / "dist" / ".vite" / "manifest.json"` | Path to the Vite production `manifest.json`. |
|
|
206
|
+
| `DJVITE_JS_ENTRYPOINT` | `VITE_JS_ENTRYPOINT` | `"{dev_server_url}/src/index.js"` | JS module URL used in dev mode. |
|
|
207
|
+
| `DJVITE_CSS_ENTRYPOINT` | `VITE_CSS_ENTRYPOINT` | `"{dev_server_url}/src/style.css"` | CSS URL used in dev mode. |
|
|
208
|
+
| `DJVITE_NPM_BIN_PATH` | `NPM_BIN_PATH` | `"npm"` | Path to the `npm` executable. |
|
|
209
|
+
|
|
210
|
+
## Management Commands
|
|
211
|
+
|
|
212
|
+
- `python manage.py vite init [--ts] [--force]` — Scaffold Vite + Tailwind CSS v4 + `djvite.{js,ts}` helpers.
|
|
213
|
+
- `python manage.py vite install` — Run `npm install`.
|
|
214
|
+
- `python manage.py vite build` — Run `npm run build`.
|
|
215
|
+
- `python manage.py vite dev [--use-honcho]` — Start Vite dev server (or Honcho).
|
|
216
|
+
- `python manage.py vite typecheck` — Run `npm run typecheck` (`tsc --noEmit`).
|
|
217
|
+
- `python manage.py vite add <pkgs...> [--dev] [--expose NAME] [--css PATH] [--namespace] [--import-path PATH]` — Install & optionally auto-register npm libraries.
|
|
218
|
+
- `python manage.py vite remove <pkgs...>` — Uninstall npm libraries & clean up registry.
|
|
219
|
+
- `python manage.py vite preset [names...] [--list]` — List or enable built-in JS presets.
|
|
220
|
+
- `python manage.py vite npm <args...>` — Run any raw `npm` command.
|
|
221
|
+
|
|
222
|
+
## Production Deployment
|
|
223
|
+
|
|
224
|
+
1. **Build frontend assets and collect static files** in your CI/Docker pipeline:
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
python manage.py vite install
|
|
228
|
+
python manage.py vite build
|
|
229
|
+
python manage.py collectstatic --noinput
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
2. **Disable dev mode in production** (`DJVITE_DEV_MODE` defaults to `settings.DEBUG`):
|
|
233
|
+
|
|
234
|
+
```python
|
|
235
|
+
DEBUG = False
|
|
236
|
+
DJVITE_DEV_MODE = False
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
In production, `djvite` caches `manifest.json` in memory (thread-safe), emits `<link rel="stylesheet">` for CSS (including transitive imports), `<link rel="modulepreload">` for shared JS chunks, and `<script type="module">` for your entrypoint.
|
|
240
|
+
|
|
241
|
+
3. **Run Django's deployment checks** before releasing:
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
python manage.py check --deploy
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
`djvite` registers built-in checks:
|
|
248
|
+
- `djvite.W001` — warns if `DJVITE_DEV_MODE` is still `True` during `--deploy`.
|
|
249
|
+
- `djvite.E001` — errors if `manifest.json` is missing or invalid when `DJVITE_DEV_MODE=False`.
|
|
250
|
+
- `djvite.W003` — warns if `STATIC_URL` is not configured.
|
|
251
|
+
|
|
252
|
+
4. **Content Security Policy (CSP) Nonce Support**:
|
|
253
|
+
|
|
254
|
+
If you use `django-csp` (or any middleware that sets `request.csp_nonce`), `djvite` automatically attaches `nonce="..."` to all rendered `<script>` and `<link>` tags. You can also pass it explicitly:
|
|
255
|
+
|
|
256
|
+
```django
|
|
257
|
+
{% vite_assets nonce=request.csp_nonce %}
|
|
258
|
+
{% vite_entry 'src/dashboard.js' nonce=request.csp_nonce %}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## License
|
|
262
|
+
|
|
263
|
+
MIT
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# djvite
|
|
2
|
+
|
|
3
|
+
A Django package that makes it effortless to install, configure, and use **JavaScript libraries** and **Tailwind CSS v4** in Django templates — powered by **Vite**.
|
|
4
|
+
|
|
5
|
+
## Why djvite?
|
|
6
|
+
|
|
7
|
+
Using npm JavaScript libraries in Django usually means fighting bundlers, wiring CSRF tokens, exposing globals, or writing unsafe inline `<script>` tags. `djvite` handles the bridge cleanly:
|
|
8
|
+
|
|
9
|
+
- **One-command JS library setup**: `manage.py vite add chart.js --expose Chart` or built-in presets (`manage.py vite preset alpine htmx`)
|
|
10
|
+
- **Zero-config Vite + Tailwind CSS v4 scaffolding**: `manage.py vite init`
|
|
11
|
+
- **Page-specific JS entrypoints**: `{% vite_entry 'src/dashboard.js' %}` so heavy libraries only load where needed
|
|
12
|
+
- **CSP-safe Django-to-JS data bridge**: `{% vite_data context_dict "chart-data" %}` + `getDjangoData("chart-data")`
|
|
13
|
+
- **Seamless Dev HMR & Production Manifests**: automatic hashed JS + transitive CSS resolution
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install django-vite-kit
|
|
19
|
+
# or with uv
|
|
20
|
+
uv add django-vite-kit
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Add `djvite` to `INSTALLED_APPS`:
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
INSTALLED_APPS = [
|
|
27
|
+
# ...
|
|
28
|
+
"djvite",
|
|
29
|
+
]
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# 1. Scaffold Vite + Tailwind CSS v4 + djvite helpers (add --ts for TypeScript)
|
|
36
|
+
python manage.py vite init
|
|
37
|
+
# or for TypeScript:
|
|
38
|
+
python manage.py vite init --ts
|
|
39
|
+
|
|
40
|
+
# 2. Install base npm dependencies
|
|
41
|
+
python manage.py vite install
|
|
42
|
+
|
|
43
|
+
# 3. (Optional) Add popular JS libraries with one command
|
|
44
|
+
python manage.py vite preset alpine htmx
|
|
45
|
+
python manage.py vite add flatpickr --expose flatpickr --css flatpickr/dist/flatpickr.min.css
|
|
46
|
+
|
|
47
|
+
# 4. Run Vite dev server
|
|
48
|
+
python manage.py vite dev
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Include assets in your `base.html`:
|
|
52
|
+
|
|
53
|
+
```django
|
|
54
|
+
{% load djvite %}
|
|
55
|
+
<!DOCTYPE html>
|
|
56
|
+
<html>
|
|
57
|
+
<head>
|
|
58
|
+
{% vite_assets %}
|
|
59
|
+
</head>
|
|
60
|
+
<body>
|
|
61
|
+
{% block content %}{% endblock %}
|
|
62
|
+
{% block extra_js %}{% endblock %}
|
|
63
|
+
</body>
|
|
64
|
+
</html>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Using JavaScript Libraries Easily
|
|
68
|
+
|
|
69
|
+
### 1. Built-in Presets (`vite preset`)
|
|
70
|
+
|
|
71
|
+
List available presets:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
python manage.py vite preset --list
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Enable one or more presets (`alpine`, `htmx`, `sortable`, `chartjs`, `lucide`):
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
python manage.py vite preset alpine htmx lucide
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Each preset installs the required npm packages and generates ready-to-use wiring in `src/djvite.plugins.js` (e.g., automatic `X-CSRFToken` header for HTMX, `Alpine.start()` + plugins for Alpine.js, auto-render on HTMX swaps for Lucide).
|
|
84
|
+
|
|
85
|
+
### 2. Add or Remove Any npm Library (`vite add` / `vite remove`)
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
# Install and expose on window.Chart
|
|
89
|
+
python manage.py vite add chart.js --expose Chart --import-path chart.js/auto
|
|
90
|
+
|
|
91
|
+
# Install with accompanying CSS
|
|
92
|
+
python manage.py vite add flatpickr --expose flatpickr --css flatpickr/dist/flatpickr.min.css
|
|
93
|
+
|
|
94
|
+
# Install for direct ES module import only
|
|
95
|
+
python manage.py vite add dayjs
|
|
96
|
+
|
|
97
|
+
# Remove package (and unregister from djvite.plugins.js)
|
|
98
|
+
python manage.py vite remove flatpickr
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 3. Page-Specific Entrypoints (`{% vite_entry %}`)
|
|
102
|
+
|
|
103
|
+
Keep your global bundle small by loading heavy libraries only on the pages that need them:
|
|
104
|
+
|
|
105
|
+
1. Add the entry in `vite.config.js`:
|
|
106
|
+
```js
|
|
107
|
+
rollupOptions: {
|
|
108
|
+
input: {
|
|
109
|
+
main: 'index.html',
|
|
110
|
+
dashboard: 'src/dashboard.js',
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
2. Load it in a specific Django template:
|
|
115
|
+
```django
|
|
116
|
+
{% extends "base.html" %}
|
|
117
|
+
{% load djvite %}
|
|
118
|
+
|
|
119
|
+
{% block extra_js %}
|
|
120
|
+
{% vite_entry 'src/dashboard.js' %}
|
|
121
|
+
{% endblock %}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 4. Pass Django Data Safely to JavaScript (`{% vite_data %}`)
|
|
125
|
+
|
|
126
|
+
No inline `<script>` tags needed (100% Content-Security-Policy friendly):
|
|
127
|
+
|
|
128
|
+
```django
|
|
129
|
+
{% load djvite %}
|
|
130
|
+
{% vite_data chart_series "sales-chart-data" %}
|
|
131
|
+
<canvas id="sales-chart"></canvas>
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Read it in your Vite JS module:
|
|
135
|
+
|
|
136
|
+
```js
|
|
137
|
+
import { getDjangoData, getCsrfToken } from './djvite.js'
|
|
138
|
+
|
|
139
|
+
const series = getDjangoData('sales-chart-data', [])
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### 5. First-Class TypeScript Support
|
|
143
|
+
|
|
144
|
+
Initialize with `--ts` (or `--typescript`):
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
python manage.py vite init --ts
|
|
148
|
+
python manage.py vite install
|
|
149
|
+
python manage.py vite typecheck
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
When `tsconfig.json` or `src/index.ts` is present:
|
|
153
|
+
- `DJVITE_JS_ENTRYPOINT` automatically defaults to `src/index.ts`.
|
|
154
|
+
- `vite add` and `vite preset` write to `src/djvite.plugins.ts` and auto-generate `src/djvite.d.ts` with typed `Window` globals (`window.Alpine`, `window.htmx`, `window.Chart`, `window.djvite`, etc.).
|
|
155
|
+
- `getDjangoData<MyType>("element-id")` in `src/djvite.ts` supports full TypeScript generics.
|
|
156
|
+
|
|
157
|
+
## Configuration
|
|
158
|
+
|
|
159
|
+
All settings are optional. Every `DJVITE_*` setting falls back to its legacy `VITE_*` / `NPM_BIN_PATH` counterpart if not set.
|
|
160
|
+
|
|
161
|
+
| Setting | Fallback | Default | Description |
|
|
162
|
+
| --- | --- | --- | --- |
|
|
163
|
+
| `DJVITE_DEV_MODE` | `VITE_DEV_MODE` | `settings.DEBUG` | Use Vite dev server when `True`, or `manifest.json` when `False`. |
|
|
164
|
+
| `DJVITE_DEV_SERVER_URL` | `VITE_DEV_SERVER_URL` | `"http://localhost:5173"` | Base URL of the running Vite dev server. |
|
|
165
|
+
| `DJVITE_FRONTEND_DIR` | `VITE_FRONTEND_DIR` | `BASE_DIR / "frontend"` | Directory containing `package.json` and `vite.config.js`. |
|
|
166
|
+
| `DJVITE_STATIC_URL_PREFIX` | `VITE_STATIC_URL_PREFIX` | `"dist"` | Prefix under Django staticfiles for Vite build output. |
|
|
167
|
+
| `DJVITE_MANIFEST_PATH` | `VITE_MANIFEST_PATH` | `BASE_DIR / "static" / "dist" / ".vite" / "manifest.json"` | Path to the Vite production `manifest.json`. |
|
|
168
|
+
| `DJVITE_JS_ENTRYPOINT` | `VITE_JS_ENTRYPOINT` | `"{dev_server_url}/src/index.js"` | JS module URL used in dev mode. |
|
|
169
|
+
| `DJVITE_CSS_ENTRYPOINT` | `VITE_CSS_ENTRYPOINT` | `"{dev_server_url}/src/style.css"` | CSS URL used in dev mode. |
|
|
170
|
+
| `DJVITE_NPM_BIN_PATH` | `NPM_BIN_PATH` | `"npm"` | Path to the `npm` executable. |
|
|
171
|
+
|
|
172
|
+
## Management Commands
|
|
173
|
+
|
|
174
|
+
- `python manage.py vite init [--ts] [--force]` — Scaffold Vite + Tailwind CSS v4 + `djvite.{js,ts}` helpers.
|
|
175
|
+
- `python manage.py vite install` — Run `npm install`.
|
|
176
|
+
- `python manage.py vite build` — Run `npm run build`.
|
|
177
|
+
- `python manage.py vite dev [--use-honcho]` — Start Vite dev server (or Honcho).
|
|
178
|
+
- `python manage.py vite typecheck` — Run `npm run typecheck` (`tsc --noEmit`).
|
|
179
|
+
- `python manage.py vite add <pkgs...> [--dev] [--expose NAME] [--css PATH] [--namespace] [--import-path PATH]` — Install & optionally auto-register npm libraries.
|
|
180
|
+
- `python manage.py vite remove <pkgs...>` — Uninstall npm libraries & clean up registry.
|
|
181
|
+
- `python manage.py vite preset [names...] [--list]` — List or enable built-in JS presets.
|
|
182
|
+
- `python manage.py vite npm <args...>` — Run any raw `npm` command.
|
|
183
|
+
|
|
184
|
+
## Production Deployment
|
|
185
|
+
|
|
186
|
+
1. **Build frontend assets and collect static files** in your CI/Docker pipeline:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
python manage.py vite install
|
|
190
|
+
python manage.py vite build
|
|
191
|
+
python manage.py collectstatic --noinput
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
2. **Disable dev mode in production** (`DJVITE_DEV_MODE` defaults to `settings.DEBUG`):
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
DEBUG = False
|
|
198
|
+
DJVITE_DEV_MODE = False
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
In production, `djvite` caches `manifest.json` in memory (thread-safe), emits `<link rel="stylesheet">` for CSS (including transitive imports), `<link rel="modulepreload">` for shared JS chunks, and `<script type="module">` for your entrypoint.
|
|
202
|
+
|
|
203
|
+
3. **Run Django's deployment checks** before releasing:
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
python manage.py check --deploy
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
`djvite` registers built-in checks:
|
|
210
|
+
- `djvite.W001` — warns if `DJVITE_DEV_MODE` is still `True` during `--deploy`.
|
|
211
|
+
- `djvite.E001` — errors if `manifest.json` is missing or invalid when `DJVITE_DEV_MODE=False`.
|
|
212
|
+
- `djvite.W003` — warns if `STATIC_URL` is not configured.
|
|
213
|
+
|
|
214
|
+
4. **Content Security Policy (CSP) Nonce Support**:
|
|
215
|
+
|
|
216
|
+
If you use `django-csp` (or any middleware that sets `request.csp_nonce`), `djvite` automatically attaches `nonce="..."` to all rendered `<script>` and `<link>` tags. You can also pass it explicitly:
|
|
217
|
+
|
|
218
|
+
```django
|
|
219
|
+
{% vite_assets nonce=request.csp_nonce %}
|
|
220
|
+
{% vite_entry 'src/dashboard.js' nonce=request.csp_nonce %}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## License
|
|
224
|
+
|
|
225
|
+
MIT
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25.0"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "django-vite-kit"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Django connector for Tailwind CSS v4 and modern JavaScript powered by Vite"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Ta3lem Team" },
|
|
14
|
+
]
|
|
15
|
+
keywords = ["django", "vite", "tailwind", "tailwindcss", "javascript", "frontend"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 5 - Production/Stable",
|
|
18
|
+
"Environment :: Web Environment",
|
|
19
|
+
"Framework :: Django",
|
|
20
|
+
"Framework :: Django :: 4.2",
|
|
21
|
+
"Framework :: Django :: 5.0",
|
|
22
|
+
"Framework :: Django :: 5.1",
|
|
23
|
+
"Framework :: Django :: 5.2",
|
|
24
|
+
"Framework :: Django :: 6.0",
|
|
25
|
+
"Intended Audience :: Developers",
|
|
26
|
+
"License :: OSI Approved :: MIT License",
|
|
27
|
+
"Operating System :: OS Independent",
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"Programming Language :: Python :: 3.11",
|
|
30
|
+
"Programming Language :: Python :: 3.12",
|
|
31
|
+
"Programming Language :: Python :: 3.13",
|
|
32
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
33
|
+
"Topic :: Software Development :: Build Tools",
|
|
34
|
+
"Typing :: Typed",
|
|
35
|
+
]
|
|
36
|
+
dependencies = [
|
|
37
|
+
"Django>=4.2",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Homepage = "https://github.com/bbal10/django-vite-kit"
|
|
42
|
+
Repository = "https://github.com/bbal10/django-vite-kit"
|
|
43
|
+
Issues = "https://github.com/bbal10/django-vite-kit/issues"
|
|
44
|
+
Changelog = "https://github.com/bbal10/django-vite-kit/blob/main/CHANGELOG.md"
|
|
45
|
+
|
|
46
|
+
[project.optional-dependencies]
|
|
47
|
+
dev = [
|
|
48
|
+
"pytest>=8.0",
|
|
49
|
+
"pytest-django>=4.8",
|
|
50
|
+
"ruff>=0.8.0",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
[tool.hatch.build.targets.wheel]
|
|
54
|
+
packages = ["src/djvite"]
|
|
55
|
+
|
|
56
|
+
[tool.hatch.build.targets.sdist]
|
|
57
|
+
include = [
|
|
58
|
+
"src/djvite",
|
|
59
|
+
"tests",
|
|
60
|
+
"README.md",
|
|
61
|
+
"CHANGELOG.md",
|
|
62
|
+
"LICENSE",
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
[tool.pytest.ini_options]
|
|
66
|
+
testpaths = ["tests"]
|
|
67
|
+
pythonpath = ["src"]
|
|
68
|
+
|
|
69
|
+
[tool.ruff]
|
|
70
|
+
line-length = 100
|
|
71
|
+
target-version = "py311"
|
|
72
|
+
|
|
73
|
+
[tool.ruff.lint]
|
|
74
|
+
select = ["E4", "E7", "E9", "F", "I", "UP", "B", "SIM"]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""djvite — Django connector for Tailwind CSS v4 and modern JavaScript powered by Vite."""
|
|
2
|
+
|
|
3
|
+
from djvite.conf import get_config, is_typescript_project
|
|
4
|
+
from djvite.manifest import ViteManifestError, clear_manifest_cache, get_manifest, resolve_entry
|
|
5
|
+
from djvite.npm import NpmManager, NpmPackageSpec, parse_npm_package_spec
|
|
6
|
+
from djvite.presets import PRESETS, PresetDefinition, get_preset, list_presets
|
|
7
|
+
from djvite.registry import register_package, register_preset, unregister_item
|
|
8
|
+
|
|
9
|
+
__version__ = "0.2.0"
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"PRESETS",
|
|
13
|
+
"NpmManager",
|
|
14
|
+
"NpmPackageSpec",
|
|
15
|
+
"PresetDefinition",
|
|
16
|
+
"ViteManifestError",
|
|
17
|
+
"__version__",
|
|
18
|
+
"clear_manifest_cache",
|
|
19
|
+
"get_config",
|
|
20
|
+
"get_manifest",
|
|
21
|
+
"get_preset",
|
|
22
|
+
"is_typescript_project",
|
|
23
|
+
"list_presets",
|
|
24
|
+
"parse_npm_package_spec",
|
|
25
|
+
"register_package",
|
|
26
|
+
"register_preset",
|
|
27
|
+
"resolve_entry",
|
|
28
|
+
"unregister_item",
|
|
29
|
+
]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from django.apps import AppConfig
|
|
6
|
+
from django.core.signals import setting_changed
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _clear_manifest_cache_on_setting_change(
|
|
10
|
+
sender: Any,
|
|
11
|
+
setting: str,
|
|
12
|
+
**kwargs: Any,
|
|
13
|
+
) -> None:
|
|
14
|
+
if setting == "DEBUG" or setting.startswith(("DJVITE_", "VITE_")):
|
|
15
|
+
from djvite.manifest import clear_manifest_cache
|
|
16
|
+
|
|
17
|
+
clear_manifest_cache()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class DjviteConfig(AppConfig):
|
|
21
|
+
default_auto_field = "django.db.models.BigAutoField"
|
|
22
|
+
name = "djvite"
|
|
23
|
+
verbose_name = "Django Vite & Tailwind Connector"
|
|
24
|
+
|
|
25
|
+
def ready(self) -> None:
|
|
26
|
+
import djvite.checks # noqa: F401
|
|
27
|
+
|
|
28
|
+
setting_changed.connect(
|
|
29
|
+
_clear_manifest_cache_on_setting_change,
|
|
30
|
+
dispatch_uid="djvite.clear_manifest_cache_on_setting_change",
|
|
31
|
+
)
|