diff --git a/README.md b/README.md
index 01de2ee..9d3d5ab 100644
--- a/README.md
+++ b/README.md
@@ -215,6 +215,8 @@ pin "md5", to: "https://cdn.jsdelivr.net/npm/md5@2.3.0/md5.js"
...
```
+You can preload all your pins by default by setting `config.importmap.preload_by_default` in any environment file to `true`.
+
## Composing import maps
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 --git a/lib/importmap/engine.rb b/lib/importmap/engine.rb
index c991b9c..a6dcbdc 100755
--- a/lib/importmap/engine.rb
+++ b/lib/importmap/engine.rb
@@ -10,6 +10,7 @@ class Engine < ::Rails::Engine
config.importmap.sweep_cache = Rails.env.development? || Rails.env.test?
config.importmap.cache_sweepers = []
config.importmap.rescuable_asset_errors = []
+ config.importmap.preload_by_default = false
config.autoload_once_paths = %W( #{root}/app/helpers )
diff --git a/lib/importmap/map.rb b/lib/importmap/map.rb
index 3ef8107..504f0d4 100644
--- a/lib/importmap/map.rb
+++ b/lib/importmap/map.rb
@@ -25,12 +25,12 @@ def draw(path = nil, &block)
self
end
- def pin(name, to: nil, preload: false)
+ def pin(name, to: nil, preload: Rails.application.config.importmap.preload_by_default)
clear_cache
@packages[name] = MappedFile.new(name: name, path: to || "#{name}.js", preload: preload)
end
- def pin_all_from(dir, under: nil, to: nil, preload: false)
+ def pin_all_from(dir, under: nil, to: nil, preload: Rails.application.config.importmap.preload_by_default)
clear_cache
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
end
diff --git a/test/importmap_tags_helper_test.rb b/test/importmap_tags_helper_test.rb
index 74f75d8..d44de84 100644
--- a/test/importmap_tags_helper_test.rb
+++ b/test/importmap_tags_helper_test.rb
@@ -69,4 +69,41 @@ def content_security_policy_nonce
refute_includes importmap_html, %{}
assert_includes importmap_html, %{}
end
+
+ test "with preload by default disabled" do
+ with_preload_by_default(false) do
+ importmap = Importmap::Map.new
+ importmap.pin "foo"
+ importmap.pin_all_from "app/javascript/controllers"
+ importmap_html = javascript_importmap_tags("foo", importmap: importmap)
+
+ refute_includes importmap_html, %{}
+ refute_includes importmap_html, %{}
+ end
+ end
+
+ test "with preload by default enabled" do
+ with_preload_by_default(true) do
+ importmap = Importmap::Map.new
+ importmap.pin "foo"
+ importmap.pin_all_from "app/javascript/controllers"
+ importmap_html = javascript_importmap_tags("foo", importmap: importmap)
+
+ assert_includes importmap_html, %{}
+ assert_includes importmap_html, %{}
+ end
+ end
+
+ private
+
+ def with_preload_by_default(bool)
+ old_preload_setting = Rails.application.config.importmap.preload_by_default
+
+ begin
+ Rails.application.config.importmap.preload_by_default = bool
+ yield
+ ensure
+ Rails.application.config.importmap.preload_by_default = old_preload_setting
+ end
+ end
end