importmap-rails 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -17
- data/app/helpers/importmap/importmap_tags_helper.rb +1 -1
- data/lib/importmap-rails.rb +1 -0
- data/lib/importmap/engine.rb +12 -0
- data/lib/importmap/map.rb +18 -13
- data/lib/importmap/reloader.rb +20 -0
- data/lib/importmap/version.rb +1 -1
- data/lib/install/install.rb +6 -6
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35f2f43892e0e51811094caf1cc3239ff50cc84d6c25343ac462b77f284797a6
|
4
|
+
data.tar.gz: 0f91507c1daf180e40744350b7335cb1a24a5bc063963cc7df88b71695de5a35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96a9a8b0bc81dc2f1f7ea085c9fa9cb765cfaf4545a643f5dac80e299ea9c12c6b3c7597361f67e79f9261c28298c68d0c06aea582efa8736ee79afcf5e0ad68
|
7
|
+
data.tar.gz: b82ae667a77b3b96665bcf053388b2a2573926c4920d44c2c69316929fbacb7f034dbdd6f11b71fefc2965deb59e954925b5289323e7498328d7ac86a3338c68
|
data/README.md
CHANGED
@@ -13,14 +13,14 @@ There's [native support for import maps in Chrome/Edge 89+](https://caniuse.com/
|
|
13
13
|
2. Run `./bin/bundle install`
|
14
14
|
3. Run `./bin/rails importmap:install`
|
15
15
|
|
16
|
-
By default, all the files in `app/assets/javascripts` and the three major Rails JavaScript libraries are already mapped. You can add more mappings in `config/
|
16
|
+
By default, all the files in `app/assets/javascripts` and the three major Rails JavaScript libraries are already mapped. You can add more mappings in `config/importmap.rb`.
|
17
17
|
|
18
18
|
Note: In order to use JavaScript from Rails frameworks like Action Cable, Action Text, and Active Storage, you must be running Rails 7.0+. This was the first version that shipped with ESM compatible builds of these libraries.
|
19
19
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
The import map is configured programmatically through the `Rails.application.config.importmap` assignment, which by default is setup in `config/
|
23
|
+
The import map is configured programmatically through the `Rails.application.config.importmap` assignment, which by default is setup in `config/importmap.rb` after running the installer. 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 programmatically configured 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/assets/javascripts/application.js`, which is copied and digested by the asset pipeline.
|
26
26
|
|
@@ -29,19 +29,9 @@ It's in `app/assets/javascripts/application.js` you setup your application by im
|
|
29
29
|
It makes sense to use logical names that match the package names used by NPM, such that if you later want to start transpiling or bundling your code, you'll not have to change any module imports.
|
30
30
|
|
31
31
|
|
32
|
-
## Use with Hotwire
|
33
|
-
|
34
|
-
This gem was designed for use with [Hotwire](https://hotwired.dev) in mind. The Hotwire gems, like [turbo-rails](https://github.com/hotwired/turbo-rails) and [stimulus-rails](https://github.com/hotwired/stimulus-rails) (both bundled as [hotwire-rails](https://github.com/hotwired/hotwire-rails)), are automatically configured for use with `importmap-rails`. This means you won't have to manually setup the path mapping in `config/initializers/importmap.rb`, and instead can simply refer to the logical names directly in your `app/assets/javascripts/application.js`, like so:
|
35
|
-
|
36
|
-
```js
|
37
|
-
import "@hotwired/turbo-rails"
|
38
|
-
import "@hotwired/stimulus-autoloader"
|
39
|
-
```
|
40
|
-
|
41
|
-
|
42
32
|
## Use with Skypack (and other CDNs)
|
43
33
|
|
44
|
-
Instead of mapping JavaScript modules to files in your application's path, you can also reference them directly from JavaScript CDNs like Skypack. Simply add them to the `config/
|
34
|
+
Instead of mapping JavaScript modules to files in your application's path, you can also reference them directly from JavaScript CDNs like Skypack. Simply add them to the `config/importmap.rb` with the URL instead of the local path:
|
45
35
|
|
46
36
|
```ruby
|
47
37
|
Rails.application.config.importmap.draw do
|
@@ -61,12 +51,12 @@ console.log(md5("Hash it out"))
|
|
61
51
|
|
62
52
|
## Preloading pinned modules
|
63
53
|
|
64
|
-
To mitigate the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, we use [modulepreload links](https://developers.google.com/web/updates/2017/12/modulepreload)
|
54
|
+
To mitigate the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, we use [modulepreload links](https://developers.google.com/web/updates/2017/12/modulepreload). Pinned modules are preloaded by default, but you can turn this off with `preload: false`.
|
65
55
|
|
66
56
|
Example:
|
67
57
|
|
68
58
|
```ruby
|
69
|
-
# config/
|
59
|
+
# config/importmap.rb
|
70
60
|
Rails.application.config.importmap.draw do
|
71
61
|
pin "trix", to: "https://cdn.skypack.dev/trix"
|
72
62
|
pin "md5", to: "https://cdn.skypack.dev/md5", preload: false
|
@@ -94,5 +84,3 @@ While import maps are native in Chrome and Edge, they need a shim in other brows
|
|
94
84
|
## License
|
95
85
|
|
96
86
|
Importmap for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).
|
97
|
-
|
98
|
-
|
@@ -2,8 +2,8 @@ module Importmap::ImportmapTagsHelper
|
|
2
2
|
# Setup all script tags needed to use an importmap-powered entrypoint (which defaults to application.js)
|
3
3
|
def javascript_importmap_tags(entry_point = "application")
|
4
4
|
safe_join [
|
5
|
-
javascript_importmap_module_preload_tags,
|
6
5
|
javascript_inline_importmap_tag,
|
6
|
+
javascript_importmap_module_preload_tags,
|
7
7
|
javascript_importmap_shim_tag,
|
8
8
|
javascript_import_module_tag(entry_point)
|
9
9
|
], "\n"
|
data/lib/importmap-rails.rb
CHANGED
data/lib/importmap/engine.rb
CHANGED
@@ -6,6 +6,18 @@ module Importmap
|
|
6
6
|
|
7
7
|
config.autoload_once_paths = %W( #{root}/app/helpers )
|
8
8
|
|
9
|
+
initializer "importmap.reloader" do |app|
|
10
|
+
app.config.paths.add "config/importmap.rb"
|
11
|
+
|
12
|
+
reloader = Importmap::Reloader.new
|
13
|
+
|
14
|
+
reloader.execute
|
15
|
+
app.reloaders << reloader
|
16
|
+
app.reloader.to_run do
|
17
|
+
reloader.execute
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
9
21
|
initializer "importmap.assets" do
|
10
22
|
if Rails.application.config.respond_to?(:assets)
|
11
23
|
Rails.application.config.assets.precompile += %w( es-module-shims.js )
|
data/lib/importmap/map.rb
CHANGED
@@ -14,8 +14,8 @@ class Importmap::Map
|
|
14
14
|
@files[name] = MappedFile.new(name: name, path: to || "#{name}.js", preload: preload)
|
15
15
|
end
|
16
16
|
|
17
|
-
def pin_all_from(
|
18
|
-
@directories[
|
17
|
+
def pin_all_from(dir, under: nil, to: nil, preload: true)
|
18
|
+
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
|
19
19
|
end
|
20
20
|
|
21
21
|
def preloaded_module_paths(resolver:)
|
@@ -32,7 +32,7 @@ class Importmap::Map
|
|
32
32
|
|
33
33
|
private
|
34
34
|
MappedFile = Struct.new(:name, :path, :preload, keyword_init: true)
|
35
|
-
MappedDir = Struct.new(:path, :under, :preload, keyword_init: true)
|
35
|
+
MappedDir = Struct.new(:dir, :path, :under, :preload, keyword_init: true)
|
36
36
|
|
37
37
|
def cache_as(name)
|
38
38
|
if (cached && result = instance_variable_get("@cached_#{name}"))
|
@@ -63,11 +63,11 @@ class Importmap::Map
|
|
63
63
|
|
64
64
|
def expand_directories_into(paths)
|
65
65
|
@directories.values.each do |mapping|
|
66
|
-
if (absolute_path = absolute_root_of(mapping.
|
66
|
+
if (absolute_path = absolute_root_of(mapping.dir)).exist?
|
67
67
|
find_javascript_files_in_tree(absolute_path).each do |filename|
|
68
68
|
module_filename = filename.relative_path_from(absolute_path)
|
69
|
-
module_name = module_name_from(module_filename, mapping
|
70
|
-
module_path =
|
69
|
+
module_name = module_name_from(module_filename, mapping)
|
70
|
+
module_path = module_path_from(module_filename, mapping)
|
71
71
|
|
72
72
|
paths[module_name] = MappedFile.new(name: module_name, path: module_path, preload: mapping.preload)
|
73
73
|
end
|
@@ -75,18 +75,23 @@ class Importmap::Map
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
-
|
79
|
-
|
80
|
-
filename_without_ext = filename.to_s.remove(filename.extname)
|
78
|
+
def module_name_from(filename, mapping)
|
79
|
+
filename_without_ext_and_version = filename.to_s.remove(filename.extname).split("@").first
|
81
80
|
|
82
|
-
|
83
|
-
|
81
|
+
case
|
82
|
+
when filename_without_ext_and_version == "index" && mapping.under
|
83
|
+
mapping.under
|
84
|
+
when mapping.under
|
85
|
+
"#{mapping.under}/#{filename_without_ext_and_version}"
|
84
86
|
else
|
85
|
-
module_name
|
86
|
-
under ? "#{under}/#{module_name}" : module_name
|
87
|
+
module_name
|
87
88
|
end
|
88
89
|
end
|
89
90
|
|
91
|
+
def module_path_from(filename, mapping)
|
92
|
+
[ mapping.path || mapping.under, filename.to_s ].join("/")
|
93
|
+
end
|
94
|
+
|
90
95
|
def find_javascript_files_in_tree(path)
|
91
96
|
Dir[path.join("**/*.js{,m}")].collect { |file| Pathname.new(file) }.select(&:file?)
|
92
97
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Importmap::Reloader
|
2
|
+
delegate :execute_if_updated, :execute, :updated?, to: :updater
|
3
|
+
|
4
|
+
def reload!
|
5
|
+
import_map_paths.each { |path| load path }
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
def updater
|
10
|
+
@updater ||= config.file_watcher.new(import_map_paths) { reload! }
|
11
|
+
end
|
12
|
+
|
13
|
+
def import_map_paths
|
14
|
+
config.paths["config/importmap.rb"].existent
|
15
|
+
end
|
16
|
+
|
17
|
+
def config
|
18
|
+
Rails.application.config
|
19
|
+
end
|
20
|
+
end
|
data/lib/importmap/version.rb
CHANGED
data/lib/install/install.rb
CHANGED
@@ -10,7 +10,7 @@ end
|
|
10
10
|
|
11
11
|
say "Create application.js module as entrypoint"
|
12
12
|
create_file Rails.root.join("app/javascript/application.js") do <<-JS
|
13
|
-
// Configure your import map in config/
|
13
|
+
// Configure your import map in config/importmap.rb
|
14
14
|
|
15
15
|
// import "@rails/actioncable"
|
16
16
|
// import "@rails/activestorage"
|
@@ -20,8 +20,8 @@ end
|
|
20
20
|
say "Ensure JavaScript files are in the asset pipeline manifest"
|
21
21
|
append_to_file Rails.root.join("app/assets/config/manifest.js"), %(//= link_tree ../../javascript .js\n)
|
22
22
|
|
23
|
-
say "Configure importmap paths in config/
|
24
|
-
create_file Rails.root.join("config/
|
23
|
+
say "Configure importmap paths in config/importmap.rb"
|
24
|
+
create_file Rails.root.join("config/importmap.rb") do <<-RUBY
|
25
25
|
Rails.application.config.importmap.draw do
|
26
26
|
pin "application"
|
27
27
|
|
@@ -29,9 +29,9 @@ Rails.application.config.importmap.draw do
|
|
29
29
|
# pin "@rails/actioncable", to: "actioncable.esm.js"
|
30
30
|
# pin "@rails/activestorage", to: "activestorage.esm.js"
|
31
31
|
|
32
|
-
# Use libraries directly from JavaScript CDNs (see https://www.skypack.dev, https://
|
33
|
-
# pin "vue", to: "https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.esm.browser.js"
|
34
|
-
# pin "d3", to: "https://
|
32
|
+
# Use libraries directly from JavaScript CDNs (see https://www.skypack.dev, https://esm.sh, https://www.jsdelivr.com/esm)
|
33
|
+
# pin "vue", to: "https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.esm.browser.js"
|
34
|
+
# pin "d3", to: "https://esm.sh/d3?bundle"
|
35
35
|
|
36
36
|
# Pin vendored modules by first adding the following to app/assets/config/manifest.js:
|
37
37
|
# //= link_tree ../../../vendor/assets/javascripts .js
|
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.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-08-
|
11
|
+
date: 2021-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 6.0.0
|
27
|
-
description:
|
27
|
+
description:
|
28
28
|
email: david@loudthinking.com
|
29
29
|
executables: []
|
30
30
|
extensions: []
|
@@ -39,6 +39,7 @@ files:
|
|
39
39
|
- lib/importmap-rails.rb
|
40
40
|
- lib/importmap/engine.rb
|
41
41
|
- lib/importmap/map.rb
|
42
|
+
- lib/importmap/reloader.rb
|
42
43
|
- lib/importmap/version.rb
|
43
44
|
- lib/install/install.rb
|
44
45
|
- lib/shim.js
|
@@ -49,7 +50,7 @@ licenses:
|
|
49
50
|
metadata:
|
50
51
|
homepage_uri: https://github.com/rails/importmap-rails
|
51
52
|
source_code_uri: https://github.com/rails/importmap-rails
|
52
|
-
post_install_message:
|
53
|
+
post_install_message:
|
53
54
|
rdoc_options: []
|
54
55
|
require_paths:
|
55
56
|
- lib
|
@@ -65,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
66
|
version: '0'
|
66
67
|
requirements: []
|
67
68
|
rubygems_version: 3.1.4
|
68
|
-
signing_key:
|
69
|
+
signing_key:
|
69
70
|
specification_version: 4
|
70
71
|
summary: Use ESM with importmap to manage modern JavaScript in Rails without transpiling
|
71
72
|
or bundling.
|