marten_importmap

Version, currently 0.1.03 versions

github.com/treagod/marten-importmap

No description declared in shard.yml.

4 stars
1 dependent
License: MIT

Nothing has been indexed for 0.1.0 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  marten_importmap:
    github: treagod/marten-importmap
    version: ~> 0.1.0

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.1.0. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

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" to src/project.cr if not present
  • add require "marten_importmap/cli" to src/cli.cr if the file exists and the require is missing
  • add MartenImportmap::App to config.installed_apps
  • create config/initializers/importmap.cr for user-managed importmap entries
  • create config/initializers/importmap_pins.cr for CLI-managed pins
  • create src/assets/application.js if 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.