importmap-rails 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -5
- data/app/helpers/importmap/importmap_tags_helper.rb +7 -3
- data/lib/importmap/engine.rb +0 -6
- data/lib/importmap/map.rb +20 -2
- data/lib/importmap/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 446689178bd41da319ca7d85a90090d8aaaebcda8ba79fb8216fc42a1948af4b
|
4
|
+
data.tar.gz: d856a84a0acf723fe91b75ccb4e3f67727a5e1f64bde871a8604a79892cdc79c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,
|
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|
|
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
|
data/lib/importmap/engine.rb
CHANGED
@@ -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
|
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
|
data/lib/importmap/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2021-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|