Skip to content

Commit 27236e3

Browse files
Make importmap paths configurable (#62)
1 parent f99ad78 commit 27236e3

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ There's [native support for import maps in Chrome/Edge 89+](https://caniuse.com/
99

1010
## Installation
1111

12-
Importmap for Rails is automatically included in Rails 7+ for new applications, but you can also install it manually in existing applications:
12+
Importmap for Rails is automatically included in Rails 7+ for new applications, but you can also install it manually in existing applications:
1313

1414
1. Add `importmap-rails` to your Gemfile with `gem 'importmap-rails'`
1515
2. Run `./bin/bundle install`
@@ -188,7 +188,7 @@ pin "@github/hotkey", to: "https://ga.jspm.io/npm:@github/[email protected]/dist/inde
188188
pin "md5", to: "https://cdn.jsdelivr.net/npm/[email protected]/md5.js"
189189

190190
# app/views/layouts/application.html.erb
191-
<%= javascript_importmap_tags %>
191+
<%= javascript_importmap_tags %>
192192
193193
# will include the following link before the importmap is setup:
194194
<link rel="modulepreload" href="https://ga.jspm.io/npm:@github/[email protected]/dist/index.js">
@@ -199,16 +199,17 @@ pin "md5", to: "https://cdn.jsdelivr.net/npm/[email protected]/md5.js"
199199

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

202-
You can combine multiple import maps by drawing their definitions onto the `Rails.application.importmap`. For example, appending import maps defined in Rails engines:
202+
You can combine multiple import maps by adding paths to additional import map configs to `Rails.application.config.importmap.paths`. For example, appending import maps defined in Rails engines:
203203

204204
```ruby
205205
# my_engine/lib/my_engine/engine.rb
206206

207207
module MyEngine
208208
class Engine < ::Rails::Engine
209209
# ...
210-
initializer "my-engine.importmap", after: "importmap" do |app|
211-
app.importmap.draw(Engine.root.join("config/importmap.rb"))
210+
initializer "my-engine.importmap", before: "importmap" do |app|
211+
app.config.importmap.paths << Engine.root.join("config/importmap.rb")
212+
# ...
212213
end
213214
end
214215
end
@@ -238,14 +239,19 @@ end
238239

239240
Generating the import map json and modulepreloads may require resolving hundreds of assets. This can take a while, so these operations are cached, but in development and test, we watch for changes to both `config/importmap.rb` and files in `app/javascript` to clear this cache. This feature can be controlled in an environment configuration file via the boolean `config.importmap.sweep_cache`.
240241

241-
If you're pinning local files from outside of `app/javascript`, you'll need to add them to the cache sweeper configuration or restart your development server upon changes to those external files. To add them to the configuration to clear the cache on changes, for instance when locally developing an engine, use an initializer like the following sample `config/initializers/importmap-caching.rb`:
242+
If you're pinning local files from outside of `app/javascript`, you'll need to add them to the cache sweeper configuration or restart your development server upon changes to those external files. For example, here's how you can do it for Rails engine:
242243

243244
```ruby
244-
if Rails.env.development?
245-
Rails.application.importmap.cache_sweeper watches: [
246-
Rails.application.root.join("app/javascript"),
247-
MyEngine::Engine.root.join("app/assets/javascripts"),
248-
]
245+
# my_engine/lib/my_engine/engine.rb
246+
247+
module MyEngine
248+
class Engine < ::Rails::Engine
249+
# ...
250+
initializer "my-engine.importmap", before: "importmap" do |app|
251+
# ...
252+
app.config.importmap.cache_sweepers << Engine.root.join("app/assets/javascripts")
253+
end
254+
end
249255
end
250256
```
251257

lib/importmap/engine.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
module Importmap
77
class Engine < ::Rails::Engine
88
config.importmap = ActiveSupport::OrderedOptions.new
9+
config.importmap.paths = []
910
config.importmap.sweep_cache = Rails.env.development? || Rails.env.test?
11+
config.importmap.cache_sweepers = []
1012
config.importmap.rescuable_asset_errors = []
1113

1214
config.autoload_once_paths = %W( #{root}/app/helpers )
1315

1416
initializer "importmap" do |app|
15-
app.importmap = Importmap::Map.new.draw("config/importmap.rb")
17+
app.importmap = Importmap::Map.new
18+
app.config.importmap.paths << app.root.join("config/importmap.rb")
19+
app.config.importmap.paths.each { |path| app.importmap.draw(path) }
1620
end
1721

1822
initializer "importmap.reloader" do |app|
19-
app.config.paths.add "config/importmap.rb"
20-
2123
Importmap::Reloader.new.tap do |reloader|
2224
reloader.execute
2325
app.reloaders << reloader
@@ -27,9 +29,9 @@ class Engine < ::Rails::Engine
2729

2830
initializer "importmap.cache_sweeper" do |app|
2931
if app.config.importmap.sweep_cache
30-
app.importmap.cache_sweeper watches: [
31-
app.root.join("app/javascript"), app.root.join("vendor/javascript")
32-
]
32+
app.config.importmap.cache_sweepers << app.root.join("app/javascript")
33+
app.config.importmap.cache_sweepers << app.root.join("vendor/javascript")
34+
app.importmap.cache_sweeper(watches: app.config.importmap.cache_sweepers)
3335

3436
ActiveSupport.on_load(:action_controller_base) do
3537
before_action { Rails.application.importmap.cache_sweeper.execute_if_updated }

lib/importmap/reloader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def updater
1111
end
1212

1313
def import_map_paths
14-
config.paths["config/importmap.rb"].existent
14+
config.importmap.paths
1515
end
1616

1717
def config

0 commit comments

Comments
 (0)