importmap-rails 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1,18 @@
1
1
  module Importmap::ImportmapTagsHelper
2
2
  # Setup all script tags needed to use an importmap-powered entrypoint (which defaults to application.js)
3
- def javascript_importmap_tags(entry_point = "application")
3
+ def javascript_importmap_tags(entry_point = "application", shim: true)
4
4
  safe_join [
5
5
  javascript_inline_importmap_tag,
6
6
  javascript_importmap_module_preload_tags,
7
- javascript_importmap_shim_nonce_configuration_tag,
8
- javascript_importmap_shim_tag,
7
+ (javascript_importmap_shim_nonce_configuration_tag if shim),
8
+ (javascript_importmap_shim_tag if shim),
9
9
  javascript_import_module_tag(entry_point)
10
10
  ].compact, "\n"
11
11
  end
12
12
 
13
13
  # Generate an inline importmap tag using the passed `importmap_json` JSON string.
14
- # By default, `Rails.application.config.importmap.to_json(resolver: self)` is used.
15
- def javascript_inline_importmap_tag(importmap_json = Rails.application.config.importmap.to_json(resolver: self))
14
+ # By default, `Rails.application.importmap.to_json(resolver: self)` is used.
15
+ def javascript_inline_importmap_tag(importmap_json = Rails.application.importmap.to_json(resolver: self))
16
16
  tag.script importmap_json.html_safe,
17
17
  type: "importmap", "data-turbo-track": "reload", nonce: content_security_policy_nonce
18
18
  end
@@ -39,9 +39,9 @@ module Importmap::ImportmapTagsHelper
39
39
  end
40
40
 
41
41
  # Link tags for preloading all modules marked as preload: true in the `importmap`
42
- # (defaults to Rails.application.config.importmap), such that they'll be fetched
42
+ # (defaults to Rails.application.importmap), such that they'll be fetched
43
43
  # in advance by browsers supporting this link type (https://caniuse.com/?search=modulepreload).
44
- def javascript_importmap_module_preload_tags(importmap = Rails.application.config.importmap)
44
+ def javascript_importmap_module_preload_tags(importmap = Rails.application.importmap)
45
45
  javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self))
46
46
  end
47
47
 
@@ -43,7 +43,7 @@ class Importmap::Commands < Thor
43
43
 
44
44
  desc "json", "Show the full importmap in json"
45
45
  def json
46
- puts Rails.application.config.importmap.to_json(resolver: ActionController::Base.helpers)
46
+ puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers)
47
47
  end
48
48
 
49
49
  private
@@ -1,10 +1,19 @@
1
1
  require "importmap/map"
2
2
 
3
+ # Use Rails.application.importmap to access the map
4
+ Rails::Application.send(:attr_accessor, :importmap)
5
+
3
6
  module Importmap
4
7
  class Engine < ::Rails::Engine
5
- config.importmap = Importmap::Map.new.draw("config/importmap.rb")
8
+ config.importmap = ActiveSupport::OrderedOptions.new
9
+ config.importmap.sweep_cache = Rails.env.development? || Rails.env.test?
10
+
6
11
  config.autoload_once_paths = %W( #{root}/app/helpers )
7
12
 
13
+ initializer "importmap" do |app|
14
+ app.importmap = Importmap::Map.new.draw("config/importmap.rb")
15
+ end
16
+
8
17
  initializer "importmap.reloader" do |app|
9
18
  app.config.paths.add "config/importmap.rb"
10
19
 
@@ -15,6 +24,16 @@ module Importmap
15
24
  end
16
25
  end
17
26
 
27
+ initializer "importmap.cache_sweeper" do |app|
28
+ if app.config.importmap.sweep_cache
29
+ app.importmap.cache_sweeper watches: app.root.join("app/javascript")
30
+
31
+ ActiveSupport.on_load(:action_controller_base) do
32
+ before_action { Rails.application.importmap.cache_sweeper.execute_if_updated }
33
+ end
34
+ end
35
+ end
36
+
18
37
  initializer "importmap.assets" do
19
38
  if Rails.application.config.respond_to?(:assets)
20
39
  Rails.application.config.assets.precompile += %w( es-module-shims.js )
data/lib/importmap/map.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "pathname"
2
+ require "active_support/evented_file_update_checker"
2
3
 
3
4
  class Importmap::Map
4
5
  attr_reader :packages, :directories
@@ -50,12 +51,26 @@ class Importmap::Map
50
51
  # Example:
51
52
  #
52
53
  # class ApplicationController < ActionController::Base
53
- # etag { Rails.application.config.importmap.digest(resolver: helpers) if request.format&.html? }
54
+ # etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
54
55
  # end
55
56
  def digest(resolver:)
56
57
  Digest::SHA1.hexdigest(to_json(resolver: resolver).to_s)
57
58
  end
58
59
 
60
+ # Returns an instance ActiveSupport::EventedFileUpdateChecker configured to clear the cache of the map
61
+ # when the directories passed on initialization via `watches:` have changes. This is used in development
62
+ # and test to ensure the map caches are reset when javascript files are changed.
63
+ def cache_sweeper(watches: nil)
64
+ if watches
65
+ @cache_sweeper =
66
+ ActiveSupport::EventedFileUpdateChecker.new([], Array(watches).collect { |dir| [ dir, "js"] }.to_h) do
67
+ clear_cache
68
+ end
69
+ else
70
+ @cache_sweeper
71
+ end
72
+ end
73
+
59
74
  private
60
75
  MappedDir = Struct.new(:dir, :path, :under, :preload, keyword_init: true)
61
76
  MappedFile = Struct.new(:name, :path, :preload, keyword_init: true)
@@ -2,7 +2,7 @@ class Importmap::Reloader
2
2
  delegate :execute_if_updated, :execute, :updated?, to: :updater
3
3
 
4
4
  def reload!
5
- import_map_paths.each { |path| config.importmap.draw(path) }
5
+ import_map_paths.each { |path| Rails.application.importmap.draw(path) }
6
6
  end
7
7
 
8
8
  private
@@ -1,3 +1,3 @@
1
1
  module Importmap
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -14,8 +14,10 @@ create_file Rails.root.join("app/javascript/application.js") do <<-JS
14
14
  JS
15
15
  end
16
16
 
17
- say "Ensure JavaScript files are in the asset pipeline manifest"
18
- append_to_file Rails.root.join("app/assets/config/manifest.js"), %(//= link_tree ../../javascript .js\n)
17
+ if (sprockets_manifest_path = Rails.root.join("app/assets/config/manifest.js")).exist?
18
+ say "Ensure JavaScript files are in the Sprocket manifest"
19
+ append_to_file sprockets_manifest_path, %(//= link_tree ../../javascript .js\n)
20
+ end
19
21
 
20
22
  say "Configure importmap paths in config/importmap.rb"
21
23
  copy_file "#{__dir__}/config/importmap.rb", "config/importmap.rb"
@@ -7,6 +7,6 @@ namespace :importmap do
7
7
  desc "Show the importmap"
8
8
  task :pins do
9
9
  require Rails.root.join("config/environment")
10
- puts Rails.application.config.importmap.to_json(resolver: ActionController::Base.helpers)
10
+ puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers)
11
11
  end
12
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: importmap-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-17 00:00:00.000000000 Z
11
+ date: 2021-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -34,7 +34,6 @@ files:
34
34
  - README.md
35
35
  - Rakefile
36
36
  - app/assets/javascripts/es-module-shims.js
37
- - app/assets/javascripts/es-module-shims@0.14.0.csp.js
38
37
  - app/helpers/importmap/importmap_tags_helper.rb
39
38
  - lib/importmap-rails.rb
40
39
  - lib/importmap/commands.rb