Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions reflex/compiler/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,13 @@ def package_json_template(
)


def vite_config_template(base: str):
def vite_config_template(base: str, hmr: bool, force_full_reload: bool):
"""Template for vite.config.js.

Args:
base: The base path for the Vite config.
hmr: Whether to enable hot module replacement.
force_full_reload: Whether to force a full reload on changes.

Returns:
Rendered vite.config.js content as string.
Expand Down Expand Up @@ -537,12 +539,25 @@ def vite_config_template(base: str):
}};
}}

function fullReload() {{
return {{
name: "full-reload",
enforce: "pre",
handleHotUpdate({{ server }}) {{
server.ws.send({{
type: "full-reload",
}});
return [];
}}
}};
}}

export default defineConfig((config) => ({{
plugins: [
alwaysUseReactDomServerNode(),
reactRouter(),
safariCacheBustPlugin(),
],
].concat({"[fullReload()]" if force_full_reload else "[]"}),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why concat and not just {"fullReload()" if force_full_reload else ""} in the plugins list?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a strong reason i just wanted it to be at least in theory extendable but it's not different either way

build: {{
assetsDir: "{base}assets".slice(1),
rollupOptions: {{
Expand All @@ -564,6 +579,7 @@ def vite_config_template(base: str):
}},
server: {{
port: process.env.PORT,
hmr: {"true" if hmr else "false"},
watch: {{
ignored: [
"**/.web/backend/**",
Expand Down
6 changes: 6 additions & 0 deletions reflex/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,12 @@ class EnvironmentVariables:
# Enable full logging of debug messages to reflex user directory.
REFLEX_ENABLE_FULL_LOGGING: EnvVar[bool] = env_var(False)

# Whether to enable hot module replacement
VITE_HMR: EnvVar[bool] = env_var(True)

# Whether to force a full reload on changes.
VITE_FORCE_FULL_RELOAD: EnvVar[bool] = env_var(False)


environment = EnvironmentVariables()

Expand Down
7 changes: 6 additions & 1 deletion reflex/utils/frontend_skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from reflex import constants
from reflex.compiler import templates
from reflex.config import Config, get_config
from reflex.environment import environment
from reflex.utils import console, path_ops
from reflex.utils.prerequisites import get_project_hash, get_web_dir
from reflex.utils.registry import get_npm_registry
Expand Down Expand Up @@ -192,7 +193,11 @@ def _compile_vite_config(config: Config):
base = "/"
if frontend_path := config.frontend_path.strip("/"):
base += frontend_path + "/"
return templates.vite_config_template(base=base)
return templates.vite_config_template(
base=base,
hmr=environment.VITE_HMR.get(),
force_full_reload=environment.VITE_FORCE_FULL_RELOAD.get(),
)


def initialize_vite_config():
Expand Down
Loading