importmap-rails 2.0.3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bec8044eff12e2d9ad0a16b077c6aca6c1fd365ebcb218815910c778407f7f91
4
- data.tar.gz: c6b5ba9bb3b4e80762c1bdf49030d54504e6c865bab7dd8dda0e6df964ac3637
3
+ metadata.gz: 24bbd66810fcdeffe321841aa86a4308b0fa76749e9ec20105f8d74475634aa7
4
+ data.tar.gz: ea19b79e7701a7aafd6fe546edc88880aa5e68858999959d8e52005ce0be5a34
5
5
  SHA512:
6
- metadata.gz: 1f76fcb0a38faba63a48c59c4735d3dc33cc6e24d30e55b689e33e2fb75285461bc7ec7c6684a4325666b3405956069a16c778ad09f26aa40b29d0e05b3fecf3
7
- data.tar.gz: 878c7a4bf22f8b130aaa6cd2f14ef84db3cda9924278a825ff7be98e9c3f0c50aa44adf1d87c207dd51239782470a2ca05c9964c00ecd01e0730a3dff169c6f8
6
+ metadata.gz: 741aab3cb6747eba7daf39dd5d311c126dfccfb55132c68d1e1cdaafe2b84a9e2596ea4746446cf6f1702d851a109cae3413b97a233889cd8855e347098ee592
7
+ data.tar.gz: 3b9ed94a737134bbfcd8be54f95fa5094539b2b6d56b7365362bdf7ac961bb1e727cef396eca0001629d45e2c50578c3af819559a521829f41b4e875733d3e80
data/README.md CHANGED
@@ -226,14 +226,16 @@ Import your module on the specific page. Note: you'll likely want to use a `cont
226
226
 
227
227
  ## Include a digest of the import map in your ETag
228
228
 
229
- If you're using [ETags](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) generated by Rails helpers like `stale?` or `fresh_when`, you need to include the digest of the import map into this calculation. Otherwise your application will return [304](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304) cache responses even when your JavaScript assets have changed. You can avoid this with something like:
229
+ If you're using [ETags](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) generated by Rails helpers like `stale?` or `fresh_when`, you need to include the digest of the import map into this calculation. Otherwise your application will return [304](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304) cache responses even when your JavaScript assets have changed. You can avoid this using the `stale_when_importmap_changes` method:
230
230
 
231
231
  ```ruby
232
232
  class ApplicationController < ActionController::Base
233
- etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
233
+ stale_when_importmap_changes
234
234
  end
235
235
  ```
236
236
 
237
+ This will add the digest of the importmap to the etag calculation when the request format is HTML.
238
+
237
239
 
238
240
  ## Sweeping the cache in development and test
239
241
 
@@ -0,0 +1,5 @@
1
+ module Importmap::Freshness
2
+ def stale_when_importmap_changes
3
+ etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
4
+ end
5
+ end
@@ -3,7 +3,7 @@ module Importmap::ImportmapTagsHelper
3
3
  def javascript_importmap_tags(entry_point = "application", importmap: Rails.application.importmap)
4
4
  safe_join [
5
5
  javascript_inline_importmap_tag(importmap.to_json(resolver: self)),
6
- javascript_importmap_module_preload_tags(importmap, entry_point: entry_point),
6
+ javascript_importmap_module_preload_tags(importmap, entry_point:),
7
7
  javascript_import_module_tag(entry_point)
8
8
  ], "\n"
9
9
  end
@@ -25,7 +25,7 @@ module Importmap::ImportmapTagsHelper
25
25
  # (defaults to Rails.application.importmap), such that they'll be fetched
26
26
  # in advance by browsers supporting this link type (https://caniuse.com/?search=modulepreload).
27
27
  def javascript_importmap_module_preload_tags(importmap = Rails.application.importmap, entry_point: "application")
28
- javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self, entry_point: entry_point, cache_key: entry_point))
28
+ javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self, entry_point:, cache_key: entry_point))
29
29
  end
30
30
 
31
31
  # Link tag(s) for preloading the JavaScript module residing in `*paths`. Will return one link tag per path element.
@@ -46,6 +46,24 @@ class Importmap::Commands < Thor
46
46
  end
47
47
  end
48
48
 
49
+ desc "pristine", "Redownload all pinned packages"
50
+ option :env, type: :string, aliases: :e, default: "production"
51
+ option :from, type: :string, aliases: :f, default: "jspm"
52
+ def pristine
53
+ packages = npm.packages_with_versions.map do |p, v|
54
+ v.blank? ? p : [p, v].join("@")
55
+ end
56
+
57
+ if imports = packager.import(*packages, env: options[:env], from: options[:from])
58
+ imports.each do |package, url|
59
+ puts %(Downloading "#{package}" to #{packager.vendor_path}/#{package}.js from #{url})
60
+ packager.download(package, url)
61
+ end
62
+ else
63
+ puts "Couldn't find any packages in #{packages.inspect} on #{options[:from]}"
64
+ end
65
+ end
66
+
49
67
  desc "json", "Show the full importmap in json"
50
68
  def json
51
69
  require Rails.root.join("config/environment")
@@ -11,7 +11,7 @@ module Importmap
11
11
  config.importmap.cache_sweepers = []
12
12
  config.importmap.rescuable_asset_errors = []
13
13
 
14
- config.autoload_once_paths = %W( #{root}/app/helpers )
14
+ config.autoload_once_paths = %W( #{root}/app/helpers #{root}/app/controllers )
15
15
 
16
16
  initializer "importmap" do |app|
17
17
  app.importmap = Importmap::Map.new
@@ -48,6 +48,12 @@ module Importmap
48
48
  end
49
49
  end
50
50
 
51
+ initializer "importmap.concerns" do
52
+ ActiveSupport.on_load(:action_controller_base) do
53
+ extend Importmap::Freshness
54
+ end
55
+ end
56
+
51
57
  initializer "importmap.helpers" do
52
58
  ActiveSupport.on_load(:action_controller_base) do
53
59
  helper Importmap::ImportmapTagsHelper
data/lib/importmap/map.rb CHANGED
@@ -42,7 +42,7 @@ class Importmap::Map
42
42
  # the different cases.
43
43
  def preloaded_module_paths(resolver:, entry_point: "application", cache_key: :preloaded_module_paths)
44
44
  cache_as(cache_key) do
45
- resolve_asset_paths(expanded_preloading_packages_and_directories(entry_point: entry_point), resolver: resolver).values
45
+ resolve_asset_paths(expanded_preloading_packages_and_directories(entry_point:), resolver:).values
46
46
  end
47
47
  end
48
48
 
@@ -1,3 +1,3 @@
1
1
  module Importmap
2
- VERSION = "2.0.3"
2
+ VERSION = "2.1.0"
3
3
  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: 2.0.3
4
+ version: 2.1.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: 2024-10-10 00:00:00.000000000 Z
11
+ date: 2024-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -61,6 +61,7 @@ files:
61
61
  - MIT-LICENSE
62
62
  - README.md
63
63
  - Rakefile
64
+ - app/controllers/importmap/freshness.rb
64
65
  - app/helpers/importmap/importmap_tags_helper.rb
65
66
  - lib/importmap-rails.rb
66
67
  - lib/importmap/commands.rb
@@ -88,14 +89,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
89
  requirements:
89
90
  - - ">="
90
91
  - !ruby/object:Gem::Version
91
- version: 2.7.0
92
+ version: 3.1.0
92
93
  required_rubygems_version: !ruby/object:Gem::Requirement
93
94
  requirements:
94
95
  - - ">="
95
96
  - !ruby/object:Gem::Version
96
97
  version: '0'
97
98
  requirements: []
98
- rubygems_version: 3.2.33
99
+ rubygems_version: 3.5.22
99
100
  signing_key:
100
101
  specification_version: 4
101
102
  summary: Use ESM with importmap to manage modern JavaScript in Rails without transpiling