Skip to content

Commit 387c180

Browse files
committed
fix brittle component_from_npm tests
1 parent 986ea79 commit 387c180

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/reactpy/reactjs/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def component_from_npm(
111111
resolve_imports_depth: int = ...,
112112
version: str = "latest",
113113
cdn: str = "https://esm.sh/v135",
114+
bundle: bool = ...,
114115
fallback: Any | None = ...,
115116
unmount_before_update: bool = ...,
116117
allow_children: bool = ...,
@@ -125,6 +126,7 @@ def component_from_npm(
125126
resolve_imports_depth: int = ...,
126127
version: str = "latest",
127128
cdn: str = "https://esm.sh/v135",
129+
bundle: bool = ...,
128130
fallback: Any | None = ...,
129131
unmount_before_update: bool = ...,
130132
allow_children: bool = ...,
@@ -138,6 +140,7 @@ def component_from_npm(
138140
resolve_imports_depth: int = 5,
139141
version: str = "latest",
140142
cdn: str = "https://esm.sh/v135",
143+
bundle: bool = True,
141144
fallback: Any | None = None,
142145
unmount_before_update: bool = False,
143146
allow_children: bool = True,
@@ -162,6 +165,13 @@ def component_from_npm(
162165
The version of the package to use. Defaults to "latest".
163166
cdn:
164167
The CDN to use. Defaults to "https://esm.sh".
168+
bundle:
169+
Whether to ask the CDN (e.g. esm.sh) to bundle the package's dependencies
170+
into a single module. Defaults to ``True`` for faster loads in the common
171+
case, but some packages (e.g. MUI v7) ship sub-paths that esm.sh's bundle
172+
mode fails to rewrite correctly. Set this to ``False`` to fall back to
173+
esm.sh's per-module resolution, which is slower but more robust to
174+
package-specific bundling quirks.
165175
fallback:
166176
What to temporarily display while the module is being loaded.
167177
unmount_before_update:
@@ -176,7 +186,10 @@ def component_from_npm(
176186

177187
if "esm.sh" in cdn:
178188
url += "&" if "?" in url else "?"
179-
url += "external=react,react-dom,react/jsx-runtime&bundle&target=es2020"
189+
url += "external=react,react-dom,react/jsx-runtime"
190+
if bundle:
191+
url += "&bundle"
192+
url += "&target=es2020"
180193

181194
return component_from_url(
182195
url,

tests/test_reactjs/test_modules_from_npm.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ def App():
8787

8888

8989
async def test_component_from_npm_material_ui(display: DisplayFixture):
90-
Button = component_from_npm("@mui/material", "Button", version="7")
90+
# MUI v7 ships sub-paths (e.g. `@mui/styled-engine/esm/GlobalStyles/index`) that
91+
# esm.sh's ``bundle`` mode fails to rewrite into a working URL. Disabling the
92+
# bundle forces per-module resolution, which esm.sh handles correctly for MUI v7.
93+
Button = component_from_npm("@mui/material", "Button", version="7", bundle=False)
9194

9295
@reactpy.component
9396
def App():
@@ -271,7 +274,10 @@ def App():
271274

272275
async def test_interleaved_npm_and_server_components(display: DisplayFixture):
273276
Card = component_from_npm("antd", "Card", version="6")
274-
Button = component_from_npm("@mui/material", "Button", version="7")
277+
# MUI v7 ships sub-paths that esm.sh's ``bundle`` mode fails to rewrite into a
278+
# working URL. Disabling the bundle forces per-module resolution, which esm.sh
279+
# handles correctly for MUI v7.
280+
Button = component_from_npm("@mui/material", "Button", version="7", bundle=False)
275281

276282
@reactpy.component
277283
def App():
@@ -302,10 +308,14 @@ def App():
302308

303309

304310
async def test_complex_nested_material_ui(display: DisplayFixture):
311+
# MUI v7 ships sub-paths that esm.sh's ``bundle`` mode fails to rewrite into a
312+
# working URL. Disabling the bundle forces per-module resolution, which esm.sh
313+
# handles correctly for MUI v7.
305314
mui_components = component_from_npm(
306315
"@mui/material",
307316
["Button", "Card", "CardContent", "Typography", "Box", "Stack"],
308317
version="7",
318+
bundle=False,
309319
)
310320
Button, Card, CardContent, Typography, Box, Stack = mui_components
311321

0 commit comments

Comments
 (0)