|
@@ -1,12 +1,18 @@
|
|
|
"""Components that are dynamically generated on the backend."""
|
|
|
|
|
|
+from typing import TYPE_CHECKING
|
|
|
+
|
|
|
from reflex import constants
|
|
|
from reflex.utils import imports
|
|
|
+from reflex.utils.exceptions import DynamicComponentMissingLibrary
|
|
|
from reflex.utils.format import format_library_name
|
|
|
from reflex.utils.serializers import serializer
|
|
|
from reflex.vars import Var, get_unique_variable_name
|
|
|
from reflex.vars.base import VarData, transform
|
|
|
|
|
|
+if TYPE_CHECKING:
|
|
|
+ from reflex.components.component import Component
|
|
|
+
|
|
|
|
|
|
def get_cdn_url(lib: str) -> str:
|
|
|
"""Get the CDN URL for a library.
|
|
@@ -20,6 +26,23 @@ def get_cdn_url(lib: str) -> str:
|
|
|
return f"https://cdn.jsdelivr.net/npm/{lib}" + "/+esm"
|
|
|
|
|
|
|
|
|
+bundled_libraries = {"react", "@radix-ui/themes"}
|
|
|
+
|
|
|
+
|
|
|
+def bundle_library(component: "Component"):
|
|
|
+ """Bundle a library with the component.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ component: The component to bundle the library with.
|
|
|
+
|
|
|
+ Raises:
|
|
|
+ DynamicComponentMissingLibrary: Raised when a dynamic component is missing a library.
|
|
|
+ """
|
|
|
+ if component.library is None:
|
|
|
+ raise DynamicComponentMissingLibrary("Component must have a library to bundle.")
|
|
|
+ bundled_libraries.add(format_library_name(component.library))
|
|
|
+
|
|
|
+
|
|
|
def load_dynamic_serializer():
|
|
|
"""Load the serializer for dynamic components."""
|
|
|
# Causes a circular import, so we import here.
|
|
@@ -58,10 +81,7 @@ def load_dynamic_serializer():
|
|
|
)
|
|
|
] = None
|
|
|
|
|
|
- libs_in_window = [
|
|
|
- "react",
|
|
|
- "@radix-ui/themes",
|
|
|
- ]
|
|
|
+ libs_in_window = bundled_libraries
|
|
|
|
|
|
imports = {}
|
|
|
for lib, names in component._get_all_imports().items():
|
|
@@ -69,10 +89,7 @@ def load_dynamic_serializer():
|
|
|
if (
|
|
|
not lib.startswith((".", "/"))
|
|
|
and not lib.startswith("http")
|
|
|
- and all(
|
|
|
- formatted_lib_name != lib_in_window
|
|
|
- for lib_in_window in libs_in_window
|
|
|
- )
|
|
|
+ and formatted_lib_name not in libs_in_window
|
|
|
):
|
|
|
imports[get_cdn_url(lib)] = names
|
|
|
else:
|