importmap-rails 0.2.7 → 0.3.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: d76219ce916f51a755ec35408f60b5258cf63b8d8d5b5252ab04b351348cfebb
4
- data.tar.gz: 9467508f9d692ce701ccf1943adf146b776be046e1eac06f60541209fe26ace7
3
+ metadata.gz: 35f2f43892e0e51811094caf1cc3239ff50cc84d6c25343ac462b77f284797a6
4
+ data.tar.gz: 0f91507c1daf180e40744350b7335cb1a24a5bc063963cc7df88b71695de5a35
5
5
  SHA512:
6
- metadata.gz: c013fde57b613a62f6f228e6ceb5f106dbbe77af14d3737e2206bcdd38e78d1a8bbdf2c21d5e292b155b03ea6069918c4083c9f913595c925dc626d91286f1ca
7
- data.tar.gz: dce28047300891ecdea4b5165dd99163066676fbf3145789ac094f74a2ef2aeb5f4f5a99d6b576da66ea70cb502ba3be61d8517e6ab9e0e8d62e40269d85f06d
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/initializers/importmap.rb`.
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/initializers/importmap.rb` after running the installer. (Note that since this is a config initializer, you must restart your development server after making any changes.)
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/initializers/importmap.rb` with the URL instead of the local path:
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) in [browsers that support it](https://caniuse.com/?search=modulepreload). Pinned modules are preloaded by default, but you can turn this off with `preload: false`.
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/initializers/importmap.rb
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,4 +2,5 @@ module Importmap
2
2
  end
3
3
 
4
4
  require "importmap/version"
5
+ require "importmap/reloader"
5
6
  require "importmap/engine"
@@ -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 )
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Importmap
2
- VERSION = "0.2.7"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -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/initializers/importmap.rb
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/initializers/importmap.rb"
24
- create_file Rails.root.join("config/initializers/importmap.rb") do <<-RUBY
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
 
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.2.7
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-17 00:00:00.000000000 Z
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.