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 +4 -4
- data/README.md +2 -2
- data/lib/importmap/map.rb +23 -6
- data/lib/importmap/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88dca9c2f87f4777a70f6d6e95bf7212c4f7392b04da6571f479c46b65805e28
|
4
|
+
data.tar.gz: c93c502ef9a4f739fe630bc24052ea4df3e17ce37ee5323a513ad5d302e0d7e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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/
|
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
|
-
|
36
|
-
|
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
|
-
|
42
|
-
|
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
|
-
@
|
87
|
-
|
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)
|
data/lib/importmap/version.rb
CHANGED
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.
|
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:
|
11
|
+
date: 2022-01-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|