Skip to content

Commit ebdeaa7

Browse files
committed
ENG-6309: Ignore dynamic import analysis issues
* Restructure dynamic import code snippet * For non-markdown use cases, perform the dynamic import inside of useEffect to avoid re-running the code multiple times.
1 parent 684ca96 commit ebdeaa7

2 files changed

Lines changed: 60 additions & 54 deletions

File tree

pyi_hashes.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"reflex/components/core/sticky.pyi": "66c4ce29e7f9419bbb2101278973c0e0",
2323
"reflex/components/core/upload.pyi": "90039579d573a7aa887d40889ced6154",
2424
"reflex/components/datadisplay/__init__.pyi": "cf087efa8b3960decc6b231cc986cfa9",
25-
"reflex/components/datadisplay/code.pyi": "735a3bc0693e5c9bcb52af072aa10bd7",
25+
"reflex/components/datadisplay/code.pyi": "4a05f580a6cdd86346cd083ccd29ccec",
2626
"reflex/components/datadisplay/dataeditor.pyi": "82ff9fcac9209b8edabe9798f3d3bd9f",
2727
"reflex/components/datadisplay/shiki_code_block.pyi": "ecbb5e9d66023204ae2c23cc3f6a640c",
2828
"reflex/components/el/__init__.pyi": "f07f5957ca4dc3d95ffdc2ddb75fe2f8",

reflex/components/datadisplay/code.py

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,54 @@
300300
"yang",
301301
"zig",
302302
]
303+
_LITERAL_CODE_LANGUAGE_VAR = Var.create(typing.get_args(LiteralCodeLanguage))
304+
305+
306+
def _get_language_registration_hook(language: Var | str) -> Var:
307+
"""Get the hook to register the language.
308+
309+
Args:
310+
language: The const/literal Var of the language module to import.
311+
For markdown, use the default placeholder _LANGUAGE.
312+
For direct use, a LiteralStringVar should be passed via the language
313+
prop.
314+
315+
Returns:
316+
JS snippet that will register the language with react-syntax-highlighter when evaluated.
317+
"""
318+
language_var = Var.create(language)
319+
language_in_there = _LITERAL_CODE_LANGUAGE_VAR.contains(language_var)
320+
check_language = (
321+
f"if ({language_var} && !{language_in_there}) {{ console.warn(`Language \\`${{{language_var}}}\\` is not supported for code blocks inside of markdown.`); {language_var} = ''; }}"
322+
if not isinstance(language_var, LiteralVar)
323+
else ""
324+
)
325+
import_statement = (
326+
f"import('react-syntax-highlighter/dist/esm/languages/prism/{language_var._var_value}')"
327+
if isinstance(language_var, LiteralVar)
328+
else f"import(/* @vite-ignore */`react-syntax-highlighter/dist/esm/languages/prism/${{{language_var}}}`)"
329+
)
330+
async_load = f"""
331+
(async () => {{
332+
if ({language_var} == '') {{ return; }}
333+
try {{
334+
const module = await {import_statement};
335+
SyntaxHighlighter.registerLanguage({language_var}, module.default);
336+
}} catch (error) {{
337+
console.error(`Language ${{{language_var}}} is not supported for code blocks inside of markdown: `, error);
338+
}}
339+
}})();
340+
"""
341+
return Var(
342+
f"\n{check_language}\n{async_load}",
343+
_var_data=VarData(
344+
imports={
345+
CodeBlock.get_fields()["library"].default_value(): [
346+
ImportVar(tag="PrismAsyncLight", alias="SyntaxHighlighter")
347+
]
348+
},
349+
),
350+
)
303351

304352

305353
def construct_theme_var(theme: str) -> Var[Theme]:
@@ -315,7 +363,7 @@ def construct_theme_var(theme: str) -> Var[Theme]:
315363
theme,
316364
_var_data=VarData(
317365
imports={
318-
f"react-syntax-highlighter/dist/cjs/styles/prism/{format.to_kebab_case(theme)}": [
366+
f"react-syntax-highlighter/dist/esm/styles/prism/{format.to_kebab_case(theme)}": [
319367
ImportVar(tag=theme, is_default=True, install=False)
320368
]
321369
}
@@ -502,64 +550,14 @@ def _render(self):
502550
def _exclude_props(self) -> list[str]:
503551
return ["can_copy", "copy_button"]
504552

505-
@classmethod
506-
def _get_language_registration_hook(cls, language_var: Var = _LANGUAGE) -> Var:
507-
"""Get the hook to register the language.
508-
509-
Args:
510-
language_var: The const/literal Var of the language module to import.
511-
For markdown, uses the default placeholder _LANGUAGE. For direct use,
512-
a LiteralStringVar should be passed via the language prop.
513-
514-
Returns:
515-
The hook to register the language.
516-
"""
517-
language_in_there = Var.create(typing.get_args(LiteralCodeLanguage)).contains(
518-
language_var
519-
)
520-
async_load = f"""
521-
(async () => {{
522-
try {{
523-
const module = await import(`react-syntax-highlighter/dist/cjs/languages/prism/${{{language_var!s}}}`);
524-
SyntaxHighlighter.registerLanguage({language_var!s}, module.default);
525-
}} catch (error) {{
526-
console.error(`Language ${{{language_var!s}}} is not supported for code blocks inside of markdown: `, error);
527-
}}
528-
}})();
529-
"""
530-
return Var(
531-
f"""
532-
if ({language_var!s}) {{
533-
if (!{language_in_there!s}) {{
534-
console.warn(`Language \\`${{{language_var!s}}}\\` is not supported for code blocks inside of markdown.`);
535-
{language_var!s} = '';
536-
}} else {{
537-
{async_load!s}
538-
}}
539-
}}
540-
"""
541-
if not isinstance(language_var, LiteralVar)
542-
else f"""
543-
if ({language_var!s}) {{
544-
{async_load!s}
545-
}}""",
546-
_var_data=VarData(
547-
imports={
548-
cls.get_fields()["library"].default_value(): [
549-
ImportVar(tag="PrismAsyncLight", alias="SyntaxHighlighter")
550-
]
551-
},
552-
),
553-
)
554-
555553
@classmethod
556554
def get_component_map_custom_code(cls) -> Var:
557555
"""Get the custom code for the component.
558556
559557
Returns:
560558
The custom code for the component.
561559
"""
562-
return cls._get_language_registration_hook()
560+
return _get_language_registration_hook(_LANGUAGE)
563561

564562
def add_hooks(self) -> list[str | Var]:
565563
"""Add hooks for the component.
@@ -568,7 +566,15 @@ def add_hooks(self) -> list[str | Var]:
568566
The hooks for the component.
569567
"""
570568
return [
571-
self._get_language_registration_hook(language_var=self.language),
569+
Var(
570+
f"useEffect(() => {{ {_get_language_registration_hook(self.language)} }},"
571+
f"[{self.language if not isinstance(self.language, LiteralVar) else ''}]);",
572+
_var_data=VarData(
573+
imports={
574+
"react": "useEffect",
575+
},
576+
),
577+
),
572578
]
573579

574580

0 commit comments

Comments
 (0)