bundle chakra in window for CSR

This commit is contained in:
Khaleel Al-Adhami 2024-10-02 14:40:39 -07:00
parent c08720ed1a
commit aabb1ed190
3 changed files with 29 additions and 13 deletions

View File

@ -7,10 +7,9 @@ import '/styles/styles.css'
{% block declaration %} {% block declaration %}
import { EventLoopProvider, StateProvider, defaultColorMode } from "/utils/context.js"; import { EventLoopProvider, StateProvider, defaultColorMode } from "/utils/context.js";
import { ThemeProvider } from 'next-themes' import { ThemeProvider } from 'next-themes'
import * as React from "react"; {% for library_alias, library_path in window_libraries %}
import * as utils_context from "/utils/context.js"; import * as {{library_alias}} from "{{library_path}}";
import * as utils_state from "/utils/state.js"; {% endfor %}
import * as radix from "@radix-ui/themes";
{% for custom_code in custom_codes %} {% for custom_code in custom_codes %}
{{custom_code}} {{custom_code}}
@ -33,10 +32,9 @@ export default function MyApp({ Component, pageProps }) {
React.useEffect(() => { React.useEffect(() => {
// Make contexts and state objects available globally for dynamic eval'd components // Make contexts and state objects available globally for dynamic eval'd components
let windowImports = { let windowImports = {
"react": React, {% for library_alias, library_path in window_libraries %}
"@radix-ui/themes": radix, "{{library_path}}": {{library_alias}},
"/utils/context": utils_context, {% endfor %}
"/utils/state": utils_state,
}; };
window["__reflex"] = windowImports; window["__reflex"] = windowImports;
}, []); }, []);

View File

@ -4,6 +4,7 @@ from __future__ import annotations
import os import os
from datetime import datetime from datetime import datetime
from importlib.util import find_spec
from pathlib import Path from pathlib import Path
from typing import Dict, Iterable, Optional, Type, Union from typing import Dict, Iterable, Optional, Type, Union
@ -49,10 +50,25 @@ def _compile_app(app_root: Component) -> str:
Returns: Returns:
The compiled app. The compiled app.
""" """
chakra_available = find_spec("reflex_chakra") is not None
return templates.APP_ROOT.render( return templates.APP_ROOT.render(
imports=utils.compile_imports(app_root._get_all_imports()), imports=utils.compile_imports(app_root._get_all_imports()),
custom_codes=app_root._get_all_custom_code(), custom_codes=app_root._get_all_custom_code(),
hooks={**app_root._get_all_hooks_internal(), **app_root._get_all_hooks()}, hooks={**app_root._get_all_hooks_internal(), **app_root._get_all_hooks()},
window_libraries=[
("React", "react"),
("utils_context", f"/{constants.Dirs.UTILS}/context.js"),
("utils_state", f"/{constants.Dirs.UTILS}/state.js"),
("radix", "@radix-ui/themes"),
]
+ (
[
("chakra", "@chakra-ui/react"),
]
if chakra_available
else []
),
render=app_root.render(), render=app_root.render(),
) )

View File

@ -1,5 +1,7 @@
"""Components that are dynamically generated on the backend.""" """Components that are dynamically generated on the backend."""
from importlib.util import find_spec
from reflex import constants from reflex import constants
from reflex.utils import imports from reflex.utils import imports
from reflex.utils.format import format_library_name from reflex.utils.format import format_library_name
@ -58,10 +60,13 @@ def load_dynamic_serializer():
) )
] = None ] = None
chakra_available = find_spec("reflex_chakra") is not None
libs_in_window = [ libs_in_window = [
"react", "react",
"@radix-ui/themes", "@radix-ui/themes",
] "@chakra-ui/react",
] + (["@chakra-ui/react"] if chakra_available else [])
imports = {} imports = {}
for lib, names in component._get_all_imports().items(): for lib, names in component._get_all_imports().items():
@ -69,10 +74,7 @@ def load_dynamic_serializer():
if ( if (
not lib.startswith((".", "/")) not lib.startswith((".", "/"))
and not lib.startswith("http") and not lib.startswith("http")
and all( and formatted_lib_name not in libs_in_window
formatted_lib_name != lib_in_window
for lib_in_window in libs_in_window
)
): ):
imports[get_cdn_url(lib)] = names imports[get_cdn_url(lib)] = names
else: else: