A Reflex Plugin that allows you to fully customize your Vite config
Project description
Reflex Vite Config Plugin
A Reflex rx.Config() plugin that allows you to fully customize your Vite configuration using Python dictionaries. This plugin seamlessly integrates with Reflex's build system to provide complete control over Vite's build process, development server, and optimization settings.
Table of Contents:
Features
- Pythonic Configuration: Define Vite configs using Python dictionaries with full type safety
- Complete Vite API Coverage: Support for all Vite configuration options
- Raw JavaScript Support: Embed raw JavaScript code where needed using the
RawJSwrapper - Reflex Default Merging: Utilizes deep merging with Reflex's defaults
- Type Safety: Type hints for all configuration options
- Development Tools: Enhanced development server configuration
Requirements
- Python 3.11+
- Reflex >= 0.8.17
Quick Start
Installation
pip install reflex-vite-config-plugin
Basic Usage
import reflex as rx
from vite_config_plugin import ViteConfigPlugin, RawJS
# Create your Vite configuration
vite_config = {
"server": {
"port": 4000,
"host": "0.0.0.0"
},
"build": {
"sourcemap": True,
"target": "es2020"
}
}
# Create and configure your app
app = rx.App(
plugins=[
ViteConfigPlugin(vite_config)
]
)
Advanced Configuration
from vite_config_plugin import ViteConfigPlugin, RawJS
# Advanced configuration with custom plugins and optimization
advanced_config = {
"plugins": [
RawJS("vue()"),
RawJS("typescript()"),
],
"server": {
"port": 3000,
"hmr": {
"port": 24678,
"overlay": True
},
"proxy": {
"/api": "http://localhost:8000"
}
},
"build": {
"target": "es2020",
"sourcemap": True,
"rollupOptions": {
"external": ["react", "react-dom"],
"output": {
"manualChunks": RawJS("""
(id) => {
if (id.includes('node_modules')) {
return 'vendor';
}
}
""")
}
}
},
"resolve": {
"alias": [
{"find": "@", "replacement": "./src"},
{"find": "@components", "replacement": "./src/components"},
{"find": "@utils", "replacement": "./src/utils"}
]
},
"optimizeDeps": {
"include": ["lodash", "axios"],
"exclude": ["@my/custom-package"]
}
}
# With custom imports
app = rx.App(
plugins=[
ViteConfigPlugin(
advanced_config,
imports=[
'import vue from "@vitejs/plugin-vue";',
'import typescript from "@rollup/plugin-typescript";'
]
)
]
)
Configuration Options
The plugin supports all Vite configuration options through typed dictionaries:
Server Configuration
{
"server": {
"host": "localhost", # Server host
"port": 3000, # Server port
"strictPort": False, # Strict port binding
"https": { # HTTPS configuration
"key": "/path/to/key.pem",
"cert": "/path/to/cert.pem"
},
"proxy": { # Proxy configuration
"/api": "http://localhost:8000",
"/ws": {
"target": "ws://localhost:8080",
"ws": True
}
},
"hmr": { # Hot Module Replacement
"port": 24678,
"overlay": True
}
}
}
Build Configuration
{
"build": {
"target": "es2020", # Build target
"outDir": "dist", # Output directory
"sourcemap": True, # Generate sourcemaps
"minify": "esbuild", # Minification method
"rollupOptions": { # Rollup-specific options
"external": ["react"],
"output": {
"format": "es"
}
},
"lib": { # Library mode
"entry": "./src/lib.ts",
"name": "MyLib",
"formats": ["es", "umd"]
}
}
}
Module Resolution
{
"resolve": {
"alias": [ # Path aliases
{"find": "@", "replacement": "./src"},
{"find": "~", "replacement": "./"}
],
"extensions": [".js", ".ts", ".jsx", ".tsx"],
"mainFields": ["browser", "module", "main"]
}
}
CSS Configuration
{
"css": {
"postcss": "./postcss.config.js",
"preprocessorOptions": {
"scss": {
"additionalData": "$primary: #1976d2;"
},
"less": {
"math": "parens-division"
}
}
}
}
Using RawJS for JavaScript Code
For configuration values that need to be raw JavaScript, use the RawJS wrapper:
from vite_config_plugin import RawJS
vite_config = {
"define": {
"__VERSION__": RawJS("JSON.stringify(process.env.npm_package_version)"),
"__DEV__": RawJS("process.env.NODE_ENV === 'development'")
},
"plugins": [
RawJS("react()"),
RawJS("viteTsconfigPaths()")
],
"build": {
"rollupOptions": {
"output": {
"manualChunks": RawJS("""
(id) => {
if (id.includes('node_modules')) {
if (id.includes('react')) {
return 'react-vendor';
}
return 'vendor';
}
}
""")
}
}
}
}
Custom Imports
Add custom JavaScript imports to your Vite config:
config = rx.Config(
plugins=[
ViteConfigPlugin(
config,
imports=[
'import react from "@vitejs/plugin-react";',
'import { resolve } from "path";',
'import { defineConfig, loadEnv } from "vite";'
]
)
]
)
API Reference
ViteConfigPlugin
The main plugin class for Vite configuration.
ViteConfigPlugin(
config: ViteConfig,
*,
imports: list[str] | None = None
)
Parameters:
config: A dictionary containing Vite configuration optionsimports: Optional list of JavaScript import statements
RawJS
Wrapper for embedding raw JavaScript code in configuration.
RawJS(code: str)
Parameters:
code: Raw JavaScript code string
Type Definitions
The plugin includes comprehensive TypeScript-style type definitions:
ViteConfig: Main configuration typeServer: Development server optionsBuildOptions: Build configurationResolve: Module resolution settingsOptimizeDeps: Dependency optimization- And many more...
Example
import reflex as rx
from vite_config_plugin import ViteConfigPlugin, RawJS
config = rx.Config(
plugins=[
ViteConfigPlugin(
{
"plugins": [
RawJS("react()"),
RawJS("tsconfigPaths()")
],
"build": {
"target": "es2020",
"sourcemap": True
},
"server": {
"port": 3000,
"open": True
}
},
imports=[
'import react from "@vitejs/plugin-react";',
'import tsconfigPaths from "vite-tsconfig-paths";'
],
),
],
)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Prereuisites
This repository builds with pantsbuild which requires Linux or Mac. If you're on a Windows, you can use WSL.
Install pants using the provided instructions for your OS.
Development Setup
- Fork the repository and clone.
- Install dependencies:
pants lock - Optionally export a venv:
pants venv. Createsdist/export/python/virtualenvs/python-default/3.X.Y. - If you run
pants venvyou can update your IDE interpreter using the available venv.
Now you can make code changes as necessary.
Submitting a PR
Before you submit a PR, ensure the following run without errors:
- Run tests:
pants test all - Format code:
pants fmt all - Lint code:
pants lint all
Links
FAQ
Q: Can I use this plugin with existing Vite plugins?
A: Yes! You can include any Vite plugin using the RawJS wrapper in the plugins array.
Q: How do I handle environment-specific configurations?
A: Use Python's environment variables and conditionals to create different configs for development/production.
Q: Can I override the default Reflex Vite configuration?
A: That depends. The plugin uses deep dict merging, so MOST of your configuration will override defaults but lists of items are concantenated rather than overridden. That being said, it is not recommended to override Reflex's defaults as it can break Reflex/Vite in unexpected ways.
Made with ❤️ for the Reflex community
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file reflex_vite_config_plugin-0.0.1.tar.gz.
File metadata
- Download URL: reflex_vite_config_plugin-0.0.1.tar.gz
- Upload date:
- Size: 51.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f7c96fca653531d2b1e3b5ef6474f6ba604933c2efa1336181334d9d5fa910fb
|
|
| MD5 |
10bfed960093f028bfedf76f7403d1ca
|
|
| BLAKE2b-256 |
c9e983f1eac46f24768bd7d69d4e675790cb8e648f51ce7a2c7982d4dd928fc1
|
Provenance
The following attestation bundles were made for reflex_vite_config_plugin-0.0.1.tar.gz:
Publisher:
publish.yaml on riebecj/reflex-vite-config-plugin
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
reflex_vite_config_plugin-0.0.1.tar.gz -
Subject digest:
f7c96fca653531d2b1e3b5ef6474f6ba604933c2efa1336181334d9d5fa910fb - Sigstore transparency entry: 646693688
- Sigstore integration time:
-
Permalink:
riebecj/reflex-vite-config-plugin@f623cc0dd4b57b462ff32b6b6b657e21ed36984e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/riebecj
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@f623cc0dd4b57b462ff32b6b6b657e21ed36984e -
Trigger Event:
push
-
Statement type:
File details
Details for the file reflex_vite_config_plugin-0.0.1-py3-none-any.whl.
File metadata
- Download URL: reflex_vite_config_plugin-0.0.1-py3-none-any.whl
- Upload date:
- Size: 35.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c47f893a806813c7ade986c883f5bb88acd24047c37ff8352c1c6b1b111b0b9
|
|
| MD5 |
33af6b7ee74c132cb3063991ac785471
|
|
| BLAKE2b-256 |
7e117fd931085d720f43643120bb28d20b596535ebcc43b2d0ff43b7ce0199f0
|
Provenance
The following attestation bundles were made for reflex_vite_config_plugin-0.0.1-py3-none-any.whl:
Publisher:
publish.yaml on riebecj/reflex-vite-config-plugin
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
reflex_vite_config_plugin-0.0.1-py3-none-any.whl -
Subject digest:
8c47f893a806813c7ade986c883f5bb88acd24047c37ff8352c1c6b1b111b0b9 - Sigstore transparency entry: 646693719
- Sigstore integration time:
-
Permalink:
riebecj/reflex-vite-config-plugin@f623cc0dd4b57b462ff32b6b6b657e21ed36984e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/riebecj
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@f623cc0dd4b57b462ff32b6b6b657e21ed36984e -
Trigger Event:
push
-
Statement type: