class Importmap::Map

Constants

MappedDir
MappedFile

Attributes

cached[RW]
directories[R]
packages[R]

Public Class Methods

new() click to toggle source
# File lib/importmap/map.rb, line 7
def initialize
  @packages, @directories = {}, {}
end

Public Instance Methods

draw(path = nil, &block) click to toggle source
# File lib/importmap/map.rb, line 11
def draw(path = nil, &block)
  if path && File.exist?(path)
    begin
      instance_eval(File.read(path), path.to_s)
    rescue Exception => e
      Rails.logger.error "Unable to parse import map from #{path}: #{e.message}"
      raise "Unable to parse import map from #{path}: #{e.message}"
    end
  elsif block_given?
    instance_eval(&block)
  end

  self
end
pin(name, to: nil, preload: true) click to toggle source
# File lib/importmap/map.rb, line 26
def pin(name, to: nil, preload: true)
  @packages[name] = MappedFile.new(name: name, path: to || "#{name}.js", preload: preload)
end
pin_all_from(dir, under: nil, to: nil, preload: true) click to toggle source
# File lib/importmap/map.rb, line 30
def pin_all_from(dir, under: nil, to: nil, preload: true)
  @directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
end
preloaded_module_paths(resolver:) click to toggle source
# File lib/importmap/map.rb, line 34
def preloaded_module_paths(resolver:)
  cache_as(:preloaded_module_paths) do
    resolve_asset_paths(expanded_preloading_packages_and_directories, resolver: resolver).values
  end
end
to_json(resolver:) click to toggle source
# File lib/importmap/map.rb, line 40
def to_json(resolver:)
  cache_as(:json) do
    JSON.pretty_generate({ "imports" => resolve_asset_paths(expanded_packages_and_directories, resolver: resolver) })
  end
end

Private Instance Methods

absolute_root_of(path) click to toggle source
# File lib/importmap/map.rb, line 103
def absolute_root_of(path)
  (pathname = Pathname.new(path)).absolute? ? pathname : Rails.root.join(path)
end
cache_as(name) { || ... } click to toggle source
# File lib/importmap/map.rb, line 50
def cache_as(name)
  if (cached && result = instance_variable_get("@cached_#{name}"))
    result
  else
    instance_variable_set("@cached_#{name}", yield)
  end
end
expand_directories_into(paths) click to toggle source
# File lib/importmap/map.rb, line 77
def expand_directories_into(paths)
  @directories.values.each do |mapping|
    if (absolute_path = absolute_root_of(mapping.dir)).exist?
      find_javascript_files_in_tree(absolute_path).each do |filename|
        module_filename = filename.relative_path_from(absolute_path)
        module_name     = module_name_from(module_filename, mapping)
        module_path     = module_path_from(module_filename, mapping)

        paths[module_name] = MappedFile.new(name: module_name, path: module_path, preload: mapping.preload)
      end
    end
  end
end
expanded_packages_and_directories() click to toggle source
# File lib/importmap/map.rb, line 73
def expanded_packages_and_directories
  @packages.dup.tap { |expanded| expand_directories_into expanded }
end
expanded_preloading_packages_and_directories() click to toggle source
# File lib/importmap/map.rb, line 69
def expanded_preloading_packages_and_directories
  expanded_packages_and_directories.select { |name, mapping| mapping.preload }
end
find_javascript_files_in_tree(path) click to toggle source
# File lib/importmap/map.rb, line 99
def find_javascript_files_in_tree(path)
  Dir[path.join("**/*.js{,m}")].collect { |file| Pathname.new(file) }.select(&:file?)
end
module_name_from(filename, mapping) click to toggle source
# File lib/importmap/map.rb, line 91
def module_name_from(filename, mapping)
  [ mapping.under, filename.to_s.remove(filename.extname).remove(/\/?index$/).presence ].compact.join("/")
end
module_path_from(filename, mapping) click to toggle source
# File lib/importmap/map.rb, line 95
def module_path_from(filename, mapping)
  [ mapping.path || mapping.under, filename.to_s ].compact.join("/")
end
resolve_asset_paths(paths, resolver:) click to toggle source
# File lib/importmap/map.rb, line 58
def resolve_asset_paths(paths, resolver:)
  paths.transform_values do |mapping|
    begin
      resolver.asset_path(mapping.path)
    rescue Sprockets::Rails::Helper::AssetNotFound
      Rails.logger.warn "Importmap skipped missing path: #{mapping.path}"
      nil
    end
  end.compact
end