importmap-rails 0.1.3 → 0.2.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: e9569cf8ada42f3b40596da6bbb24565237a45f3f49108b91470d18715d6fe55
4
- data.tar.gz: 682592d777e3fffa178e4b0f74fc97c83564f4222a881532376a26890faa12da
3
+ metadata.gz: d90db013f0d628f59edd7b9b17892aae92033dea65298c03351d1ae75b9a782a
4
+ data.tar.gz: 3d8212059da5f5e4dc1ca241385132633dd7e6bd1a700087af8a18e38ae62456
5
5
  SHA512:
6
- metadata.gz: 6ff7b0f6d2f5510281a6f540096b03dd7dc760927edf229425c1b5032b93e88ae17276fc9b348c7777cff0eacf39b58e944db26169d11db44c35c75185b89384
7
- data.tar.gz: 9456a86bc8264595860b0e0e03690c82f9fb739f1a5a4ea7850f59f7570daec22c42ba18d6fde9fd5bc5cc76d4b310c6d4e172c4a5dfe8b18ef98d0a7e597dae
6
+ metadata.gz: 9f0b4a69a02f52d8a20be1235dadda2b9412b76677ef4897558822ed0244c5226428930891386bf830ac0fa93887ecaa71e3a4a69000afad926462bee2b9fc2b
7
+ data.tar.gz: 4c9da92a7e433445db8d9ea9b1db049996eb0b3943a229163985d0b17b6e31f7d5b0fc5fc013104330511d3d473c976fc3d4751a8b4ccfbbaad6644fac75b690
data/README.md CHANGED
@@ -20,7 +20,7 @@ Note: In order to use JavaScript from Rails frameworks like Action Cable, Action
20
20
 
21
21
  ## Usage
22
22
 
23
- The import map is configured programmatically through the `Rails.application.config.importmap.paths` assignment, which by default is setup in `config/initializers/assets.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/initializers/assets.rb` after running the installer. (Note that since this is a config initializer, you must restart your development server after making any changes.)
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
 
@@ -44,9 +44,9 @@ import "@hotwired/stimulus-autoloader"
44
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/assets.rb` with the URL instead of the local path:
45
45
 
46
46
  ```ruby
47
- Rails.application.config.importmap.paths.tap do |paths|
48
- paths.asset "trix", path: "https://cdn.skypack.dev/trix"
49
- paths.asset "md5", path: "https://cdn.skypack.dev/md5"
47
+ Rails.application.config.importmap.draw do
48
+ pin "trix", to: "https://cdn.skypack.dev/trix"
49
+ pin "md5", to: "https://cdn.skypack.dev/md5"
50
50
  end
51
51
  ```
52
52
 
@@ -8,10 +8,10 @@ module Importmap::ImportmapTagsHelper
8
8
  ], "\n"
9
9
  end
10
10
 
11
- # Generate an inline importmap tag using the passed `importmap_paths` object to produce the JSON map.
12
- # By default, `Rails.application.config.importmap.paths` is used for this object,
13
- def javascript_inline_importmap_tag(importmap_paths = Rails.application.config.importmap.paths)
14
- tag.script(importmap_paths.to_json(self).html_safe, type: "importmap", "data-turbo-track": "reload")
11
+ # Generate an inline importmap tag using the passed `importmap` object to produce the JSON map.
12
+ # By default, `Rails.application.config.importmap` is used for this object.
13
+ def javascript_inline_importmap_tag(importmap = Rails.application.config.importmap)
14
+ tag.script(importmap.to_json(self).html_safe, type: "importmap", "data-turbo-track": "reload")
15
15
  end
16
16
 
17
17
  # Include the es-module-shim needed to make importmaps work in browsers without native support (like Firefox + Safari).
@@ -1,15 +1,14 @@
1
- require "importmap/paths"
1
+ require "importmap/map"
2
2
 
3
3
  module Importmap
4
4
  class Engine < ::Rails::Engine
5
- config.importmap = ActiveSupport::OrderedOptions.new
6
- config.importmap.paths = Importmap::Paths.new.tap { |paths| paths.assets_in "app/assets/javascripts" }
5
+ config.importmap = Importmap::Map.new
7
6
 
8
7
  config.autoload_once_paths = %W( #{root}/app/helpers )
9
8
 
10
9
  initializer "importmap.assets" do
11
10
  if Rails.application.config.respond_to?(:assets)
12
- Rails.application.config.assets.precompile += %w( es-module-shims )
11
+ Rails.application.config.assets.precompile += %w( es-module-shims.js )
13
12
  end
14
13
  end
15
14
 
@@ -18,5 +17,11 @@ module Importmap
18
17
  helper Importmap::ImportmapTagsHelper
19
18
  end
20
19
  end
20
+
21
+ initializer "importmap.map" do
22
+ Rails.application.config.to_prepare do
23
+ load Rails.root.join("config/importmap.rb")
24
+ end
25
+ end
21
26
  end
22
27
  end
@@ -1,30 +1,33 @@
1
- class Importmap::Paths
1
+ class Importmap::Map
2
2
  attr_reader :files, :directories
3
3
 
4
4
  def initialize
5
- @files = {}
6
- @directories = {}
5
+ @files, @directories = {}, {}
7
6
  end
8
7
 
9
- def asset(name, path: nil)
10
- @files[name] = path || "#{name}.js"
8
+ def draw(&block)
9
+ instance_eval(&block)
11
10
  end
12
11
 
13
- def assets_in(path, append_base_path: false)
12
+ def pin(name, to:)
13
+ @files[name] = to
14
+ end
15
+
16
+ def pin_all_from(path, append_base_path: false)
14
17
  @directories[path] = append_base_path
15
18
  end
16
19
 
17
20
  def to_json(resolver)
18
- { "imports" => map_to_asset_paths(resolver) }.to_json
21
+ { "imports" => resolve_asset_paths(resolver) }.to_json
19
22
  end
20
23
 
21
24
  private
22
- def map_to_asset_paths(resolver)
25
+ def resolve_asset_paths(resolver)
23
26
  expanded_files_and_directories.transform_values do |path|
24
27
  begin
25
28
  resolver.asset_path(path)
26
29
  rescue Sprockets::Rails::Helper::AssetNotFound
27
- Rails.logger.warn "Importmap skipped missing asset: #{path}"
30
+ Rails.logger.warn "Importmap skipped missing path: #{path}"
28
31
  nil
29
32
  end
30
33
  end.compact
@@ -1,3 +1,3 @@
1
1
  module Importmap
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -23,22 +23,25 @@ append_to_file Rails.root.join("app/assets/config/manifest.js"), %(//= link_tree
23
23
  say "Configure importmap paths in config/initializers/assets.rb"
24
24
  append_to_file Rails.root.join("config/initializers/assets.rb") do <<-RUBY
25
25
 
26
- # Configure import map beyond the default of having all files in app/assets/javascripts mapped.
27
- Rails.application.config.importmap.paths.tap do |paths|
26
+ # Configure import map to be used for ESM
27
+ Rails.application.config.importmap.draw do
28
+ # All JavaScript files in the tree are mapped to their name
29
+ pin_all_from "app/assets/javascripts"
30
+
28
31
  # Match libraries with their NPM package names for possibility of later porting.
29
32
  # Ensure that libraries listed in the path have been linked in the asset pipeline manifest or precompiled.
30
- paths.asset "@rails/actioncable", path: "actioncable.esm.js"
31
- paths.asset "@rails/activestorage", path: "activestorage.esm.js"
32
- paths.asset "@rails/actiontext", path: "actiontext.js"
33
- paths.asset "trix"
33
+ pin "@rails/actioncable", to: "actioncable.esm.js"
34
+ pin "@rails/activestorage", to: "activestorage.esm.js"
35
+ pin "@rails/actiontext", to: "actiontext.js"
36
+ pin "trix", to: "trix.js"
34
37
 
35
38
  # Use libraries directly from JavaScript CDNs
36
- # paths.asset "vue", path: "https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.esm.browser.js"
37
- # paths.asset "d3", path: "https://cdn.skypack.dev/pin/d3@v7.0.0-03vFl9bie0TSesDkWTJV/mode=imports/optimized/d3.js"
39
+ # pin "vue", to: "https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.esm.browser.js"
40
+ # pin "d3", to: "https://cdn.skypack.dev/pin/d3@v7.0.0-03vFl9bie0TSesDkWTJV/mode=imports/optimized/d3.js"
38
41
 
39
- # Map vendored modules by first adding the following to app/assets/config/manifest.js:
42
+ # Pin vendored modules by first adding the following to app/assets/config/manifest.js:
40
43
  # //= link_tree ../../../vendor/assets/javascripts .js
41
- # paths.assets_in "vendor/assets/javascripts"
44
+ # pin_all_from "vendor/assets/javascripts"
42
45
  end
43
46
  RUBY
44
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: importmap-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -38,7 +38,7 @@ files:
38
38
  - app/helpers/importmap/importmap_tags_helper.rb
39
39
  - lib/importmap-rails.rb
40
40
  - lib/importmap/engine.rb
41
- - lib/importmap/paths.rb
41
+ - lib/importmap/map.rb
42
42
  - lib/importmap/version.rb
43
43
  - lib/install/install.rb
44
44
  - lib/shim.js