Skip to content

Commit

Permalink
Support setting Next.js basePath in Reflex config. (reflex-dev#1633)
Browse files Browse the repository at this point in the history
- Tests.
- And sorted config in next.config.js template.
  • Loading branch information
nevdelap committed Aug 22, 2023
1 parent 703ec72 commit 2e3ef76
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 10 deletions.
3 changes: 2 additions & 1 deletion reflex/.templates/web/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
reactStrictMode: true,
basePath: "",
compress: true,
reactStrictMode: true,
trailingSlash: true,
};
3 changes: 3 additions & 0 deletions reflex/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class Config:
# The port to run the frontend on.
frontend_port: int = 3000

# The path to run the frontend on.
frontend_path: str = ""

# The port to run the backend on.
backend_port: int = 8000

Expand Down
36 changes: 27 additions & 9 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from redis import Redis

from reflex import constants, model
from reflex.config import get_config
from reflex.config import Config, get_config
from reflex.utils import console, path_ops, processes


Expand Down Expand Up @@ -214,23 +214,41 @@ def initialize_web_directory():
next_config_file = os.path.join(constants.WEB_DIR, constants.NEXT_CONFIG_FILE)

with open(next_config_file, "r") as file:
lines = file.readlines()
for i, line in enumerate(lines):
if "compress:" in line:
new_line = line.replace(
"true", "true" if get_config().next_compression else "false"
)
lines[i] = new_line
next_config = file.read()
next_config = update_next_config(next_config, get_config())

with open(next_config_file, "w") as file:
file.writelines(lines)
file.write(next_config)

# Write the current version of distributed reflex package to a REFLEX_JSON."""
with open(constants.REFLEX_JSON, "w") as f:
reflex_json = {"version": constants.VERSION}
json.dump(reflex_json, f, ensure_ascii=False)


def update_next_config(next_config: str, config: Config) -> str:
"""Update Next.js config from Reflex config. Is its own function for testing.
Args:
next_config: Content of next.config.js.
config: A reflex Config object.
Returns:
The next_config updated from config.
"""
next_config = re.sub(
"compress: (true|false)",
f'compress: {"true" if config.next_compression else "false"}',
next_config,
)
next_config = re.sub(
'basePath: ".*?"',
f'basePath: "{config.frontend_path or ""}"',
next_config,
)
return next_config


def remove_existing_bun_installation():
"""Remove existing bun installation."""
console.debug("Removing existing bun installation.")
Expand Down
1 change: 1 addition & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_deprecated_params(base_config_values, param):
[
("APP_NAME", "my_test_app"),
("FRONTEND_PORT", 3001),
("FRONTEND_PATH", "/test"),
("BACKEND_PORT", 8001),
("API_URL", "https://mybackend.com:8000"),
("DEPLOY_URL", "https://myfrontend.com"),
Expand Down
103 changes: 103 additions & 0 deletions tests/test_prerequites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import pytest

from reflex.config import Config
from reflex.utils.prerequisites import update_next_config


@pytest.mark.parametrize(
"template_next_config, reflex_config, expected_next_config",
[
(
"""
module.exports = {
basePath: "",
compress: true,
reactStrictMode: true,
trailingSlash: true,
};
""",
Config(
app_name="test",
),
"""
module.exports = {
basePath: "",
compress: true,
reactStrictMode: true,
trailingSlash: true,
};
""",
),
(
"""
module.exports = {
basePath: "",
compress: true,
reactStrictMode: true,
trailingSlash: true,
};
""",
Config(
app_name="test",
next_compression=False,
),
"""
module.exports = {
basePath: "",
compress: false,
reactStrictMode: true,
trailingSlash: true,
};
""",
),
(
"""
module.exports = {
basePath: "",
compress: true,
reactStrictMode: true,
trailingSlash: true,
};
""",
Config(
app_name="test",
frontend_path="/test",
),
"""
module.exports = {
basePath: "/test",
compress: true,
reactStrictMode: true,
trailingSlash: true,
};
""",
),
(
"""
module.exports = {
basePath: "",
compress: true,
reactStrictMode: true,
trailingSlash: true,
};
""",
Config(
app_name="test",
frontend_path="/test",
next_compression=False,
),
"""
module.exports = {
basePath: "/test",
compress: false,
reactStrictMode: true,
trailingSlash: true,
};
""",
),
],
)
def test_update_next_config(template_next_config, reflex_config, expected_next_config):
assert (
update_next_config(template_next_config, reflex_config) == expected_next_config
)

0 comments on commit 2e3ef76

Please sign in to comment.