From 2a5480ff056f51b64e84a96691f7a53ab986ea23 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Thu, 28 May 2026 09:55:41 +0545 Subject: [PATCH] =?UTF-8?q?feat(src/djinit/templates):=20AddVite=E2=80=91b?= =?UTF-8?q?ased=20asset=20pipeline=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - New Features - Insert manifest_path and static_url_prefix into base settings for Vite output - Enable/disable dev mode via DJANGO_VITE["default"]["dev_mode"] in development and production templates - Add django‑vite dependency conditionally when use_vite is set in pyproject.toml and requirements - Create new vite.config.ts template with React, Tailwind, dev server proxy, and asset resolution - Enhancements - Align Tailwind and Django settings to respect Vite mode - Update dependency templates to reflect Vite usage - Bug Fixes - (none) --- .../templates/config/settings/base.py-tpl | 10 +++++ .../config/settings/development.py-tpl | 5 +++ .../config/settings/production.py-tpl | 5 +++ .../templates/project/pyproject.toml-tpl | 3 ++ src/djinit/templates/project/requirements-tpl | 1 + .../templates/project/vite.config.ts-tpl | 44 +++++++++++++++++++ 6 files changed, 68 insertions(+) create mode 100644 src/djinit/templates/project/vite.config.ts-tpl diff --git a/src/djinit/templates/config/settings/base.py-tpl b/src/djinit/templates/config/settings/base.py-tpl index 15a5da0..29547fd 100644 --- a/src/djinit/templates/config/settings/base.py-tpl +++ b/src/djinit/templates/config/settings/base.py-tpl @@ -225,3 +225,13 @@ TAILWIND_CLI_CONFIG = { }, } # @ENDIF + +# @IF use_vite +# Vite Settings +DJANGO_VITE = { + "default": { + "manifest_path": BASE_DIR / "static" / "dist" / "manifest.json", + "static_url_prefix": "dist", + } +} +# @ENDIF diff --git a/src/djinit/templates/config/settings/development.py-tpl b/src/djinit/templates/config/settings/development.py-tpl index 2e3c1b3..60a9e62 100644 --- a/src/djinit/templates/config/settings/development.py-tpl +++ b/src/djinit/templates/config/settings/development.py-tpl @@ -40,3 +40,8 @@ CORS_ALLOW_CREDENTIALS = True # Disable HTTPS redirect in development SECURE_SSL_REDIRECT = False + +# @IF use_vite +# Vite Settings +DJANGO_VITE["default"]["dev_mode"] = True +# @ENDIF diff --git a/src/djinit/templates/config/settings/production.py-tpl b/src/djinit/templates/config/settings/production.py-tpl index c454e0f..40512a7 100644 --- a/src/djinit/templates/config/settings/production.py-tpl +++ b/src/djinit/templates/config/settings/production.py-tpl @@ -72,3 +72,8 @@ TAILWIND_CLI_VERSION = "4.1.3" # Pin version TAILWIND_CLI_AUTOMATIC_DOWNLOAD = False # Use pre-installed CLI TAILWIND_CLI_DIST_CSS = "static/css/tailwind.min.css" # @ENDIF + +# @IF use_vite +# Vite Settings +DJANGO_VITE["default"]["dev_mode"] = False +# @ENDIF diff --git a/src/djinit/templates/project/pyproject.toml-tpl b/src/djinit/templates/project/pyproject.toml-tpl index 37e9a4f..532123a 100644 --- a/src/djinit/templates/project/pyproject.toml-tpl +++ b/src/djinit/templates/project/pyproject.toml-tpl @@ -26,6 +26,9 @@ dependencies = [ # @IF use_htmx "django-htmx", # @ENDIF +# @IF use_vite + "django-vite", +# @ENDIF # @IF use_database_url "dj-database-url", # @ENDIF diff --git a/src/djinit/templates/project/requirements-tpl b/src/djinit/templates/project/requirements-tpl index bfefe12..9dbb655 100644 --- a/src/djinit/templates/project/requirements-tpl +++ b/src/djinit/templates/project/requirements-tpl @@ -12,6 +12,7 @@ django-cors-headers whitenoise django-tailwind-cli # @IF use_tailwind django-htmx # @IF use_htmx +django-vite # @IF use_vite # @IF database_type == 'mysql' mysqlclient # @ELSE diff --git a/src/djinit/templates/project/vite.config.ts-tpl b/src/djinit/templates/project/vite.config.ts-tpl new file mode 100644 index 0000000..60f872b --- /dev/null +++ b/src/djinit/templates/project/vite.config.ts-tpl @@ -0,0 +1,44 @@ +import { defineConfig } from "vite" +import react from "@vitejs/plugin-react-swc" +import tailwindcss from "@tailwindcss/vite" +import path from "path" + +// https://vitejs.dev/config/ +export default defineConfig(({ mode }) => ({ + plugins: [react(), tailwindcss()], + base: mode === "production" ? "/static/" : "/", + build: { + outDir: "../[[ module_name ]]/static/dist", + emptyOutDir: true, + manifest: "manifest.json", + rollupOptions: { + input: "src/main.tsx", + }, + }, + server: { + port: 5173, + host: "127.0.0.1", + proxy: { + "/api": { + target: "http://localhost:8000", + changeOrigin: true, + }, + "/media": { + target: "http://localhost:8000", + changeOrigin: true, + }, + }, + }, + resolve: { + alias: { + "@": path.resolve(__dirname, "./src"), + }, + dedupe: [ + "react", + "react-dom", + "react/jsx-runtime", + "react/jsx-dev-runtime", + "@tanstack/react-query", + ], + }, +}))