Skip to main content

Lightweight JavaScript ESM module loader for Django.

Project description

Django ESM

NextGen JavaScript ESM module support for Django.

PyPi Version Test Coverage GitHub License

Highlights

  • easy transition
  • smart cache busting
  • no more bundling
  • native ESM support
  • local vendoring with npm

Setup

Install the package and add it to your INSTALLED_APPS setting:

pip install django-esm
# settings.py
INSTALLED_APPS = [
    # …
    'django_esm',
]

Next, add a new staticfiles finder to your STATICFILES_FINDERS setting:

# settings.py
STATICFILES_FINDERS = [
    # Django's default finders
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    # django-esm finder
    "django_esm.finders.ESMFinder",
]

You will also need to expose your node_modules directory to Django's staticfiles finder. You may run npm ci --omit=dev prior to running collectstatic to avoid exposing your devDependencies publicly.

# settings.py
from pathlib import Path

# add BASE_DIR (if not already present)
BASE_DIR = Path(__file__).resolve().parent.parent

STATICFILES_DIRS = [
    # …
    BASE_DIR / "node_modules",
]

Finally, add the import map to your base template:

<!-- base.html -->
<!DOCTYPE html>
{% load esm %}
<html lang="en">
<head>
  <script type="importmap">{% importmap %}</script>
</head>
</html>

That's it! Don't forget to run npm install and python manage.py collectstatic.

Usage

You can now import JavaScript modules in your Django templates:

<!-- index.html -->
{% block content %}
  <script type="module">
    import "htmx.org"
    htmx.logAll()
  </script>
{% endblock %}

Private modules

You can also import private modules from your Django app:

<!-- index.html -->
{% block content %}
  <script type="module">
    import "#myapp/js/my-module.js"
  </script>
{% endblock %}

To import a private module, prefix the module name with #. You need to define your private modules in your package.json file:

{
  "imports": {
    "#myapp/script": "./myapp/static/js/script.js",
    // You may use trailing stars to import all files in a directory.
    "#myapp/*": "./myapp/static/js/*"
  }
}

Testing (with Jest)

You can use the django_esm package to test your JavaScript modules with Jest. Jest v27.4 and upwards will honor imports in your package.json file.

Before v27.4 that, you can try to use a custom moduleNameMapper, like so:

// jest.config.js
module.exports = {
  // …
  moduleNameMapper: {
    '^#(.*)$': '<rootDir>/staticfiles/js/$1' // @todo: remove this with Jest >=29.4
  },
}

How it works

Django ESM works via native JavaScript module support in modern browsers. It uses the import map to map module names to their location on the server.

Here is an example import map:

{
  "imports": {
    "htmx.org": "/static/htmx.org/dist/htmx.min.js"
  }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_esm-0.4.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

django_esm-0.4.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file django_esm-0.4.1.tar.gz.

File metadata

  • Download URL: django_esm-0.4.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for django_esm-0.4.1.tar.gz
Algorithm Hash digest
SHA256 85491c304bcceb5f39adf16e121d2f5dbcf85380054a3d8a400c5a25b158ab37
MD5 8209d705bc2fd51ad13b04e5adec3731
BLAKE2b-256 a255b95a33f863cd3491e2b8d2cfe6ef6d302b02fbc3331a88a20358af73b788

See more details on using hashes here.

Provenance

File details

Details for the file django_esm-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: django_esm-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for django_esm-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bcaa161f4e1a2bda76ec750e594f2beec62b12eb4a51f2ab0db62233b6b00c25
MD5 cf10687b002abe35edd73605c6322913
BLAKE2b-256 53929d6d1026a491c68bf3df2ae9a84f49f4bd7d97dda1ef6018874a8e41bafa

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page