lib/importmap/map.rb in importmap-rails-0.6.3 vs lib/importmap/map.rb in importmap-rails-0.7.0
- old
+ new
@@ -1,6 +1,7 @@
require "pathname"
+require "active_support/evented_file_update_checker"
class Importmap::Map
attr_reader :packages, :directories
def initialize
@@ -48,13 +49,27 @@
# ensure that a html cache is invalidated when the import map is changed.
#
# Example:
#
# class ApplicationController < ActionController::Base
- # etag { Rails.application.config.importmap.digest(resolver: helpers) if request.format&.html? }
+ # etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
# end
def digest(resolver:)
Digest::SHA1.hexdigest(to_json(resolver: resolver).to_s)
+ end
+
+ # 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.
+ def cache_sweeper(watches: nil)
+ if watches
+ @cache_sweeper =
+ ActiveSupport::EventedFileUpdateChecker.new([], Array(watches).collect { |dir| [ dir, "js"] }.to_h) do
+ clear_cache
+ end
+ else
+ @cache_sweeper
+ end
end
private
MappedDir = Struct.new(:dir, :path, :under, :preload, keyword_init: true)
MappedFile = Struct.new(:name, :path, :preload, keyword_init: true)