importmap-rails 0.5.2 → 0.6.2

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,12 +1,13 @@
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_tag,
7
+ (javascript_importmap_shim_nonce_configuration_tag if shim),
8
+ (javascript_importmap_shim_tag if shim),
8
9
  javascript_import_module_tag(entry_point)
9
- ], "\n"
10
+ ].compact, "\n"
10
11
  end
11
12
 
12
13
  # Generate an inline importmap tag using the passed `importmap_json` JSON string.
@@ -16,9 +17,18 @@ module Importmap::ImportmapTagsHelper
16
17
  type: "importmap", "data-turbo-track": "reload", nonce: content_security_policy_nonce
17
18
  end
18
19
 
19
- # Include the es-module-shim needed to make importmaps work in browsers without native support (like Firefox + Safari).
20
+ # Configure es-modules-shim with nonce support if the application is using a content security policy.
21
+ def javascript_importmap_shim_nonce_configuration_tag
22
+ if content_security_policy?
23
+ tag.script({ nonce: content_security_policy_nonce }.to_json.html_safe,
24
+ type: "esms-options", nonce: content_security_policy_nonce)
25
+ end
26
+ end
27
+
28
+ # Include the es-modules-shim needed to make importmaps work in browsers without native support (like Firefox + Safari).
20
29
  def javascript_importmap_shim_tag
21
- 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
22
32
  end
23
33
 
24
34
  # Import a named JavaScript module(s) using a script-module tag.
@@ -37,6 +47,8 @@ module Importmap::ImportmapTagsHelper
37
47
 
38
48
  # Link tag(s) for preloading the JavaScript module residing in `*paths`. Will return one link tag per path element.
39
49
  def javascript_module_preload_tag(*paths)
40
- 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")
41
53
  end
42
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.2"
2
+ VERSION = "0.6.2"
3
3
  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.5.2
4
+ version: 0.6.2
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-12 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.12.8.js
38
37
  - app/helpers/importmap/importmap_tags_helper.rb
39
38
  - lib/importmap-rails.rb
40
39
  - lib/importmap/commands.rb