Skip to main content

JavaScript import maps for Plain.

Project description

plain.importmap

Use import maps in templates.

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 plain-importmap.

The TL;DR is:

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

1. Install it

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

# settings.py
INSTALLED_PACKAGES = [
    ...
    "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_js %} 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_js %}
    <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.)

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

plain_importmap-0.3.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

plain_importmap-0.3.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: plain_importmap-0.3.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for plain_importmap-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6cf66638f47479386342f9837f3d95d7cedeb150a7710a723e8806ee21391588
MD5 205a3f36fca2e3012e3dd2fdea8f8de9
BLAKE2b-256 015070be22d2b9b206dc637b80f69a45a2d3b2c678e79085816db375562ef29c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: plain_importmap-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for plain_importmap-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e1a82088ffd28449f4356220f61b36eb306ac100d73905ead42f1c8f07391a28
MD5 61f1796676461f8b503ea038b7b1f81b
BLAKE2b-256 b8f4543edfaf80aa3446e3485620aafbbf7117742f4cd22793834e7c1d03a403

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