# 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 `<npm package>@<version>` 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 %}
<!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:
```html
<!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](https://www.dependencies.io/) to update shim version
- Preload option
- Vendoring option (including shim)
- More complete error handling (custom exceptions, etc.)
Raw data
{
"_id": null,
"home_page": "https://github.com/forgepackages/forge-importmap",
"name": "forge-importmap",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "django,javascript,import,map,import-maps",
"author": "Dave Gaeddert",
"author_email": "dave.gaeddert@dropseed.dev",
"download_url": "https://files.pythonhosted.org/packages/55/36/d26c901de97c88dfcaf4dd4ad13a8feb5f38392f2ccd559145cdd42964b3/forge_importmap-1.0.0.tar.gz",
"platform": null,
"description": "# forge-importmap\n\nHeavily inspired by [rails/importmap-rails](https://github.com/rails/importmap-rails),\nthis app adds a simple process for integrating [import maps](https://github.com/WICG/import-maps) into Django.\n\nThis is a new project and it hasn't been used in production yet.\nBut if you're looking to use import maps with Django, give it a try and tell us how it goes.\nThe structure (and code) is pretty simple.\nContributions are welcome!\n\n## How to use it\n\nYou'll need to do four things to use forge-importmap.\n\nThe TL;DR is:\n\n- Add \"importmap\" to `INSTALLED_APPS`\n- Create an `importmap.toml`\n- Run `python manage.py importmap_generate`\n- Use `{% importmap_scripts %}` in your template\n\n### 1. Install it\n\nDo the equivalent of `pip install forge-importmap` and add it to your `INSTALLED_APPS` list in your `settings.py` file.\n\n```python\n# settings.py\nINSTALLED_APPS = [\n ...\n \"importmap\",\n]\n```\n\n### 2. Create an `importmap.toml` file\n\nThis should live next to your `manage.py` file.\nHere you'll add a list of \"packages\" you want to use.\n\nThe \"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\"`).\n\nThe \"source\" will get passed on to the [jspm.org generator](https://jspm.org/docs/api#install), but is basically the `<npm package>@<version>` you want to use.\n\n```toml\n[[packages]]\nname = \"react\"\nsource = \"react@17.0.2\"\n```\n\n### 3. Run `importmap_generate`\n\nTo resolve the import map, you'll need to run `python manage.py importmap_generate`.\n\nThis 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).\n\nYou don't need to look at this file yourself, but here is an example of what it will contain:\n\n```json\n{\n \"config_hash\": \"09d6237cdd891aad07de60f54689d130\",\n \"importmap\": {\n \"imports\": {\n \"react\": \"https://ga.jspm.io/npm:react@17.0.2/index.js\"\n },\n \"scopes\": {\n \"https://ga.jspm.io/\": {\n \"object-assign\": \"https://ga.jspm.io/npm:object-assign@4.1.1/index.js\"\n }\n }\n },\n \"importmap_dev\": {\n \"imports\": {\n \"react\": \"https://ga.jspm.io/npm:react@17.0.2/dev.index.js\"\n },\n \"scopes\": {\n \"https://ga.jspm.io/\": {\n \"object-assign\": \"https://ga.jspm.io/npm:object-assign@4.1.1/index.js\"\n }\n }\n }\n}\n```\n\n### 4. Add the scripts to your template\n\nThe 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).\n\nAfter that, you can include your own JavaScript!\nThis could be inline or from `static`.\nJust be sure to use `type=\"module\"` and the \"name\" you provided when doing your JS imports (i.e. \"react\").\n\n```html\n{% load importmap %}\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n {% importmap_scripts %}\n <script type=\"module\">\n import React from \"react\"\n\n console.log(React);\n </script>\n</head>\n<body>\n\n</body>\n</html>\n```\n\nWhen it renders you should get something like this:\n\n```html\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <script async src=\"https://ga.jspm.io/npm:es-module-shims@1.3.6/dist/es-module-shims.js\"></script>\n <script type=\"importmap\">\n {\n \"imports\": {\n \"react\": \"https://ga.jspm.io/npm:react@17.0.2/dev.index.js\"\n },\n \"scopes\": {\n \"https://ga.jspm.io/\": {\n \"object-assign\": \"https://ga.jspm.io/npm:object-assign@4.1.1/index.js\"\n }\n }\n }\n </script>\n\n <script type=\"module\">\n import React from \"react\"\n\n console.log(React);\n </script>\n</head>\n<body>\n\n</body>\n</html>\n```\n\n## Project status\n\nThis is partly an experiment,\nbut honestly it's so simple that I don't think there can be much wrong with how it works currently.\n\nHere's a list of things that would be nice to do (PRs welcome):\n\n- Command to add new importmap dependency (use `^` version automatically?)\n- Django check for comparing lock and config (at deploy time, etc.)\n- Use [deps](https://www.dependencies.io/) to update shim version\n- Preload option\n- Vendoring option (including shim)\n- More complete error handling (custom exceptions, etc.)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "JavaScript import maps for Django",
"version": "1.0.0",
"split_keywords": [
"django",
"javascript",
"import",
"map",
"import-maps"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "62cb7eda21242d3c388bf11353a51de5159588392b0d298cb4aa9698341cbe82",
"md5": "b46c31bf19d0e45727b7ac6c710b49f1",
"sha256": "fb507f21569a75e860daeaa2dcfbcb7e5811136ff7b61a9a723abc4c79a6c38f"
},
"downloads": -1,
"filename": "forge_importmap-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b46c31bf19d0e45727b7ac6c710b49f1",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 7250,
"upload_time": "2023-01-20T03:23:18",
"upload_time_iso_8601": "2023-01-20T03:23:18.149795Z",
"url": "https://files.pythonhosted.org/packages/62/cb/7eda21242d3c388bf11353a51de5159588392b0d298cb4aa9698341cbe82/forge_importmap-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5536d26c901de97c88dfcaf4dd4ad13a8feb5f38392f2ccd559145cdd42964b3",
"md5": "a04ab013e53f36cd3d31febcc8b7f4f7",
"sha256": "4f5153536aa6a34110984ba8a79b59eb8ad6b36cfac1b1ef5de6077116b276c8"
},
"downloads": -1,
"filename": "forge_importmap-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "a04ab013e53f36cd3d31febcc8b7f4f7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 6470,
"upload_time": "2023-01-20T03:23:19",
"upload_time_iso_8601": "2023-01-20T03:23:19.912965Z",
"url": "https://files.pythonhosted.org/packages/55/36/d26c901de97c88dfcaf4dd4ad13a8feb5f38392f2ccd559145cdd42964b3/forge_importmap-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-20 03:23:19",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "forgepackages",
"github_project": "forge-importmap",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "forge-importmap"
}