move dynamic imports to dedicated method (#1785)
This commit is contained in:
parent
3406be3ff8
commit
27eeab485e
@ -4,6 +4,10 @@
|
|||||||
{% for module in imports%}
|
{% for module in imports%}
|
||||||
{{- utils.get_import(module) }}
|
{{- utils.get_import(module) }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
{% for dynamic_import in dynamic_imports %}
|
||||||
|
{{dynamic_import}}
|
||||||
|
{% endfor %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block declaration %}
|
{% block declaration %}
|
||||||
|
@ -109,6 +109,7 @@ def _compile_page(
|
|||||||
# Compile the code to render the component.
|
# Compile the code to render the component.
|
||||||
return templates.PAGE.render(
|
return templates.PAGE.render(
|
||||||
imports=imports,
|
imports=imports,
|
||||||
|
dynamic_imports=component.get_dynamic_imports(),
|
||||||
custom_codes=component.get_custom_code(),
|
custom_codes=component.get_custom_code(),
|
||||||
state_name=state.get_name(),
|
state_name=state.get_name(),
|
||||||
hooks=component.get_hooks(),
|
hooks=component.get_hooks(),
|
||||||
|
@ -91,8 +91,8 @@ def compile_imports(imports: imports.ImportDict) -> List[dict]:
|
|||||||
import_dicts = []
|
import_dicts = []
|
||||||
for lib, fields in imports.items():
|
for lib, fields in imports.items():
|
||||||
default, rest = compile_import_statement(fields)
|
default, rest = compile_import_statement(fields)
|
||||||
# prevent all NoRenderImportVar from being rendered on the page
|
# prevent lib from being rendered on the page if all imports are NoRenderImportVar
|
||||||
if any({f for f in fields if isinstance(f, NoRenderImportVar)}): # type: ignore
|
if all({isinstance(f, NoRenderImportVar) for f in fields}): # type: ignore
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not lib:
|
if not lib:
|
||||||
|
@ -517,6 +517,35 @@ class Component(Base, ABC):
|
|||||||
# Return the code.
|
# Return the code.
|
||||||
return code
|
return code
|
||||||
|
|
||||||
|
def _get_dynamic_imports(self) -> Optional[str]:
|
||||||
|
"""Get dynamic import for the component.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The dynamic import.
|
||||||
|
"""
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_dynamic_imports(self) -> Set[str]:
|
||||||
|
"""Get dynamic imports for the component and its children.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The dynamic imports.
|
||||||
|
"""
|
||||||
|
# Store the import in a set to avoid duplicates.
|
||||||
|
dynamic_imports = set()
|
||||||
|
|
||||||
|
# Get dynamic import for this component.
|
||||||
|
dynamic_import = self._get_dynamic_imports()
|
||||||
|
if dynamic_import:
|
||||||
|
dynamic_imports.add(dynamic_import)
|
||||||
|
|
||||||
|
# Get the dynamic imports from children
|
||||||
|
for child in self.children:
|
||||||
|
dynamic_imports |= child.get_dynamic_imports()
|
||||||
|
|
||||||
|
# Return the dynamic imports
|
||||||
|
return dynamic_imports
|
||||||
|
|
||||||
def _get_imports(self) -> imports.ImportDict:
|
def _get_imports(self) -> imports.ImportDict:
|
||||||
imports = {}
|
imports = {}
|
||||||
if self.library is not None and self.tag is not None:
|
if self.library is not None and self.tag is not None:
|
||||||
@ -846,7 +875,7 @@ class NoSSRComponent(Component):
|
|||||||
|
|
||||||
return imports
|
return imports
|
||||||
|
|
||||||
def _get_custom_code(self) -> str:
|
def _get_dynamic_imports(self) -> str:
|
||||||
opts_fragment = ", { ssr: false });"
|
opts_fragment = ", { ssr: false });"
|
||||||
|
|
||||||
# extract the correct import name from library name
|
# extract the correct import name from library name
|
||||||
|
Loading…
Reference in New Issue
Block a user