Class: Importmap::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/importmap/map.rb

Defined Under Namespace

Classes: MappedDir, MappedFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMap

Returns a new instance of Map.



6
7
8
# File 'lib/importmap/map.rb', line 6

def initialize
  @packages, @directories = {}, {}
end

Instance Attribute Details

#directoriesObject (readonly)

Returns the value of attribute directories.



4
5
6
# File 'lib/importmap/map.rb', line 4

def directories
  @directories
end

#packagesObject (readonly)

Returns the value of attribute packages.



4
5
6
# File 'lib/importmap/map.rb', line 4

def packages
  @packages
end

Instance Method Details

#cache_sweeper(watches: nil) ⇒ Object

Returns an instance ActiveSupport::EventedFileUpdateChecker configured to clear the cache of the map when the directories passed on initialization via ‘watches:` have changes. This is used in development and test to ensure the map caches are reset when javascript files are changed.



62
63
64
65
66
67
68
69
70
71
# File 'lib/importmap/map.rb', line 62

def cache_sweeper(watches: nil)
  if watches
    @cache_sweeper =
      Rails.application.config.file_watcher.new([], Array(watches).collect { |dir| [ dir.to_s, "js"] }.to_h) do
        clear_cache
      end
  else
    @cache_sweeper
  end
end

#digest(resolver:) ⇒ Object

Returns a SHA1 digest of the import map json that can be used as a part of a page etag to ensure that a html cache is invalidated when the import map is changed.

Example:

class ApplicationController < ActionController::Base
  etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
end


55
56
57
# File 'lib/importmap/map.rb', line 55

def digest(resolver:)
  Digest::SHA1.hexdigest(to_json(resolver: resolver).to_s)
end

#draw(path = nil, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/importmap/map.rb', line 10

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: false) ⇒ Object



25
26
27
28
# File 'lib/importmap/map.rb', line 25

def pin(name, to: nil, preload: false)
  clear_cache
  @packages[name] = MappedFile.new(name: name, path: to || "#{name}.js", preload: preload)
end

#pin_all_from(dir, under: nil, to: nil, preload: false) ⇒ Object



30
31
32
33
# File 'lib/importmap/map.rb', line 30

def pin_all_from(dir, under: nil, to: nil, preload: false)
  clear_cache
  @directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
end

#preloaded_module_paths(resolver:) ⇒ Object



35
36
37
38
39
# File 'lib/importmap/map.rb', line 35

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:) ⇒ Object



41
42
43
44
45
# File 'lib/importmap/map.rb', line 41

def to_json(resolver:)
  cache_as(:json) do
    JSON.pretty_generate({ "imports" => resolve_asset_paths(expanded_packages_and_directories, resolver: resolver) })
  end
end