Skip to content

Commit

Permalink
Rails.application.config.importmap => Rails.application.importmap
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed Sep 20, 2021
1 parent 9d818ff commit 75f312b
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Note: In order to use JavaScript from Rails frameworks like Action Cable, Action

## Usage

The import map is setup through `Rails.application.config.importmap` via the configuration in `config/importmap.rb`. This file is automatically reloaded in development upon changes, but note that you must restart the server if you remove pins and need them gone from the rendered importmap or list of preloads.
The import map is setup through `Rails.application.importmap` via the configuration in `config/importmap.rb`. This file is automatically reloaded in development upon changes, but note that you must restart the server if you remove pins and need them gone from the rendered importmap or list of preloads.

This import map is inlined in the `<head>` of your application layout using `<%= javascript_importmap_tags %>`, which will setup the JSON configuration inside a `<script type="importmap">` tag. After that, the [es-module-shim](https://github.com/guybedford/es-module-shims) is loaded, and then finally the application entrypoint is imported via `<script type="module">import "application"</script>`. That logical entrypoint, `application`, is mapped in the importmap script tag to the file `app/javascript/application.js`.

Expand Down Expand Up @@ -143,9 +143,9 @@ pin "md5", to: "https://cdn.jsdelivr.net/npm/md5@2.3.0/md5.js", preload: false

## 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.config.importmap`.
By default, Rails loads import map definition from the application's `config/importmap.rb` to the `Importmap::Map` object available at `Rails.application.importmap`.

You can combine multiple import maps by drawing their definitions onto the `Rails.application.config.importmap`. For example, appending import maps defined in Rails engines:
You can combine multiple import maps by drawing their definitions onto the `Rails.application.importmap`. For example, appending import maps defined in Rails engines:

```ruby
# my_engine/lib/my_engine/engine.rb
Expand Down Expand Up @@ -175,7 +175,7 @@ If you're using etags generated by Rails helpers like `stale?` or `fresh_when`,

```ruby
class ApplicationController < ActionController::Base
etag { Rails.application.config.importmap.digest(resolver: helpers) if request.format&.html? }
etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
end
```

Expand Down
8 changes: 4 additions & 4 deletions app/helpers/importmap/importmap_tags_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def javascript_importmap_tags(entry_point = "application", shim: true)
end

# Generate an inline importmap tag using the passed `importmap_json` JSON string.
# By default, `Rails.application.config.importmap.to_json(resolver: self)` is used.
def javascript_inline_importmap_tag(importmap_json = Rails.application.config.importmap.to_json(resolver: self))
# By default, `Rails.application.importmap.to_json(resolver: self)` is used.
def javascript_inline_importmap_tag(importmap_json = Rails.application.importmap.to_json(resolver: self))
tag.script importmap_json.html_safe,
type: "importmap", "data-turbo-track": "reload", nonce: content_security_policy_nonce
end
Expand All @@ -39,9 +39,9 @@ def javascript_import_module_tag(*module_names)
end

# Link tags for preloading all modules marked as preload: true in the `importmap`
# (defaults to Rails.application.config.importmap), such that they'll be fetched
# (defaults to Rails.application.importmap), such that they'll be fetched
# in advance by browsers supporting this link type (https://caniuse.com/?search=modulepreload).
def javascript_importmap_module_preload_tags(importmap = Rails.application.config.importmap)
def javascript_importmap_module_preload_tags(importmap = Rails.application.importmap)
javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self))
end

Expand Down
2 changes: 1 addition & 1 deletion lib/importmap/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def unpin(*packages)

desc "json", "Show the full importmap in json"
def json
puts Rails.application.config.importmap.to_json(resolver: ActionController::Base.helpers)
puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers)
end

private
Expand Down
7 changes: 6 additions & 1 deletion lib/importmap/engine.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
require "importmap/map"

Rails::Application.send(:attr_accessor, :importmap)

module Importmap
class Engine < ::Rails::Engine
config.importmap = Importmap::Map.new.draw("config/importmap.rb")
config.autoload_once_paths = %W( #{root}/app/helpers )

initializer "importmap" do |app|
app.importmap = Importmap::Map.new.draw("config/importmap.rb")
end

initializer "importmap.reloader" do |app|
app.config.paths.add "config/importmap.rb"

Expand Down
2 changes: 1 addition & 1 deletion lib/importmap/reloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Importmap::Reloader
delegate :execute_if_updated, :execute, :updated?, to: :updater

def reload!
import_map_paths.each { |path| config.importmap.draw(path) }
import_map_paths.each { |path| Rails.application.importmap.draw(path) }
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/importmap_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace :importmap do
desc "Show the importmap"
task :pins do
require Rails.root.join("config/environment")
puts Rails.application.config.importmap.to_json(resolver: ActionController::Base.helpers)
puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers)
end
end
4 changes: 2 additions & 2 deletions test/reloader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class ReloaderTest < ActiveSupport::TestCase
end

test "redraws importmap when config changes" do
Rails.application.config.importmap = Importmap::Map.new.draw { pin "md5", to: "https://cdn.skypack.dev/md5" }
Rails.application.importmap = Importmap::Map.new.draw { pin "md5", to: "https://cdn.skypack.dev/md5" }
assert_not_predicate @reloader, :updated?

assert_changes -> { Rails.application.config.importmap.packages.keys }, from: %w[ md5 ], to: %w[ md5 not_there ] do
assert_changes -> { Rails.application.importmap.packages.keys }, from: %w[ md5 ], to: %w[ md5 not_there ] do
touch_config
assert @reloader.execute_if_updated
end
Expand Down

0 comments on commit 75f312b

Please sign in to comment.