Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lendemor/next 14 #2142

Merged
merged 15 commits into from
Nov 13, 2023
1 change: 1 addition & 0 deletions reflex/.templates/web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module.exports = {
compress: true,
reactStrictMode: true,
trailingSlash: true,
output: "",
};
2 changes: 1 addition & 1 deletion reflex/constants/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Dirs(SimpleNamespace):
# The name of the utils file.
UTILS = "utils"
# The name of the output static directory.
STATIC = "_static"
STATIC = "out"
Lendemor marked this conversation as resolved.
Show resolved Hide resolved
# The name of the state file.
STATE_PATH = "/".join([UTILS, "state"])
# The name of the components file.
Expand Down
6 changes: 3 additions & 3 deletions reflex/constants/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class Commands(SimpleNamespace):
"""The commands to define in package.json."""

DEV = "next dev"
EXPORT = "next build && next export -o _static"
EXPORT_SITEMAP = "next build && next-sitemap && next export -o _static"
EXPORT = "next build"
EXPORT_SITEMAP = "next-sitemap && next build"
Lendemor marked this conversation as resolved.
Show resolved Hide resolved
PROD = "next start"

PATH = os.path.join(Dirs.WEB, "package.json")
Expand All @@ -106,7 +106,7 @@ class Commands(SimpleNamespace):
"focus-visible": "5.2.0",
"framer-motion": "10.16.4",
"json5": "2.2.3",
"next": "13.5.4",
"next": "14.0.1",
"next-sitemap": "4.1.8",
"next-themes": "0.2.0",
"react": "18.2.0",
Expand Down
2 changes: 2 additions & 0 deletions reflex/reflex.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ def export(
console.rule("[bold]Compiling production app and preparing for export.")

if frontend:
# Update some parameters for export
prerequisites.update_next_config(export=True)
# Ensure module can be imported and app.compile() is called.
prerequisites.get_app()
# Set up .web directory and install frontend dependencies.
Expand Down
2 changes: 1 addition & 1 deletion reflex/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ class AppHarnessProd(AppHarness):
frontend_server: Optional[Subdir404TCPServer] = None

def _run_frontend(self):
web_root = self.app_path / reflex.constants.Dirs.WEB / "_static"
web_root = self.app_path / reflex.constants.Dirs.WEB_STATIC
error_page_map = {
404: web_root / "404.html",
}
Expand Down
2 changes: 1 addition & 1 deletion reflex/utils/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def export(
target=os.path.join(
zip_dest_dir, constants.ComponentName.FRONTEND.zip()
),
root_dir=".web/_static",
root_dir=constants.Dirs.WEB_STATIC,
files_to_exclude=files_to_exclude,
exclude_venv_dirs=False,
)
Expand Down
39 changes: 22 additions & 17 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

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


Expand Down Expand Up @@ -288,15 +288,7 @@ def initialize_web_directory():

path_ops.mkdir(constants.Dirs.WEB_ASSETS)

# update nextJS config based on rxConfig
next_config_file = os.path.join(constants.Dirs.WEB, constants.Next.CONFIG_FILE)

with open(next_config_file, "r") as file:
next_config = file.read()
next_config = update_next_config(next_config, get_config())

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

# Initialize the reflex json file.
init_reflex_json()
Expand Down Expand Up @@ -337,16 +329,24 @@ def init_reflex_json():
path_ops.update_json_file(constants.Reflex.JSON, reflex_json)


def update_next_config(next_config: str, config: Config) -> str:
"""Update Next.js config from Reflex config. Is its own function for testing.
def update_next_config(export=False):
"""Update Next.js config from Reflex config.

Args:
next_config: Content of next.config.js.
config: A reflex Config object.

Returns:
The next_config updated from config.
export: if the method run during reflex export.
"""
next_config_file = os.path.join(constants.Dirs.WEB, constants.Next.CONFIG_FILE)

with open(next_config_file, "r") as file:
Lendemor marked this conversation as resolved.
Show resolved Hide resolved
next_config = file.read()

next_config = _update_next_config(next_config, get_config(), export=export)

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


def _update_next_config(next_config, config, export=False):
next_config = re.sub(
"compress: (true|false)",
f'compress: {"true" if config.next_compression else "false"}',
Expand All @@ -357,6 +357,11 @@ def update_next_config(next_config: str, config: Config) -> str:
f'basePath: "{config.frontend_path or ""}"',
next_config,
)

if export:
Lendemor marked this conversation as resolved.
Show resolved Hide resolved
next_config = re.sub('output: ".*?"', 'output: "export"', next_config)
else:
next_config = re.sub('output: "export"', 'output: ""', next_config)
return next_config


Expand Down
50 changes: 44 additions & 6 deletions tests/test_prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from reflex import constants
from reflex.config import Config
from reflex.utils.prerequisites import initialize_requirements_txt, update_next_config
from reflex.utils.prerequisites import _update_next_config, initialize_requirements_txt


@pytest.mark.parametrize(
"template_next_config, reflex_config, expected_next_config",
"input, config, export, expected_output",
[
(
"""
Expand All @@ -17,17 +17,20 @@
compress: true,
reactStrictMode: true,
trailingSlash: true,
output: "",
};
""",
Config(
app_name="test",
),
False,
"""
module.exports = {
basePath: "",
compress: true,
reactStrictMode: true,
trailingSlash: true,
output: "",
};
""",
),
Expand All @@ -38,18 +41,21 @@
compress: true,
reactStrictMode: true,
trailingSlash: true,
output: "",
};
""",
Config(
app_name="test",
next_compression=False,
),
False,
"""
module.exports = {
basePath: "",
compress: false,
reactStrictMode: true,
trailingSlash: true,
output: "",
};
""",
),
Expand All @@ -60,18 +66,21 @@
compress: true,
reactStrictMode: true,
trailingSlash: true,
output: "",
};
""",
Config(
app_name="test",
frontend_path="/test",
),
False,
"""
module.exports = {
basePath: "/test",
compress: true,
reactStrictMode: true,
trailingSlash: true,
output: "",
};
""",
),
Expand All @@ -82,28 +91,57 @@
compress: true,
reactStrictMode: true,
trailingSlash: true,
output: "",
};
""",
Config(
app_name="test",
frontend_path="/test",
next_compression=False,
),
False,
"""
module.exports = {
basePath: "/test",
compress: false,
reactStrictMode: true,
trailingSlash: true,
output: "",
};
""",
),
(
"""
module.exports = {
basePath: "",
compress: true,
reactStrictMode: true,
trailingSlash: true,
output: "",
};
""",
Config(
app_name="test",
),
True,
"""
module.exports = {
basePath: "",
compress: true,
reactStrictMode: true,
trailingSlash: true,
output: "export",
};
""",
),
],
)
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
)
def test_update_next_config(input, config, export, expected_output):
output = _update_next_config(input, config, export=export)
assert output == expected_output

if export:
assert _update_next_config(output, config) == input


def test_initialize_requirements_txt(mocker):
Expand Down
Loading