Skip to content

Commit 1f3fee5

Browse files
committed
[REF-3004] Use relative path to stylesheet for postcss-import compat (#3435)
* test_tailwind: include custom stylesheet * [REF-3004] Use relative path to stylesheet for postcss-import compat postcss-import balls all of the CSS up into a single file, which happens at compile time. So, replace the `@/` with `../public` so the import paths can be resolved relative to the `styles` directory. * test_compiler: fix compile_stylesheets expectations * Use constants.Dirs.PUBLIC instead of "public"
1 parent 8df959d commit 1f3fee5

File tree

5 files changed

+20
-104
lines changed

5 files changed

+20
-104
lines changed

integration/test_tailwind.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def TailwindApp(
2525
paragraph_text: Text for the paragraph.
2626
paragraph_class_name: Tailwind class_name for the paragraph.
2727
"""
28+
from pathlib import Path
29+
2830
import reflex as rx
2931

3032
class UnusedState(rx.State):
@@ -35,10 +37,15 @@ def index():
3537
rx.chakra.text(paragraph_text, class_name=paragraph_class_name),
3638
rx.el.p(paragraph_text, class_name=paragraph_class_name),
3739
rx.text(paragraph_text, as_="p", class_name=paragraph_class_name),
40+
rx.el.div("Test external stylesheet", class_name="external"),
3841
id="p-content",
3942
)
4043

41-
app = rx.App(style={"font_family": "monospace"})
44+
assets = Path(__file__).resolve().parent.parent / "assets"
45+
assets.mkdir(exist_ok=True)
46+
stylesheet = assets / "test_styles.css"
47+
stylesheet.write_text(".external { color: rgba(0, 0, 255, 0.5) }")
48+
app = rx.App(style={"font_family": "monospace"}, stylesheets=[stylesheet.name])
4249
app.add_page(index)
4350
if tailwind_disabled:
4451
config = rx.config.get_config()
@@ -107,3 +114,9 @@ def test_tailwind_app(tailwind_app: AppHarness, tailwind_disabled: bool):
107114
else:
108115
# expect "text-red-500" from tailwind utility class
109116
assert p.value_of_css_property("color") in TEXT_RED_500_COLOR
117+
118+
# Assert external stylesheet is applying rules
119+
external = driver.find_elements(By.CLASS_NAME, "external")
120+
assert len(external) == 1
121+
for ext_div in external:
122+
assert ext_div.value_of_css_property("color") == "rgba(0, 0, 255, 0.5)"

reflex/compiler/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _compile_root_stylesheet(stylesheets: list[str]) -> str:
169169
raise FileNotFoundError(
170170
f"The stylesheet file {stylesheet_full_path} does not exist."
171171
)
172-
stylesheet = f"@/{stylesheet.strip('/')}"
172+
stylesheet = f"../{constants.Dirs.PUBLIC}/{stylesheet.strip('/')}"
173173
sheets.append(stylesheet) if stylesheet not in sheets else None
174174
return templates.STYLE.render(stylesheets=sheets)
175175

reflex/constants/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Dirs(SimpleNamespace):
2727
UTILS = "utils"
2828
# The name of the output static directory.
2929
STATIC = "_static"
30+
# The name of the public html directory served at "/"
31+
PUBLIC = "public"
3032
# The name of the state file.
3133
STATE_PATH = "/".join([UTILS, "state"])
3234
# The name of the components file.
@@ -40,7 +42,7 @@ class Dirs(SimpleNamespace):
4042
# The directory where the utils file is located.
4143
WEB_UTILS = os.path.join(WEB, UTILS)
4244
# The directory where the assets are located.
43-
WEB_ASSETS = os.path.join(WEB, "public")
45+
WEB_ASSETS = os.path.join(WEB, PUBLIC)
4446
# The env json file.
4547
ENV_JSON = os.path.join(WEB, "env.json")
4648
# The reflex json file.

reflex/constants/base.pyi

Lines changed: 0 additions & 99 deletions
This file was deleted.

tests/compiler/test_compiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_compile_stylesheets(tmp_path, mocker):
135135
f"@import url('./tailwind.css'); \n"
136136
f"@import url('https://fonts.googleapis.com/css?family=Sofia&effect=neon|outline|emboss|shadow-multiple'); \n"
137137
f"@import url('https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css'); \n"
138-
f"@import url('@/styles.css'); \n"
138+
f"@import url('../public/styles.css'); \n"
139139
f"@import url('https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css'); \n",
140140
)
141141

@@ -166,7 +166,7 @@ def test_compile_stylesheets_exclude_tailwind(tmp_path, mocker):
166166

167167
assert compiler.compile_root_stylesheet(stylesheets) == (
168168
os.path.join(".web", "styles", "styles.css"),
169-
"@import url('@/styles.css'); \n",
169+
"@import url('../public/styles.css'); \n",
170170
)
171171

172172

0 commit comments

Comments
 (0)