Skip to content

Commit 2eff573

Browse files
committed
Introduce preload_by_default config
1 parent e0c1676 commit 2eff573

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ pin "md5", to: "https://cdn.jsdelivr.net/npm/[email protected]/md5.js"
215215
...
216216
```
217217

218+
You can preload all your pins by default by setting `config.importmap.preload_by_default` in any environment file to `true`.
219+
218220
## Composing import maps
219221

220222
By default, Rails loads import map definition from the application's `config/importmap.rb` to the `Importmap::Map` object available at `Rails.application.importmap`.

Diff for: lib/importmap/engine.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Engine < ::Rails::Engine
1010
config.importmap.sweep_cache = Rails.env.development? || Rails.env.test?
1111
config.importmap.cache_sweepers = []
1212
config.importmap.rescuable_asset_errors = []
13+
config.importmap.preload_by_default = false
1314

1415
config.autoload_once_paths = %W( #{root}/app/helpers )
1516

Diff for: lib/importmap/map.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ def draw(path = nil, &block)
2525
self
2626
end
2727

28-
def pin(name, to: nil, preload: false)
28+
def pin(name, to: nil, preload: Rails.application.config.importmap.preload_by_default)
2929
clear_cache
3030
@packages[name] = MappedFile.new(name: name, path: to || "#{name}.js", preload: preload)
3131
end
3232

33-
def pin_all_from(dir, under: nil, to: nil, preload: false)
33+
def pin_all_from(dir, under: nil, to: nil, preload: Rails.application.config.importmap.preload_by_default)
3434
clear_cache
3535
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
3636
end

Diff for: test/importmap_tags_helper_test.rb

+37
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,41 @@ def content_security_policy_nonce
6969
refute_includes importmap_html, %{<link rel="modulepreload" href="/bar.js">}
7070
assert_includes importmap_html, %{<script type="module">import "foo"</script>}
7171
end
72+
73+
test "with preload by default disabled" do
74+
with_preload_by_default(false) do
75+
importmap = Importmap::Map.new
76+
importmap.pin "foo"
77+
importmap.pin_all_from "app/javascript/controllers"
78+
importmap_html = javascript_importmap_tags("foo", importmap: importmap)
79+
80+
refute_includes importmap_html, %{<link rel="modulepreload" href="/foo.js">}
81+
refute_includes importmap_html, %{<link rel="modulepreload" href="/goodbye_controller.js">}
82+
end
83+
end
84+
85+
test "with preload by default enabled" do
86+
with_preload_by_default(true) do
87+
importmap = Importmap::Map.new
88+
importmap.pin "foo"
89+
importmap.pin_all_from "app/javascript/controllers"
90+
importmap_html = javascript_importmap_tags("foo", importmap: importmap)
91+
92+
assert_includes importmap_html, %{<link rel="modulepreload" href="/foo.js">}
93+
assert_includes importmap_html, %{<link rel="modulepreload" href="/goodbye_controller.js">}
94+
end
95+
end
96+
97+
private
98+
99+
def with_preload_by_default(bool)
100+
old_preload_setting = Rails.application.config.importmap.preload_by_default
101+
102+
begin
103+
Rails.application.config.importmap.preload_by_default = bool
104+
yield
105+
ensure
106+
Rails.application.config.importmap.preload_by_default = old_preload_setting
107+
end
108+
end
72109
end

0 commit comments

Comments
 (0)