importmap-rails 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e83ed6ee74e2b39acc314afedb47331aa6499f73cefd377813d35042c5edef99
4
- data.tar.gz: 2892356151da356c2ca221d9cdb34742c7f258641481079f32c7e3a65b92155b
3
+ metadata.gz: 041d46b392f2efffb635e2f9f25618564d6dd2f10b7fc79f1c20a1deb300ba91
4
+ data.tar.gz: c83a93b7c61426faa75190fdd9582cd05b4cd1f87a1a85d1ae1b0d19a5603d45
5
5
  SHA512:
6
- metadata.gz: a31bde4420db119b939f660310262b2ce56bf9dc2dba8c6a255c565ce99d127361584917e378445b6a500675295a502912e49e772aec37a95d32b1d51d9def85
7
- data.tar.gz: 79dbc56c9c96dd85ef65d9d0cca3baee1c1e0c8e80e25f99b3823dd1828a20297261ea7fb21f13c7dea980c13358c9872cb09b2217704c293f260b17d11a510f
6
+ metadata.gz: c1c6ac59101df2d7fadf3a9dc7e72bb5cfff9cfc74aa9e5672a3c5a3c08aaa29e976dbd9dbddd557b0f751531016ad196bf966c2899984bd16f8275e7968a49e
7
+ data.tar.gz: f9b6cb16445170034a0ca87f08d08321c8ab571dbaf851e0d7e561f8051bd6da11dcd0431e7b1ba4cc8fd0bf5b5acd5f52ecd4198a777cf0e3a450f68b11eb25
data/README.md CHANGED
@@ -20,7 +20,7 @@ Note: In order to use JavaScript from Rails frameworks like Action Cable, Action
20
20
 
21
21
  ## Usage
22
22
 
23
- 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.
23
+ 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.
24
24
 
25
25
  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`.
26
26
 
@@ -143,9 +143,9 @@ pin "md5", to: "https://cdn.jsdelivr.net/npm/md5@2.3.0/md5.js", preload: false
143
143
 
144
144
  ## Composing import maps
145
145
 
146
- 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`.
146
+ By default, Rails loads import map definition from the application's `config/importmap.rb` to the `Importmap::Map` object available at `Rails.application.importmap`.
147
147
 
148
- 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:
148
+ You can combine multiple import maps by drawing their definitions onto the `Rails.application.importmap`. For example, appending import maps defined in Rails engines:
149
149
 
150
150
  ```ruby
151
151
  # my_engine/lib/my_engine/engine.rb
@@ -175,10 +175,16 @@ If you're using etags generated by Rails helpers like `stale?` or `fresh_when`,
175
175
 
176
176
  ```ruby
177
177
  class ApplicationController < ActionController::Base
178
- etag { Rails.application.config.importmap.digest(resolver: helpers) if request.format&.html? }
178
+ etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
179
179
  end
180
180
  ```
181
181
 
182
+
183
+ ## Sweeping the cache in development and test
184
+
185
+ 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`. If you're pinning local files from outside of `app/javascript`, you'll need to restart your development server upon changes.
186
+
187
+
182
188
  ## Expected errors from using the es-module-shim
183
189
 
184
190
  While import maps are native in Chrome and Edge, they need a shim in other browsers that'll produce a JavaScript console error like `TypeError: Module specifier, 'application' does not start with "/", "./", or "../".`. This error is normal and does not have any user-facing consequences.
@@ -188,10 +194,12 @@ While import maps are native in Chrome and Edge, they need a shim in other brows
188
194
 
189
195
  Under certain circumstances, like running system tests using chromedriver under CI (which may be resource constrained and trigger errors in certain cases), you may want to explicitly turn off including the shim. If can do this by calling the bulk tag helper with `javascript_importmap_tags("application", shim: false)`. Thus you can pass in something like `shim: !ENV["CI"]`. If you want, and are sure you're not doing any full-page caching, you can also connect this directive to a user agent check (using a gem like `useragent`) to check whether the browser is chrome/edge 89+. But you really shouldn't have to, as the shim is designed to gracefully work with natively compatible drivers.
190
196
 
197
+
191
198
  ## A note about browser extensions
192
199
 
193
200
  Certain extensions that also load Javascript modules may block import maps from being loaded (for instance, the Apollo Client Devtools extension). If you see a console message like `An import map is added after module script load was triggered`, browser extensions are likely the culprit.
194
201
 
202
+
195
203
  ## License
196
204
 
197
205
  Importmap for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).
@@ -11,8 +11,8 @@ module Importmap::ImportmapTagsHelper
11
11
  end
12
12
 
13
13
  # Generate an inline importmap tag using the passed `importmap_json` JSON string.
14
- # By default, `Rails.application.config.importmap.to_json(resolver: self)` is used.
15
- def javascript_inline_importmap_tag(importmap_json = Rails.application.config.importmap.to_json(resolver: self))
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.config.importmap), such that they'll be fetched
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.config.importmap)
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
 
@@ -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.config.importmap.to_json(resolver: ActionController::Base.helpers)
46
+ puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers)
47
47
  end
48
48
 
49
49
  private
@@ -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 = Importmap::Map.new.draw("config/importmap.rb")
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.config.importmap.digest(resolver: helpers) if request.format&.html? }
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)
@@ -2,7 +2,7 @@ class Importmap::Reloader
2
2
  delegate :execute_if_updated, :execute, :updated?, to: :updater
3
3
 
4
4
  def reload!
5
- import_map_paths.each { |path| config.importmap.draw(path) }
5
+ import_map_paths.each { |path| Rails.application.importmap.draw(path) }
6
6
  end
7
7
 
8
8
  private
@@ -1,3 +1,3 @@
1
1
  module Importmap
2
- VERSION = "0.6.3"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -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.config.importmap.to_json(resolver: ActionController::Base.helpers)
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.6.3
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-19 00:00:00.000000000 Z
11
+ date: 2021-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails