marten-importmap v0.1.2
MartenImportmap
Integration between Marten and the ImportMap shard.
Installation
Add the shard to your shard.yml:
dependencies:
marten_importmap:
github: treagod/marten-importmap
Run shards install, then add the two requires to your project:
# src/project.cr
require "marten"
require "marten_importmap"
# src/cli.cr (needed before running `marten importmap init`)
require "marten/cli"
require "marten_importmap/cli"
Then initialize the project:
marten importmap init
marten importmap init will:
- add
require "marten_importmap"tosrc/project.crif not present - add
require "marten_importmap/cli"tosrc/cli.crif the file exists and the require is missing - add
MartenImportmap::Apptoconfig.installed_apps - create
config/initializers/importmap.crfor user-managed importmap entries - create
config/initializers/importmap_pins.crfor CLI-managed pins - create
src/assets/application.jsif it does not exist
Template tag
Add {% importmap %} inside the <head> of your base template. It renders the <script type="importmap"> block and a <script type="module"> tag for the entrypoint:
<head>
<link rel="stylesheet" href="{% asset 'css/app.css' %}" />
{% importmap %}
</head>
Optional arguments:
{% importmap %} {# default namespace, entrypoint "application" #}
{% importmap "admin" %} {# custom namespace #}
{% importmap "admin" entrypoint="admin/app" %} {# custom namespace and entrypoint #}
Configuration
Marten.configure do |config|
# Default: false (download resolved modules into vendor_scripts_dir)
config.importmap.keep_cdn_urls = false
# Default: "src/assets/vendor/"
config.importmap.vendor_scripts_dir = "src/assets/vendor/"
config.importmap.draw do
pin "application", "application.js"
end
end
Use config/initializers/importmap.cr for manual pins, namespaces, and pin_all_from directives.
CLI
After initialization, pin a library using JSPM:
marten importmap pin react
marten importmap pin react --from jsdelivr
Supported providers: jspm.io (default), jsdelivr, unpkg, skypack, esm.sh.
The command downloads the resolved module to src/assets/vendor/ and updates config/initializers/importmap_pins.cr:
# AUTO-GENERATED by `marten importmap pin ...`
# DO NOT EDIT.
Marten.configure do |config|
config.importmap.draw do
pin "react", "vendor/react.js"
end
end
config/initializers/importmap_pins.cr is CLI-managed. config/initializers/importmap.cr remains user-managed.
marten-importmap
- 0
- 0
- 0
- 1
- 4
- 7 days ago
- February 5, 2026
MIT License
Thu, 26 Mar 2026 09:30:17 GMT