importmap-rails 0.6.0 → 0.7.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 +31 -4
- data/app/assets/javascripts/es-module-shims.js +768 -1
- data/app/helpers/importmap/importmap_tags_helper.rb +7 -7
- data/lib/importmap/commands.rb +1 -1
- data/lib/importmap/engine.rb +20 -1
- data/lib/importmap/map.rb +16 -1
- data/lib/importmap/reloader.rb +1 -1
- data/lib/importmap/version.rb +1 -1
- data/lib/install/install.rb +4 -2
- data/lib/tasks/importmap_tasks.rake +1 -1
- metadata +2 -3
- data/app/assets/javascripts/es-module-shims@0.14.0.csp.js +0 -1635
@@ -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.
|
15
|
-
def javascript_inline_importmap_tag(importmap_json = Rails.application.
|
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.
|
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.
|
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
|
|
data/lib/importmap/commands.rb
CHANGED
@@ -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.
|
46
|
+
puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers)
|
47
47
|
end
|
48
48
|
|
49
49
|
private
|
data/lib/importmap/engine.rb
CHANGED
@@ -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 =
|
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.
|
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)
|
data/lib/importmap/reloader.rb
CHANGED
data/lib/importmap/version.rb
CHANGED
data/lib/install/install.rb
CHANGED
@@ -14,8 +14,10 @@ create_file Rails.root.join("app/javascript/application.js") do <<-JS
|
|
14
14
|
JS
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
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.
|
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.
|
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-
|
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
|