importmap-rails 1.0.1 → 1.0.2

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: 5fd79cf4031e5503a22e3013a53a35fb7ae5c3e11b1ace5791dd36429a6e5237
4
- data.tar.gz: 3f993fba70aa37b5f80c4133c3e1a8aa64fc1b3d29096cf2d1bff823e5c9add5
3
+ metadata.gz: 88dca9c2f87f4777a70f6d6e95bf7212c4f7392b04da6571f479c46b65805e28
4
+ data.tar.gz: c93c502ef9a4f739fe630bc24052ea4df3e17ce37ee5323a513ad5d302e0d7e1
5
5
  SHA512:
6
- metadata.gz: cd7593f33ed2ff9c74b6c669f10a948cf2ee750ec136f269ff8d311c7851b7a3f2dbb81bb93c44ad505417a341c72169efdf6daf294f521a32179abdd4c2447f
7
- data.tar.gz: 6e1f7fa74597f4407e03999433a20f1470fb2cb7cfa876b1d3e457cac14dffa08bdefe99110a42761ea736c13f356f50eeebfbaf5b4460d266fbd958ff99a837
6
+ metadata.gz: e058a2747e3a884535312eed0f21235d6ac98c5e7d8ba2c8cf702f52952b634cca93ec8c66f102168d60d696ab160d3ff9b08945f0419de627e58ae8a5f64633
7
+ data.tar.gz: bda6354071b0610aba6e1f7347b533d26a3a624b87038c19b0c8404ec347a37b7e1cffe41b36c4bedd4aa058f4981e359093982ea97b099cb8a1f9c57fd0657c
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Importmap for Rails
2
2
 
3
- [Import maps](https://github.com/WICG/import-maps) let you import JavaScript modules using logical names that map to versioned/digested files – directly from the browser. So you can [build modern JavaScript applications using JavaScript libraries made for ESM without the need for transpiling or bundling](https://world.hey.com/dhh/modern-web-apps-without-javascript-bundling-or-transpiling-a20f2755).This frees you from needing Webpack, Yarn, npm, or any other part of the JavaScript toolchain. All you need is the asset pipeline that's already included in Rails.
3
+ [Import maps](https://github.com/WICG/import-maps) let you import JavaScript modules using logical names that map to versioned/digested files – directly from the browser. So you can [build modern JavaScript applications using JavaScript libraries made for ES modules (ESM) without the need for transpiling or bundling](https://world.hey.com/dhh/modern-web-apps-without-javascript-bundling-or-transpiling-a20f2755). This frees you from needing Webpack, Yarn, npm, or any other part of the JavaScript toolchain. All you need is the asset pipeline that's already included in Rails.
4
4
 
5
5
  With this approach you'll ship many small JavaScript files instead of one big JavaScript file. Thanks to HTTP/2 that no longer carries a material performance penalty during the initial transport, and in fact offers substantial benefits over the long run due to better caching dynamics. Whereas before any change to any JavaScript file included in your big bundle would invalidate the cache for the the whole bundle, now only the cache for that single file is invalidated.
6
6
 
@@ -42,7 +42,7 @@ to 1 of the 3 viable ways of loading ES Module javascript packages.
42
42
  For example:
43
43
 
44
44
  ```rb
45
- # config/importmaps.rb
45
+ # config/importmap.rb
46
46
  pin "react", to: "https://ga.jspm.io/npm:react@17.0.2/index.js"
47
47
  ```
48
48
 
data/lib/importmap/map.rb CHANGED
@@ -32,14 +32,23 @@ class Importmap::Map
32
32
  @directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
33
33
  end
34
34
 
35
- def preloaded_module_paths(resolver:)
36
- cache_as(:preloaded_module_paths) do
35
+ # Returns an array of all the resolved module paths of the pinned packages. The `resolver` must respond to `asset_path`,
36
+ # such as `ActionController::Base.helpers` or `ApplicationController.helpers`. You'll want to use the resolver that has
37
+ # been configured for the `asset_host` you want these resolved paths to use. In case you need to resolve for different
38
+ # asset hosts, you can pass in a custom `cache_key` to vary the cache used by this method for the different cases.
39
+ def preloaded_module_paths(resolver:, cache_key: :preloaded_module_paths)
40
+ cache_as(cache_key) do
37
41
  resolve_asset_paths(expanded_preloading_packages_and_directories, resolver: resolver).values
38
42
  end
39
43
  end
40
44
 
41
- def to_json(resolver:)
42
- cache_as(:json) do
45
+ # Returns a JSON hash (as a string) of all the resolved module paths of the pinned packages in the import map format.
46
+ # The `resolver` must respond to `asset_path`, such as `ActionController::Base.helpers` or `ApplicationController.helpers`.
47
+ # You'll want to use the resolver that has been configured for the `asset_host` you want these resolved paths to use.
48
+ # In case you need to resolve for different asset hosts, you can pass in a custom `cache_key` to vary the cache used
49
+ # by this method for the different cases.
50
+ def to_json(resolver:, cache_key: :json)
51
+ cache_as(cache_key) do
43
52
  JSON.pretty_generate({ "imports" => resolve_asset_paths(expanded_packages_and_directories, resolver: resolver) })
44
53
  end
45
54
  end
@@ -78,13 +87,21 @@ class Importmap::Map
78
87
  if result = instance_variable_get("@cached_#{name}")
79
88
  result
80
89
  else
90
+ remember_cache_key(name)
81
91
  instance_variable_set("@cached_#{name}", yield)
82
92
  end
83
93
  end
84
94
 
95
+ def remember_cache_key(name)
96
+ @cache_keys ||= Set.new
97
+ @cache_keys.add name
98
+ end
99
+
100
+
85
101
  def clear_cache
86
- @cached_json = nil
87
- @cached_preloaded_module_paths = nil
102
+ @cache_keys&.each do |name|
103
+ instance_variable_set("@cached_#{name}", nil)
104
+ end
88
105
  end
89
106
 
90
107
  def rescuable_asset_error?(error)
@@ -1,3 +1,3 @@
1
1
  module Importmap
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
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: 1.0.1
4
+ version: 1.0.2
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-12-15 00:00:00.000000000 Z
11
+ date: 2022-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties