Skip to content

Commit e5e35c0

Browse files
committed
Add useful WSGI middleware defaults to simpliy setup and development
1 parent bc6aa6e commit e5e35c0

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ BASE_DIR = pathlib.Path(__file__).parent.parent
4747

4848
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
4949

50-
application = get_wsgi_application()
51-
application = ESM(
52-
application,
53-
root=BASE_DIR / "staticfiles" / "esm",
54-
prefix="esm",
55-
)
50+
application = ESM(get_wsgi_application())
5651
```
5752

5853
Finally, add the import map to your base template:

django_esm/templatetags/esm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ def _resolve_importmap_urls(raw_importmap):
3737
@register.simple_tag
3838
def importmap():
3939
global importmap_json
40+
print(not importmap_json or settings.DEBUG)
4041
if not importmap_json or settings.DEBUG:
4142
with (
4243
pathlib.Path(conf.get_settings().STATIC_DIR) / "importmap.json"
4344
).open() as f:
4445
raw_importmap = json.load(f)
4546
importmap_json = _resolve_importmap_urls(raw_importmap)
47+
print(importmap_json)
4648
return mark_safe(importmap_json) # nosec

django_esm/wsgi.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,20 @@
22

33

44
class ESM(WhiteNoise):
5+
"""Lightweight WSGI ES module loader based on whitenoise."""
6+
57
def immutable_file_test(self, path, url):
68
return True
9+
10+
def __init__(self, *args, **kwargs):
11+
from django.conf import settings # noqa: F401
12+
13+
super().__init__(
14+
*args,
15+
**{
16+
"root": settings.BASE_DIR / "staticfiles" / "esm",
17+
"prefix": "esm",
18+
"autorefresh": settings.DEBUG,
19+
}
20+
| kwargs,
21+
)

0 commit comments

Comments
 (0)