Skip to content

Commit a53c324

Browse files
authored
fix: propagate frontend_path to base in vite.config.js (#5677)
1 parent 71a54bf commit a53c324

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

reflex/.templates/web/vite.config.js renamed to reflex/.templates/jinja/web/vite.config.js.jinja2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function alwaysUseReactDomServerNode() {
2525
}
2626

2727
export default defineConfig((config) => ({
28+
base: "{{base}}",
2829
plugins: [
2930
alwaysUseReactDomServerNode(),
3031
reactRouter(),

reflex/compiler/templates.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ def from_string(source: str) -> Template:
149149
# Code that generate the package json file
150150
PACKAGE_JSON = get_template("web/package.json.jinja2")
151151

152+
# Code that generate the vite.config.js file
153+
VITE_CONFIG = get_template("web/vite.config.js.jinja2")
154+
152155
# Template containing some macros used in the web pages.
153156
MACROS = get_template("web/pages/macros.js.jinja2")
154157

reflex/constants/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ class ReactRouter(Javascript):
160160
# The react router config file
161161
CONFIG_FILE = "react-router.config.js"
162162

163+
# The associated Vite config file
164+
VITE_CONFIG_FILE = "vite.config.js"
165+
163166
# Regex to check for message displayed when frontend comes up
164167
DEV_FRONTEND_LISTENING_REGEX = r"Local:[\s]+"
165168

reflex/utils/prerequisites.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,9 @@ def initialize_web_directory():
972972
console.debug("Initializing the react-router.config.js file.")
973973
update_react_router_config()
974974

975+
console.debug("Initializing the vite.config.js file.")
976+
initialize_vite_config()
977+
975978
console.debug("Initializing the reflex.json file.")
976979
# Initialize the reflex json file.
977980
init_reflex_json(project_hash=project_hash)
@@ -996,6 +999,20 @@ def initialize_package_json():
996999
output_path.write_text(_compile_package_json())
9971000

9981001

1002+
def _compile_vite_config(config: Config):
1003+
# base must have exactly one trailing slash
1004+
base = "/"
1005+
if frontend_path := config.frontend_path.strip("/"):
1006+
base += frontend_path + "/"
1007+
return templates.VITE_CONFIG.render(base=base)
1008+
1009+
1010+
def initialize_vite_config():
1011+
"""Render and write in .web the vite.config.js file using Reflex config."""
1012+
vite_config_file_path = get_web_dir() / constants.ReactRouter.VITE_CONFIG_FILE
1013+
vite_config_file_path.write_text(_compile_vite_config(get_config()))
1014+
1015+
9991016
def initialize_bun_config():
10001017
"""Initialize the bun config file."""
10011018
bun_config_path = get_web_dir() / constants.Bun.CONFIG_PATH

tests/units/test_prerequisites.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from reflex.testing import chdir
1111
from reflex.utils.prerequisites import (
1212
CpuInfo,
13+
_compile_vite_config,
1314
_update_react_router_config,
1415
cached_procedure,
1516
get_cpu_info,
@@ -59,6 +60,37 @@ def test_update_react_router_config(config, export, expected_output):
5960
assert output == expected_output
6061

6162

63+
@pytest.mark.parametrize(
64+
("config", "expected_output"),
65+
[
66+
(
67+
Config(
68+
app_name="test",
69+
frontend_path="",
70+
),
71+
'base: "/",',
72+
),
73+
(
74+
Config(
75+
app_name="test",
76+
frontend_path="/test",
77+
),
78+
'base: "/test/",',
79+
),
80+
(
81+
Config(
82+
app_name="test",
83+
frontend_path="/test/",
84+
),
85+
'base: "/test/",',
86+
),
87+
],
88+
)
89+
def test_initialise_vite_config(config, expected_output):
90+
output = _compile_vite_config(config)
91+
assert expected_output in output
92+
93+
6294
def test_cached_procedure():
6395
call_count = 0
6496

0 commit comments

Comments
 (0)