importmap-rails 0.5.3 → 0.6.3

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,11 +1,11 @@
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
@@ -20,13 +20,15 @@ module Importmap::ImportmapTagsHelper
20
20
  # Configure es-modules-shim with nonce support if the application is using a content security policy.
21
21
  def javascript_importmap_shim_nonce_configuration_tag
22
22
  if content_security_policy?
23
- tag.script({ nonce: content_security_policy_nonce }.to_json.html_safe, type: "esms-options")
23
+ tag.script({ nonce: content_security_policy_nonce }.to_json.html_safe,
24
+ type: "esms-options", nonce: content_security_policy_nonce)
24
25
  end
25
26
  end
26
27
 
27
28
  # Include the es-modules-shim needed to make importmaps work in browsers without native support (like Firefox + Safari).
28
29
  def javascript_importmap_shim_tag
29
- javascript_include_tag "es-module-shims", async: true, "data-turbo-track": "reload"
30
+ javascript_include_tag "es-module-shims", async: true, "data-turbo-track": "reload",
31
+ nonce: content_security_policy_nonce
30
32
  end
31
33
 
32
34
  # Import a named JavaScript module(s) using a script-module tag.
@@ -45,6 +47,8 @@ module Importmap::ImportmapTagsHelper
45
47
 
46
48
  # Link tag(s) for preloading the JavaScript module residing in `*paths`. Will return one link tag per path element.
47
49
  def javascript_module_preload_tag(*paths)
48
- safe_join(Array(paths).collect { |path| tag.link rel: "modulepreload", href: path }, "\n")
50
+ safe_join(Array(paths).collect { |path|
51
+ tag.link rel: "modulepreload", href: path, nonce: content_security_policy_nonce
52
+ }, "\n")
49
53
  end
50
54
  end
@@ -27,11 +27,5 @@ module Importmap
27
27
  helper Importmap::ImportmapTagsHelper
28
28
  end
29
29
  end
30
-
31
- initializer "importmap.caching" do |app|
32
- if Rails.application.config.importmap.cached.nil?
33
- Rails.application.config.importmap.cached = app.config.action_controller.perform_caching
34
- end
35
- end
36
30
  end
37
31
  end
data/lib/importmap/map.rb CHANGED
@@ -2,7 +2,6 @@ require "pathname"
2
2
 
3
3
  class Importmap::Map
4
4
  attr_reader :packages, :directories
5
- attr_accessor :cached
6
5
 
7
6
  def initialize
8
7
  @packages, @directories = {}, {}
@@ -24,10 +23,12 @@ class Importmap::Map
24
23
  end
25
24
 
26
25
  def pin(name, to: nil, preload: true)
26
+ clear_cache
27
27
  @packages[name] = MappedFile.new(name: name, path: to || "#{name}.js", preload: preload)
28
28
  end
29
29
 
30
30
  def pin_all_from(dir, under: nil, to: nil, preload: true)
31
+ clear_cache
31
32
  @directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
32
33
  end
33
34
 
@@ -43,18 +44,35 @@ class Importmap::Map
43
44
  end
44
45
  end
45
46
 
47
+ # Returns a SHA1 digest of the import map json that can be used as a part of a page etag to
48
+ # ensure that a html cache is invalidated when the import map is changed.
49
+ #
50
+ # Example:
51
+ #
52
+ # class ApplicationController < ActionController::Base
53
+ # etag { Rails.application.config.importmap.digest(resolver: helpers) if request.format&.html? }
54
+ # end
55
+ def digest(resolver:)
56
+ Digest::SHA1.hexdigest(to_json(resolver: resolver).to_s)
57
+ end
58
+
46
59
  private
47
60
  MappedDir = Struct.new(:dir, :path, :under, :preload, keyword_init: true)
48
61
  MappedFile = Struct.new(:name, :path, :preload, keyword_init: true)
49
62
 
50
63
  def cache_as(name)
51
- if (cached && result = instance_variable_get("@cached_#{name}"))
64
+ if result = instance_variable_get("@cached_#{name}")
52
65
  result
53
66
  else
54
67
  instance_variable_set("@cached_#{name}", yield)
55
68
  end
56
69
  end
57
70
 
71
+ def clear_cache
72
+ @cached_json = nil
73
+ @cached_preloaded_module_paths = nil
74
+ end
75
+
58
76
  def resolve_asset_paths(paths, resolver:)
59
77
  paths.transform_values do |mapping|
60
78
  begin
@@ -1,3 +1,3 @@
1
1
  module Importmap
2
- VERSION = "0.5.3"
2
+ VERSION = "0.6.3"
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"
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.5.3
4
+ version: 0.6.3
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-15 00:00:00.000000000 Z
11
+ date: 2021-09-19 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