@@ -19,9 +19,11 @@ NextGen JavaScript ESM module support for Django.
1919Install the package and add it to your ` INSTALLED_APPS ` setting:
2020
2121``` bash
22- pip install django-esm
22+ pip install django-esm[whitenoise]
2323```
2424
25+ First, add ` django_esm ` to your ` INSTALLED_APPS ` settings:
26+
2527``` python
2628# settings.py
2729INSTALLED_APPS = [
@@ -31,25 +33,26 @@ INSTALLED_APPS = [
3133]
3234```
3335
34- Next, lets configure Django-ESM:
36+ Optionally: If you are using whitenoise you will need to modify your WSGI application.
3537
3638``` python
37- # settings.py
38- from pathlib import Path
39+ import os
40+ import pathlib
3941
40- # add BASE_DIR setting (if not already present)
41- BASE_DIR = Path(__file__ ).resolve().parent.parent
42+ from django.core.wsgi import get_wsgi_application
4243
43- ESM = {
44- " PACKAGE_DIR" : BASE_DIR , # path to a directory containing a package.json file
45- " STATIC_DIR" : BASE_DIR / " esm" , # target directory to collect ES modules into
46- " STATIC_PREFIX" : " esm" , # prefix for the ES module URLs
47- }
44+ from django_esm.wsgi import ESM
4845
49- STATICFILES_DIRS = [
50- # …
51- (ESM [" STATIC_PREFIX" ], ESM [" STATIC_DIR" ]),
52- ]
46+ BASE_DIR = pathlib.Path(__file__ ).parent.parent
47+
48+ os.environ.setdefault(" DJANGO_SETTINGS_MODULE" , " myproject.settings" )
49+
50+ application = get_wsgi_application()
51+ application = ESM(
52+ application,
53+ root = BASE_DIR / " staticfiles" / " esm" ,
54+ prefix = " esm" ,
55+ )
5356```
5457
5558Finally, add the import map to your base template:
@@ -108,23 +111,6 @@ You need to define your private modules in your `package.json` file:
108111}
109112```
110113
111- ### Testing (with Jest)
112-
113- You can use the ` django_esm ` package to test your JavaScript modules with Jest.
114- Jest v27.4 and upwards will honor ` imports ` in your ` package.json ` file.
115-
116- Before v27.4 that, you can try to use a custom ` moduleNameMapper ` , like so:
117-
118- ``` js
119- // jest.config.js
120- module .exports = {
121- // …
122- moduleNameMapper: {
123- ' ^#(.*)$' : ' <rootDir>/staticfiles/js/$1' // @todo: remove this with Jest >=29.4
124- },
125- }
126- ```
127-
128114## How it works
129115
130116Django ESM works via native JavaScript module support in modern browsers.
0 commit comments