Skip to main content

forge-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 forge-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 forge-importmap and add it to your INSTALLED_APPS list in your settings.py file.

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

2. Create an importmap.toml file

This should live next to your manage.py file. Here you'll add a list of "packages" you want to use.

The "name" can be anything, but should probably be the same as what it you would import from in typical bundling setups (i.e. import React from "react").

The "source" will get passed on to the jspm.org generator, but is basically the <npm package>@<version> you want to use.

[[packages]]
name = "react"
source = "react@17.0.2"

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>

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.)

Metadata

Release files for forge-importmap 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for forge-importmap 1.0.0
File Size Uploaded
forge_importmap-1.0.0.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for forge-importmap 1.0.0
File Interpreter ABI Platform
forge_importmap-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.7 kB

Release files / forge_importmap-1.0.0.tar.gz

Download URL forge_importmap-1.0.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4f5153536aa6a34110984ba8a79b59eb8ad6b36cfac1b1ef5de6077116b276c8
BLAKE2b-256 checksum
How to use checksums
5536d26c901de97c88dfcaf4dd4ad13a8feb5f38392f2ccd559145cdd42964b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.11.1 Linux/5.15.0-1031-azure

Release files / forge_importmap-1.0.0-py3-none-any.whl

Download URL forge_importmap-1.0.0-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb507f21569a75e860daeaa2dcfbcb7e5811136ff7b61a9a723abc4c79a6c38f
BLAKE2b-256 checksum
How to use checksums
62cb7eda21242d3c388bf11353a51de5159588392b0d298cb4aa9698341cbe82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.11.1 Linux/5.15.0-1031-azure

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page