importmap-rails 0.5.3 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5c7d66e249679156d7602787f9edd58abbabee35a8e8b75ee88b87cd729ac3b
4
- data.tar.gz: a1af718e58dc8d15865c7e6ebd1c0f0508b237a6fbd83db710c5fb38c869cbb8
3
+ metadata.gz: 446689178bd41da319ca7d85a90090d8aaaebcda8ba79fb8216fc42a1948af4b
4
+ data.tar.gz: d856a84a0acf723fe91b75ccb4e3f67727a5e1f64bde871a8604a79892cdc79c
5
5
  SHA512:
6
- metadata.gz: db464f8246d2b23941b7841224e857f134c23fe7da5a5750005f17a32bf345e0d0355ef8a849ca3570c38ab213cc3ed31e9a42f9eabd0ce1bb585d0636b340ec
7
- data.tar.gz: 5992e6f9db39cf53ba3ae0078db871fab936de376655717d7acdd74419ae230ab360a9765890e9adf15f21d560016c617989d777fe1f6be4f537241ebd17b6a9
6
+ metadata.gz: cf025187c8d441b30e2c2d685c114063ef768132952a42038e94d99d42016af8e094586fe5939840cb42c99b449e91070c1b6666e0ed9d3167123a80a097c51c
7
+ data.tar.gz: 1594f8e3c0bb824fbfd33bb04688a101330af87c91ad2bf3df3fd4531f0821d3b2f457e53a63ff03544307e0bd7199e2208718dda712ff50db1a5350535d4f60
data/README.md CHANGED
@@ -168,11 +168,6 @@ And pinning JavaScript modules from the engine:
168
168
  pin_all_from File.expand_path("../app/assets/javascripts", __dir__)
169
169
  ```
170
170
 
171
- ## Caching the import map and preload modules
172
-
173
- The import map should be cached in production, and is so by default via the `config.importmap.cached` option that will be set to the same value as `config.action_controller.perform_caching`, unless explicitly set differently.
174
-
175
-
176
171
  ## Expected errors from using the es-module-shim
177
172
 
178
173
  While import maps are native in Chrome and Edge, they need a shim in other browsers that'll produce a JavaScript console error like `TypeError: Module specifier, 'application' does not start with "/", "./", or "../".`. This error is normal and does not have any user-facing consequences.
@@ -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.0"
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.3
4
+ version: 0.6.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-15 00:00:00.000000000 Z
11
+ date: 2021-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails