https://github.com/forgepackages/forge-importmap
Use JS libraries in Django through import maps
https://github.com/forgepackages/forge-importmap
forgepackages
Last synced: 6 months ago
JSON representation
Use JS libraries in Django through import maps
- Host: GitHub
- URL: https://github.com/forgepackages/forge-importmap
- Owner: forgepackages
- License: mit
- Created: 2022-08-15T22:03:15.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-06-01T19:08:11.000Z (almost 3 years ago)
- Last Synced: 2024-11-30T01:31:22.841Z (over 1 year ago)
- Topics: forgepackages
- Language: Python
- Homepage: https://www.forgepackages.com/docs/forge-importmap
- Size: 96.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# forge-importmap
Heavily inspired by [rails/importmap-rails](https://github.com/rails/importmap-rails),
this app adds a simple process for integrating [import maps](https://github.com/WICG/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.
```python
# 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](https://jspm.org/docs/api#install), but is basically the `@` you want to use.
```toml
[[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:
```json
{
"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](https://github.com/guybedford/es-module-shims).
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").
```html
{% load importmap %}
{% importmap_scripts %}
import React from "react"
console.log(React);
```
When it renders you should get something like this:
```html
{
"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"
}
}
}
import React from "react"
console.log(React);
```
## 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](https://www.dependencies.io/) to update shim version
- Preload option
- Vendoring option (including shim)
- More complete error handling (custom exceptions, etc.)