make "libsass" an optional dependency
This commit is contained in:
parent
81dab90b55
commit
e815903c1e
@ -8,8 +8,6 @@ from re import IGNORECASE as RE_IGNORECASE
|
|||||||
from re import compile as re_compile
|
from re import compile as re_compile
|
||||||
from typing import TYPE_CHECKING, Dict, Iterable, Optional, Tuple, Type, Union
|
from typing import TYPE_CHECKING, Dict, Iterable, Optional, Tuple, Type, Union
|
||||||
|
|
||||||
from sass import compile as sass_compile
|
|
||||||
|
|
||||||
from reflex import constants
|
from reflex import constants
|
||||||
from reflex.compiler import templates, utils
|
from reflex.compiler import templates, utils
|
||||||
from reflex.components.base.fragment import Fragment
|
from reflex.components.base.fragment import Fragment
|
||||||
@ -23,6 +21,7 @@ from reflex.components.component import (
|
|||||||
from reflex.config import environment, get_config
|
from reflex.config import environment, get_config
|
||||||
from reflex.state import BaseState
|
from reflex.state import BaseState
|
||||||
from reflex.style import SYSTEM_COLOR_MODE
|
from reflex.style import SYSTEM_COLOR_MODE
|
||||||
|
from reflex.utils import console
|
||||||
from reflex.utils.exec import is_prod_mode
|
from reflex.utils.exec import is_prod_mode
|
||||||
from reflex.utils.imports import ImportVar
|
from reflex.utils.imports import ImportVar
|
||||||
from reflex.utils.prerequisites import get_web_dir
|
from reflex.utils.prerequisites import get_web_dir
|
||||||
@ -196,6 +195,8 @@ def _compile_root_stylesheet(stylesheets: list[str]) -> str:
|
|||||||
else []
|
else []
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
sass_compile = None
|
||||||
while len(stylesheets):
|
while len(stylesheets):
|
||||||
stylesheet = stylesheets.pop(0)
|
stylesheet = stylesheets.pop(0)
|
||||||
if not utils.is_valid_url(stylesheet):
|
if not utils.is_valid_url(stylesheet):
|
||||||
@ -244,18 +245,37 @@ def _compile_root_stylesheet(stylesheets: list[str]) -> str:
|
|||||||
/ RE_SASS_SCSS_EXT.sub(".css", str(stylesheet)).strip("/")
|
/ RE_SASS_SCSS_EXT.sub(".css", str(stylesheet)).strip("/")
|
||||||
)
|
)
|
||||||
target.parent.mkdir(parents=True, exist_ok=True)
|
target.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
if stylesheet_full_path.suffix == ".css":
|
||||||
|
target.write_text(
|
||||||
|
data=stylesheet_full_path.read_text(),
|
||||||
|
encoding="utf8",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if sass_compile is None:
|
||||||
|
from sass import compile as sass_compile
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
target.write_text(
|
target.write_text(
|
||||||
data=sass_compile(
|
data=sass_compile(
|
||||||
filename=str(stylesheet_full_path), output_style="compressed"
|
filename=str(stylesheet_full_path),
|
||||||
|
output_style="compressed",
|
||||||
),
|
),
|
||||||
encoding="utf8",
|
encoding="utf8",
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
stylesheet = f"./{RE_SASS_SCSS_EXT.sub('.css', str(stylesheet)).strip('/')}"
|
stylesheet = (
|
||||||
|
f"./{RE_SASS_SCSS_EXT.sub('.css', str(stylesheet)).strip('/')}"
|
||||||
|
)
|
||||||
|
|
||||||
sheets.append(stylesheet) if stylesheet not in sheets else None
|
sheets.append(stylesheet) if stylesheet not in sheets else None
|
||||||
|
except ImportError:
|
||||||
|
console.error(
|
||||||
|
"""The `libsass` package is required to compile sass/scss stylesheet files. Run `pip install "libsass>=0.23.0"`."""
|
||||||
|
)
|
||||||
|
|
||||||
return templates.STYLE.render(stylesheets=sheets)
|
return templates.STYLE.render(stylesheets=sheets)
|
||||||
|
|
||||||
|
@ -155,7 +155,9 @@ def test_compile_stylesheets(tmp_path: Path, mocker):
|
|||||||
|
|
||||||
# NOTE: the css file is also inserted into the s(a|c)ss preprocessor, which compressed the result, which means we don't have the tab, return,... characters.
|
# NOTE: the css file is also inserted into the s(a|c)ss preprocessor, which compressed the result, which means we don't have the tab, return,... characters.
|
||||||
expected_result = "button.rt-Button{border-radius:unset !important}\n"
|
expected_result = "button.rt-Button{border-radius:unset !important}\n"
|
||||||
assert (project / ".web" / "styles" / "styles.css").read_text() == expected_result
|
assert (project / ".web" / "styles" / "styles.css").read_text() == (
|
||||||
|
assets_dir / "styles.css"
|
||||||
|
).read_text()
|
||||||
assert (
|
assert (
|
||||||
project / ".web" / "styles" / "preprocess" / "styles_a.css"
|
project / ".web" / "styles" / "preprocess" / "styles_a.css"
|
||||||
).read_text() == expected_result
|
).read_text() == expected_result
|
||||||
|
Loading…
Reference in New Issue
Block a user