From 3c11190d378e1ae480c0a966453c6b5d9c81438d Mon Sep 17 00:00:00 2001 From: Lendemor Date: Mon, 6 Nov 2023 13:42:14 +0100 Subject: [PATCH 01/12] fix to_snake_case and add tests --- reflex/utils/format.py | 2 +- tests/utils/test_format.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/reflex/utils/format.py b/reflex/utils/format.py index 857a61f820..7d18460b5e 100644 --- a/reflex/utils/format.py +++ b/reflex/utils/format.py @@ -122,7 +122,7 @@ def to_snake_case(text: str) -> str: The snake case string. """ s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", text) - return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() + return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower().replace("-", "_") def to_camel_case(text: str) -> str: diff --git a/tests/utils/test_format.py b/tests/utils/test_format.py index 768cc353ab..8e66999626 100644 --- a/tests/utils/test_format.py +++ b/tests/utils/test_format.py @@ -117,6 +117,8 @@ def test_indent(text: str, indent_level: int, expected: str, windows_platform: b ("camelTwoHumps", "camel_two_humps"), ("_start_with_underscore", "_start_with_underscore"), ("__start_with_double_underscore", "__start_with_double_underscore"), + ("kebab-case", "kebab_case"), + ("double-kebab-case", "double_kebab_case"), ], ) def test_to_snake_case(input: str, output: str): From ebc9316c7f9021b9a5b073f6e288867444b514f7 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Tue, 7 Nov 2023 12:51:45 +0100 Subject: [PATCH 02/12] upgrade to next14 --- reflex/constants/installer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflex/constants/installer.py b/reflex/constants/installer.py index b39ecb78c4..1fd25e04db 100644 --- a/reflex/constants/installer.py +++ b/reflex/constants/installer.py @@ -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", From cd0dff562da1dd2b50d7fa111a5139f4e8212aa1 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Tue, 7 Nov 2023 16:29:20 +0100 Subject: [PATCH 03/12] fix export with nextjs 14 --- reflex/.templates/web/next.config.js | 1 + reflex/constants/installer.py | 4 ++-- reflex/utils/prerequisites.py | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/reflex/.templates/web/next.config.js b/reflex/.templates/web/next.config.js index 65c4f4fd5e..63312a0cbb 100644 --- a/reflex/.templates/web/next.config.js +++ b/reflex/.templates/web/next.config.js @@ -3,4 +3,5 @@ module.exports = { compress: true, reactStrictMode: true, trailingSlash: true, + output: "export", }; diff --git a/reflex/constants/installer.py b/reflex/constants/installer.py index 1fd25e04db..28992187bb 100644 --- a/reflex/constants/installer.py +++ b/reflex/constants/installer.py @@ -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 build && next-sitemap" PROD = "next start" PATH = os.path.join(Dirs.WEB, "package.json") diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index ec7d81ee19..f52c4f6ef6 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -347,6 +347,7 @@ def update_next_config(next_config: str, config: Config) -> str: Returns: The next_config updated from config. """ + print(config) next_config = re.sub( "compress: (true|false)", f'compress: {"true" if config.next_compression else "false"}', From e90e3bc051dfe9bd77c1666464cef623127dab17 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Tue, 7 Nov 2023 20:22:22 +0100 Subject: [PATCH 04/12] fix empty zip --- reflex/.templates/web/next.config.js | 2 +- reflex/constants/base.py | 2 +- reflex/reflex.py | 3 ++ reflex/utils/build.py | 2 +- reflex/utils/prerequisites.py | 61 +++++++++++++++------------- 5 files changed, 38 insertions(+), 32 deletions(-) diff --git a/reflex/.templates/web/next.config.js b/reflex/.templates/web/next.config.js index 63312a0cbb..fed02db3e3 100644 --- a/reflex/.templates/web/next.config.js +++ b/reflex/.templates/web/next.config.js @@ -3,5 +3,5 @@ module.exports = { compress: true, reactStrictMode: true, trailingSlash: true, - output: "export", + output: "", }; diff --git a/reflex/constants/base.py b/reflex/constants/base.py index 8957edfb77..caf8c689b4 100644 --- a/reflex/constants/base.py +++ b/reflex/constants/base.py @@ -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" # The name of the state file. STATE_PATH = "/".join([UTILS, "state"]) # The name of the components file. diff --git a/reflex/reflex.py b/reflex/reflex.py index fdf7609474..51b6f40d01 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -336,6 +336,9 @@ def export( # Compile the app in production mode and export it. console.rule("[bold]Compiling production app and preparing for export.") + # Update some parameters for export + prerequisites.update_next_config(export=True) + if frontend: # Ensure module can be imported and app.compile() is called. prerequisites.get_app() diff --git a/reflex/utils/build.py b/reflex/utils/build.py index e4830ef63f..5833bdff56 100644 --- a/reflex/utils/build.py +++ b/reflex/utils/build.py @@ -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, ) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index f52c4f6ef6..6f083ca048 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -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 @@ -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(initial=True) # Initialize the reflex json file. init_reflex_json() @@ -337,28 +329,39 @@ 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(initial=False, 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. + initial: if the method run during reflex init. + export: if the method run during reflex export. """ - print(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 + console.log("Updating next.config.js") + config = get_config() + 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() + + if initial: + 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, + ) + + if export: + next_config = re.sub('output: ".*?"', 'output: "export"', next_config) + else: + next_config = re.sub('output: "export"', 'output: ""', next_config) + + with open(next_config_file, "w") as file: + file.write(next_config) def remove_existing_bun_installation(): From 7fcffc894fd5f767ee73f588eb13f3c94bd1c558 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Wed, 8 Nov 2023 13:29:12 +0100 Subject: [PATCH 05/12] fix sitemap export --- reflex/constants/installer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflex/constants/installer.py b/reflex/constants/installer.py index 28992187bb..bc755d09af 100644 --- a/reflex/constants/installer.py +++ b/reflex/constants/installer.py @@ -96,7 +96,7 @@ class Commands(SimpleNamespace): DEV = "next dev" EXPORT = "next build" - EXPORT_SITEMAP = "next build && next-sitemap" + EXPORT_SITEMAP = "next-sitemap && next build" PROD = "next start" PATH = os.path.join(Dirs.WEB, "package.json") From d08ec8be162ad2c83e07b72df805a58533c04567 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Wed, 8 Nov 2023 13:44:27 +0100 Subject: [PATCH 06/12] fix backend-only export --- reflex/reflex.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/reflex/reflex.py b/reflex/reflex.py index 51b6f40d01..10cb4f5e4e 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -336,10 +336,9 @@ def export( # Compile the app in production mode and export it. console.rule("[bold]Compiling production app and preparing for export.") - # Update some parameters for export - prerequisites.update_next_config(export=True) - 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. From b5203ebd0e05440a3a5392a5f3db00961548cc3e Mon Sep 17 00:00:00 2001 From: Lendemor Date: Wed, 8 Nov 2023 15:17:12 +0100 Subject: [PATCH 07/12] fix tests for _update_next_config --- reflex/utils/prerequisites.py | 39 ++++++++++++++------------- tests/test_prerequisites.py | 50 ++++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 6f083ca048..9193cd7353 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -288,7 +288,7 @@ def initialize_web_directory(): path_ops.mkdir(constants.Dirs.WEB_ASSETS) - update_next_config(initial=True) + update_next_config() # Initialize the reflex json file. init_reflex_json() @@ -329,39 +329,40 @@ def init_reflex_json(): path_ops.update_json_file(constants.Reflex.JSON, reflex_json) -def update_next_config(initial=False, export=False): +def update_next_config(export=False): """Update Next.js config from Reflex config. Args: - initial: if the method run during reflex init. export: if the method run during reflex export. """ - console.log("Updating next.config.js") - config = get_config() 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() - if initial: - 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, - ) + 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"}', + next_config, + ) + next_config = re.sub( + 'basePath: ".*?"', + f'basePath: "{config.frontend_path or ""}"', + next_config, + ) if export: next_config = re.sub('output: ".*?"', 'output: "export"', next_config) else: next_config = re.sub('output: "export"', 'output: ""', next_config) - - with open(next_config_file, "w") as file: - file.write(next_config) + return next_config def remove_existing_bun_installation(): diff --git a/tests/test_prerequisites.py b/tests/test_prerequisites.py index 593129b7d8..d4804b04e0 100644 --- a/tests/test_prerequisites.py +++ b/tests/test_prerequisites.py @@ -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", [ ( """ @@ -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: "", }; """, ), @@ -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: "", }; """, ), @@ -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: "", }; """, ), @@ -82,6 +91,7 @@ compress: true, reactStrictMode: true, trailingSlash: true, + output: "", }; """, Config( @@ -89,21 +99,49 @@ 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): From f740e7ed9b29ebc65af4a548d3878ed6042e1b03 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Wed, 8 Nov 2023 17:12:52 +0100 Subject: [PATCH 08/12] fix testing.py --- reflex/testing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflex/testing.py b/reflex/testing.py index dfea886709..88e3a9e35a 100644 --- a/reflex/testing.py +++ b/reflex/testing.py @@ -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", } From 8b1322a993dbf934748570f7bbc910eddbc0c0e3 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Thu, 9 Nov 2023 14:53:09 +0100 Subject: [PATCH 09/12] update distDir --- reflex/constants/base.py | 2 +- reflex/utils/prerequisites.py | 30 +++++----- tests/test_prerequisites.py | 104 +++------------------------------- 3 files changed, 22 insertions(+), 114 deletions(-) diff --git a/reflex/constants/base.py b/reflex/constants/base.py index caf8c689b4..8957edfb77 100644 --- a/reflex/constants/base.py +++ b/reflex/constants/base.py @@ -24,7 +24,7 @@ class Dirs(SimpleNamespace): # The name of the utils file. UTILS = "utils" # The name of the output static directory. - STATIC = "out" + STATIC = "_static" # The name of the state file. STATE_PATH = "/".join([UTILS, "state"]) # The name of the components file. diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 9193cd7353..e26bf914fb 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -340,29 +340,25 @@ def update_next_config(export=False): with open(next_config_file, "r") as file: next_config = file.read() - next_config = _update_next_config(next_config, get_config(), export=export) + next_config = _update_next_config(get_config(), export=export) with open(next_config_file, "w") as file: file.write(next_config) + file.write("\n") -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"}', - next_config, - ) - next_config = re.sub( - 'basePath: ".*?"', - f'basePath: "{config.frontend_path or ""}"', - next_config, - ) +def _update_next_config(config, export=False): + next_config = { + "basePath": config.frontend_path or "", + "compress": config.next_compression, + "reactStrictMode": True, + "trailingSlash": True, + "output": "export" if export else "", + "distDir": constants.Dirs.STATIC, + } - if export: - next_config = re.sub('output: ".*?"', 'output: "export"', next_config) - else: - next_config = re.sub('output: "export"', 'output: ""', next_config) - return next_config + next_config_json = re.sub(r'"([^"]+)"(?=:)', r"\1", json.dumps(next_config)) + return f"module.exports = {next_config_json};" def remove_existing_bun_installation(): diff --git a/tests/test_prerequisites.py b/tests/test_prerequisites.py index d4804b04e0..04cc9915ed 100644 --- a/tests/test_prerequisites.py +++ b/tests/test_prerequisites.py @@ -8,141 +8,53 @@ @pytest.mark.parametrize( - "input, config, export, expected_output", + "config, export, expected_output", [ ( - """ - module.exports = { - basePath: "", - compress: true, - reactStrictMode: true, - trailingSlash: true, - output: "", - }; - """, Config( app_name="test", ), False, - """ - module.exports = { - basePath: "", - compress: true, - reactStrictMode: true, - trailingSlash: true, - output: "", - }; - """, + 'module.exports = {basePath: "", compress: true, reactStrictMode: true, trailingSlash: true, output: "", distDir: "_static"};', ), ( - """ - module.exports = { - basePath: "", - 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: "", - }; - """, + 'module.exports = {basePath: "", compress: false, reactStrictMode: true, trailingSlash: true, output: "", distDir: "_static"};', ), ( - """ - module.exports = { - basePath: "", - 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: "", - }; - """, + 'module.exports = {basePath: "/test", compress: true, reactStrictMode: true, trailingSlash: true, output: "", distDir: "_static"};', ), ( - """ - module.exports = { - basePath: "", - 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: "/test", compress: false, reactStrictMode: true, trailingSlash: true, output: "", distDir: "_static"};', ), ( - """ - 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", - }; - """, + 'module.exports = {basePath: "", compress: true, reactStrictMode: true, trailingSlash: true, output: "export", distDir: "_static"};', ), ], ) -def test_update_next_config(input, config, export, expected_output): - output = _update_next_config(input, config, export=export) +def test_update_next_config(config, export, expected_output): + output = _update_next_config(config, export=export) assert output == expected_output - if export: - assert _update_next_config(output, config) == input - def test_initialize_requirements_txt(mocker): # File exists, reflex is included, do nothing From 2749430c039964196c3479e717e9ed9506a2ec79 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Thu, 9 Nov 2023 23:13:20 +0100 Subject: [PATCH 10/12] fix sitemap --- reflex/constants/installer.py | 2 +- reflex/reflex.py | 1 + reflex/utils/build.py | 41 ++++++++++++++++++++--------------- reflex/utils/console.py | 7 +++++- reflex/utils/prerequisites.py | 6 ++--- reflex/utils/processes.py | 9 ++++---- 6 files changed, 39 insertions(+), 27 deletions(-) diff --git a/reflex/constants/installer.py b/reflex/constants/installer.py index 8e3fa40d00..99639745b5 100644 --- a/reflex/constants/installer.py +++ b/reflex/constants/installer.py @@ -96,7 +96,7 @@ class Commands(SimpleNamespace): DEV = "next dev" EXPORT = "next build" - EXPORT_SITEMAP = "next-sitemap && next build" + EXPORT_SITEMAP = "next build && next-sitemap" PROD = "next start" PATH = os.path.join(Dirs.WEB, "package.json") diff --git a/reflex/reflex.py b/reflex/reflex.py index 10cb4f5e4e..91e410dcc1 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -184,6 +184,7 @@ def _run( console.rule("[bold]Starting Reflex App") if frontend: + prerequisites.update_next_config() # Get the app module. prerequisites.get_app() diff --git a/reflex/utils/build.py b/reflex/utils/build.py index 5833bdff56..ad3c3ca2c0 100644 --- a/reflex/utils/build.py +++ b/reflex/utils/build.py @@ -35,21 +35,25 @@ def set_os_env(**kwargs): os.environ[key.upper()] = value -def generate_sitemap_config(deploy_url: str): +def generate_sitemap_config(deploy_url: str, export=False): """Generate the sitemap config file. Args: deploy_url: The URL of the deployed app. + export: If the sitemap are generated for an export. """ # Import here to avoid circular imports. from reflex.compiler import templates - config = json.dumps( - { - "siteUrl": deploy_url, - "generateRobotsTxt": True, - } - ) + config = { + "siteUrl": deploy_url, + "generateRobotsTxt": True, + } + + if export: + config["outDir"] = constants.Dirs.STATIC + + config = json.dumps(config) with open(constants.Next.SITEMAP_CONFIG_FILE, "w") as f: f.write(templates.SITEMAP_CONFIG(config=config)) @@ -115,7 +119,7 @@ def _zip( with progress, zipfile.ZipFile(target, "w", zipfile.ZIP_DEFLATED) as zipf: for file in files_to_zip: - console.debug(f"{target}: {file}") + console.debug(f"{target}: {file}", progress=progress) progress.advance(task) zipf.write(file, os.path.relpath(file, root_dir)) @@ -145,22 +149,23 @@ def export( command = "export" if frontend: - # Generate a sitemap if a deploy URL is provided. - if deploy_url is not None: - generate_sitemap_config(deploy_url) - command = "export-sitemap" - checkpoints = [ "Linting and checking ", - "Compiled successfully", + "Creating an optimized production build", "Route (pages)", + "prerendered as static HTML", "Collecting page data", - "automatically rendered as static HTML", - 'Copying "static build" directory', - 'Copying "public" directory', "Finalizing page optimization", - "Export successful", + "Collecting build traces", ] + + # Generate a sitemap if a deploy URL is provided. + if deploy_url is not None: + generate_sitemap_config(deploy_url, export=zip) + command = "export-sitemap" + + checkpoints.extend(["Loading next-sitemap", "Generation completed"]) + # Start the subprocess with the progress bar. process = processes.new_process( [prerequisites.get_package_manager(), "run", command], diff --git a/reflex/utils/console.py b/reflex/utils/console.py index 64a0c7fcc1..10ad7c80d7 100644 --- a/reflex/utils/console.py +++ b/reflex/utils/console.py @@ -45,7 +45,12 @@ def debug(msg: str, **kwargs): kwargs: Keyword arguments to pass to the print function. """ if _LOG_LEVEL <= LogLevel.DEBUG: - print(f"[blue]Debug: {msg}[/blue]", **kwargs) + msg_ = f"[blue]Debug: {msg}[/blue]" + progress = kwargs.pop("progress", None) + if progress: + progress.console.print(msg_, **kwargs) + else: + print(msg_, **kwargs) def info(msg: str, **kwargs): diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index d232ec005c..36bb30e813 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -347,9 +347,10 @@ def _update_next_config(config, export=False): "compress": config.next_compression, "reactStrictMode": True, "trailingSlash": True, - "output": "export" if export else "", - "distDir": constants.Dirs.STATIC, } + if export: + next_config["output"] = "export" + next_config["distDir"] = constants.Dirs.STATIC next_config_json = re.sub(r'"([^"]+)"(?=:)', r"\1", json.dumps(next_config)) return f"module.exports = {next_config_json};" @@ -418,7 +419,6 @@ def download_and_extract_fnm_zip(): def install_node(): """Install fnm and nodejs for use by Reflex.""" if constants.IS_WINDOWS or constants.IS_LINUX and not is_valid_linux(): - path_ops.mkdir(constants.Fnm.DIR) if not os.path.exists(constants.Fnm.EXE): download_and_extract_fnm_zip() diff --git a/reflex/utils/processes.py b/reflex/utils/processes.py index e385a7276d..1691bcb22f 100644 --- a/reflex/utils/processes.py +++ b/reflex/utils/processes.py @@ -196,12 +196,13 @@ def run_concurrently(*fns: Union[Callable, Tuple]) -> None: pass -def stream_logs(message: str, process: subprocess.Popen): +def stream_logs(message: str, process: subprocess.Popen, progress=None): """Stream the logs for a process. Args: message: The message to display. process: The process. + progress: The ongoing progress bar if one is being used. Yields: The lines of the process output. @@ -212,11 +213,11 @@ def stream_logs(message: str, process: subprocess.Popen): # Store the tail of the logs. logs = collections.deque(maxlen=512) with process: - console.debug(message) + console.debug(message, progress=progress) if process.stdout is None: return for line in process.stdout: - console.debug(line, end="") + console.debug(line, end="", progress=progress) logs.append(line) yield line @@ -263,7 +264,7 @@ def show_progress(message: str, process: subprocess.Popen, checkpoints: List[str # Iterate over the process output. with console.progress() as progress: task = progress.add_task(f"{message}: ", total=len(checkpoints)) - for line in stream_logs(message, process): + for line in stream_logs(message, process, progress=progress): # Check for special strings and update the progress bar. for special_string in checkpoints: if special_string in line: From 20de4cdd48502b508bda23b9c8d04da2347975f4 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Thu, 9 Nov 2023 23:22:06 +0100 Subject: [PATCH 11/12] fix tests --- tests/test_prerequisites.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_prerequisites.py b/tests/test_prerequisites.py index 04cc9915ed..ff2c446613 100644 --- a/tests/test_prerequisites.py +++ b/tests/test_prerequisites.py @@ -15,7 +15,7 @@ app_name="test", ), False, - 'module.exports = {basePath: "", compress: true, reactStrictMode: true, trailingSlash: true, output: "", distDir: "_static"};', + 'module.exports = {basePath: "", compress: true, reactStrictMode: true, trailingSlash: true};', ), ( Config( @@ -23,7 +23,7 @@ next_compression=False, ), False, - 'module.exports = {basePath: "", compress: false, reactStrictMode: true, trailingSlash: true, output: "", distDir: "_static"};', + 'module.exports = {basePath: "", compress: false, reactStrictMode: true, trailingSlash: true};', ), ( Config( @@ -31,7 +31,7 @@ frontend_path="/test", ), False, - 'module.exports = {basePath: "/test", compress: true, reactStrictMode: true, trailingSlash: true, output: "", distDir: "_static"};', + 'module.exports = {basePath: "/test", compress: true, reactStrictMode: true, trailingSlash: true};', ), ( Config( @@ -40,7 +40,7 @@ next_compression=False, ), False, - 'module.exports = {basePath: "/test", compress: false, reactStrictMode: true, trailingSlash: true, output: "", distDir: "_static"};', + 'module.exports = {basePath: "/test", compress: false, reactStrictMode: true, trailingSlash: true};', ), ( Config( From 06ce7d5f9c89f6590d466735ef68232abc22ea43 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Mon, 13 Nov 2023 14:49:33 +0100 Subject: [PATCH 12/12] last fixes --- reflex/utils/console.py | 3 +-- reflex/utils/prerequisites.py | 3 --- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/reflex/utils/console.py b/reflex/utils/console.py index 10ad7c80d7..1580da706d 100644 --- a/reflex/utils/console.py +++ b/reflex/utils/console.py @@ -46,8 +46,7 @@ def debug(msg: str, **kwargs): """ if _LOG_LEVEL <= LogLevel.DEBUG: msg_ = f"[blue]Debug: {msg}[/blue]" - progress = kwargs.pop("progress", None) - if progress: + if progress := kwargs.pop("progress", None): progress.console.print(msg_, **kwargs) else: print(msg_, **kwargs) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index ee46d35850..52e8f73175 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -337,9 +337,6 @@ def update_next_config(export=False): """ 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(get_config(), export=export) with open(next_config_file, "w") as file: