Sha256: fb3eabb17f135e35889d4fa5f4f75a3200097b1dee8b032e5c6c79ed62a549b6

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module Importmap::ImportmapTagsHelper
  # Setup all script tags needed to use an importmap-powered entrypoint (which defaults to application.js)
  def javascript_importmap_tags(entry_point = "application")
    safe_join [
      javascript_inline_importmap_tag,
      javascript_importmap_shim_tag,
      javascript_import_module_tag(entry_point)
    ], "\n"
  end

  # Generate an inline importmap tag using the passed `importmap_paths` object to produce the JSON map.
  # By default, `Rails.application.config.importmap.paths` is used for this object,
  def javascript_inline_importmap_tag(importmap_paths = Rails.application.config.importmap.paths)
    tag.script(importmap_paths.to_json(self).html_safe, type: "importmap")
  end

  # Include the es-module-shim needed to make importmaps work in browsers without native support (like Firefox + Safari).
  def javascript_importmap_shim_tag
    javascript_include_tag "es-module-shims", async: true
  end

  # Import a named JavaScript module using a script-module tag.
  def javascript_import_module_tag(module_name)
    tag.script %(import "#{module_name}").html_safe, type: "module"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
importmap-rails-0.1.0 app/helpers/importmap/importmap_tags_helper.rb