Skip to main content

JavaScript import maps for Django

Project description

django-importmap

Heavily inspired by rails/importmap-rails, this app adds a simple process for integrating import maps into Django.

This is a new project and it hasn't been used in production yet. But if you're looking to use import maps with Django, give it a try and tell us how it goes. The structure (and code) is pretty simple. Contributions are welcome!

How to use it

You'll need to do four things to use django-importmap.

The TL;DR is:

  • Add "importmap" to INSTALLED_APPS
  • Create an importmap.toml
  • Run python manage.py importmap_generate
  • Use {% importmap_scripts %} in your template

1. Install it

Do the equivalent of pip install django-importmap and add it to your INSTALLED_APPS list in your settings.py file.

# settings.py
INSTALLED_APPS = [
    ...
    "importmap",
]

2. Configuring an import map

You JavaScript dependencies are conveniently located in yourpyproject.toml file.

They are listed under [tool.importmap.dependencies] and you can add them there. The format is name = "version", similar to how you would add a dependency to your package.json file.

# pyproject.toml
[tool.importmap.dependencies]
react = "17.0.2"
react-dom = "17.0.2"

jspm.org generator is used lock and serve the dependencies, but is basically just like installing them via npm i <npm package>@<version>.

3. Run importmap_generate

To resolve the import map, you'll need to run python manage.py importmap_generate.

This will create importmap.lock (which you should save and commit to your repo) that contains the actual import map JSON (both for development and production).

You don't need to look at this file yourself, but here is an example of what it will contain:

{
  "config_hash": "09d6237cdd891aad07de60f54689d130",
  "importmap": {
    "imports": {
      "react": "https://ga.jspm.io/npm:react@17.0.2/index.js"
    },
    "scopes": {
      "https://ga.jspm.io/": {
        "object-assign": "https://ga.jspm.io/npm:object-assign@4.1.1/index.js"
      }
    }
  },
  "importmap_dev": {
    "imports": {
      "react": "https://ga.jspm.io/npm:react@17.0.2/dev.index.js"
    },
    "scopes": {
      "https://ga.jspm.io/": {
        "object-assign": "https://ga.jspm.io/npm:object-assign@4.1.1/index.js"
      }
    }
  }
}

4. Add the scripts to your template

The import map itself gets added by using {% load importmap %} and then {% importmap_scripts %} in the head of your HTML. This will include the es-module-shim.

After that, you can include your own JavaScript! This could be inline or from static. Just be sure to use type="module" and the "name" you provided when doing your JS imports (i.e. "react").

{% load importmap %}
<!DOCTYPE html>
<html lang="en">
<head>
    {% importmap_scripts %}
    <script type="module">
        import React from "react"

        console.log(React);
    </script>
</head>
<body>

</body>
</html>

When it renders you should get something like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <script async src="https://ga.jspm.io/npm:es-module-shims@1.3.6/dist/es-module-shims.js"></script>
    <script type="importmap">
    {
        "imports": {
            "react": "https://ga.jspm.io/npm:react@17.0.2/dev.index.js"
        },
        "scopes": {
            "https://ga.jspm.io/": {
                "object-assign": "https://ga.jspm.io/npm:object-assign@4.1.1/index.js"
            }
        }
    }
    </script>

    <script type="module">
        import React from "react"

        console.log(React);
    </script>
</head>
<body>

</body>
</html>

Adding static files to import maps

You can include your own static files in the import map by passing kwargs to the {% importmap_scripts %} tag. You can actually use this to include any additional imports, but by using {% static "name" as name_static %} you can get the URL to the static file.

{% load importmap static %}
<!DOCTYPE html>
<html lang="en">
<head>
    {% static "my-script.js" as my_script_static %}
    {% importmap_scripts myscript=my_script_static %}
    <script type="module">
        import MyScript from "myscript"
    </script>
</head>
<body>

</body>
</html>

Using Jinja2

To use django-importmap with Jinja2 templates, you can add importmap to a customized Jinja environment.

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.jinja2.Jinja2",
        ...
        "OPTIONS": {
            "environment": "app.jinja2.environment",
            ...
        },
    }
]

Then in app/jinja2.py:

from django.conf import settings
from jinja2 import Environment

from importmap import Importmap


def environment(**options):
    env = Environment(**options)
    env.globals.update({"importmap": Importmap.json(development=settings.DEBUG)})
    return env

Then in your Jinja templates you can include a module shim and output the importmap variable like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <script async src="https://ga.jspm.io/npm:es-module-shims@1.3.6/dist/es-module-shims.js"></script>
    <script type="importmap">
    {{ importmap|safe }}
    </script>
    <script type="module">
        import React from "react"
        console.log(React);
    </script>
</head>
<body>

</body>
</html>

To include your own static files in the import map, you can pass a dictionary of names and URLs to the Importmap.json method:

from django.conf import settings
from django.templatetags.static import static
from jinja2 import Environment

from importmap import Importmap


def environment(**options):
    env = Environment(**options)
    env.globals.update(
        {
            "importmap": Importmap.json(
                development=settings.DEBUG, extra_imports={"myjs": static("myjs.js")}
            )
        }
    )
    return env

Project status

This is partly an experiment, but honestly it's so simple that I don't think there can be much wrong with how it works currently.

Here's a list of things that would be nice to do (PRs welcome):

  • Command to add new importmap dependency (use ^ version automatically?)
  • Django check for comparing lock and config (at deploy time, etc.)
  • Use deps to update shim version
  • Preload option
  • Vendoring option (including shim)
  • More complete error handling (custom exceptions, etc.)

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_importmap-0.3.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

django_importmap-0.3.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file django_importmap-0.3.0.tar.gz.

File metadata

  • Download URL: django_importmap-0.3.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.0 Linux/6.2.0-1016-azure

File hashes

Hashes for django_importmap-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e65fce1735bec7ca4ee0c21fb72ce29b0028dddd1e9a6e3faa5083e2b9baeb92
MD5 227ea1024b84e320873f15945e547dc6
BLAKE2b-256 494a7315cc335e65d54c287bc8dd79ded27a5b8631c04568d3c6c9a9b370e684

See more details on using hashes here.

File details

Details for the file django_importmap-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: django_importmap-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.0 Linux/6.2.0-1016-azure

File hashes

Hashes for django_importmap-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5bfe2e1f4b45a3fb0dce1c81572fa52bc460b0947450b859c40f58271c16e009
MD5 8711517fb303c01c9cd6cdc4cb2e0f53
BLAKE2b-256 5dd89db7092e9661a9fafa702392647943caaa51505cf865ed97bfb114c72c41

See more details on using hashes here.

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