importmap-rails 0.7.6 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,8 +10,11 @@ class Importmap::Packager
10
10
  singleton_class.attr_accessor :endpoint
11
11
  self.endpoint = URI("https://api.jspm.io/generate")
12
12
 
13
- def initialize(importmap_path = "config/importmap.rb")
14
- @importmap_path = importmap_path
13
+ attr_reader :vendor_path
14
+
15
+ def initialize(importmap_path = "config/importmap.rb", vendor_path: "vendor/javascript")
16
+ @importmap_path = Pathname.new(importmap_path)
17
+ @vendor_path = Pathname.new(vendor_path)
15
18
  end
16
19
 
17
20
  def import(*packages, env: "production", from: "jspm")
@@ -33,15 +36,43 @@ class Importmap::Packager
33
36
  %(pin "#{package}", to: "#{url}")
34
37
  end
35
38
 
39
+ def vendored_pin_for(package, url)
40
+ filename = package_filename(package)
41
+ version = extract_package_version_from(url)
42
+
43
+ if "#{package}.js" == filename
44
+ %(pin "#{package}" # #{version})
45
+ else
46
+ %(pin "#{package}", to: "#{filename}" # #{version})
47
+ end
48
+ end
49
+
36
50
  def packaged?(package)
37
51
  importmap.match(/^pin "#{package}".*$/)
38
52
  end
39
53
 
54
+ def download(package, url)
55
+ ensure_vendor_directory_exists
56
+ remove_existing_package_file(package)
57
+ download_package_file(package, url)
58
+ end
59
+
60
+ def remove(package)
61
+ remove_existing_package_file(package)
62
+ remove_package_from_importmap(package)
63
+ end
64
+
40
65
  private
66
+ def post_json(body)
67
+ Net::HTTP.post(self.class.endpoint, body.to_json, "Content-Type" => "application/json")
68
+ rescue => error
69
+ raise HTTPError, "Unexpected transport error (#{error.class}: #{error.message})"
70
+ end
71
+
41
72
  def extract_parsed_imports(response)
42
73
  JSON.parse(response.body).dig("map", "imports")
43
74
  end
44
-
75
+
45
76
  def handle_failure_response(response)
46
77
  if error_message = parse_service_error(response)
47
78
  raise ServiceError, error_message
@@ -56,13 +87,57 @@ class Importmap::Packager
56
87
  nil
57
88
  end
58
89
 
59
- def post_json(body)
60
- Net::HTTP.post(self.class.endpoint, body.to_json, { "Content-Type" => "application/json" })
61
- rescue => error
62
- raise HTTPError, "Unexpected transport error (#{error.class}: #{error.message})"
63
- end
64
-
65
90
  def importmap
66
91
  @importmap ||= File.read(@importmap_path)
67
92
  end
93
+
94
+
95
+ def ensure_vendor_directory_exists
96
+ FileUtils.mkdir_p @vendor_path
97
+ end
98
+
99
+ def remove_existing_package_file(package)
100
+ FileUtils.rm_rf vendored_package_path(package)
101
+ end
102
+
103
+ def remove_package_from_importmap(package)
104
+ all_lines = File.readlines(@importmap_path)
105
+ with_lines_removed = all_lines.select { |line| line !~ /pin "#{package}"/ }
106
+
107
+ File.open(@importmap_path, "w") do |file|
108
+ with_lines_removed.each { |line| file.write(line) }
109
+ end
110
+ end
111
+
112
+ def download_package_file(package, url)
113
+ response = Net::HTTP.get_response(URI(url))
114
+
115
+ if response.code == "200"
116
+ save_vendored_package(package, response.body)
117
+ else
118
+ handle_failure_response(response)
119
+ end
120
+ end
121
+
122
+ def save_vendored_package(package, source)
123
+ File.open(vendored_package_path(package), "w+") do |vendored_package|
124
+ vendored_package.write remove_sourcemap_comment_from(source).force_encoding("UTF-8")
125
+ end
126
+ end
127
+
128
+ def remove_sourcemap_comment_from(source)
129
+ source.gsub(/^\/\/# sourceMappingURL=.*/, "")
130
+ end
131
+
132
+ def vendored_package_path(package)
133
+ @vendor_path.join(package_filename(package))
134
+ end
135
+
136
+ def package_filename(package)
137
+ package.gsub("/", "--") + ".js"
138
+ end
139
+
140
+ def extract_package_version_from(url)
141
+ url.match(/@\d+\.\d+\.\d+/)&.to_a&.first
142
+ end
68
143
  end
@@ -11,7 +11,7 @@ class Importmap::Reloader
11
11
  end
12
12
 
13
13
  def import_map_paths
14
- config.paths["config/importmap.rb"].existent
14
+ config.importmap.paths
15
15
  end
16
16
 
17
17
  def config
@@ -1,3 +1,3 @@
1
1
  module Importmap
2
- VERSION = "0.7.6"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -1,6 +1,3 @@
1
- # Use direct uploads for Active Storage (remember to import "@rails/activestorage" in your application.js)
2
- # pin "@rails/activestorage", to: "activestorage.esm.js"
1
+ # Pin npm packages by running ./bin/importmap
3
2
 
4
- # Use npm packages from a JavaScript CDN by running ./bin/importmap
5
-
6
- pin "application"
3
+ pin "application", preload: true
@@ -14,9 +14,14 @@ create_file Rails.root.join("app/javascript/application.js") do <<-JS
14
14
  JS
15
15
  end
16
16
 
17
+ say "Use vendor/javascript for downloaded pins"
18
+ empty_directory "vendor/javascript"
19
+ keep_file "vendor/javascript"
20
+
17
21
  if (sprockets_manifest_path = Rails.root.join("app/assets/config/manifest.js")).exist?
18
22
  say "Ensure JavaScript files are in the Sprocket manifest"
19
- append_to_file sprockets_manifest_path, %(//= link_tree ../../javascript .js\n)
23
+ append_to_file sprockets_manifest_path,
24
+ %(//= link_tree ../../javascript .js\n//= link_tree ../../../vendor/javascript .js\n)
20
25
  end
21
26
 
22
27
  say "Configure importmap paths in config/importmap.rb"
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.7.6
4
+ version: 0.9.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-09-29 00:00:00.000000000 Z
11
+ date: 2021-11-23 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: []
@@ -34,6 +34,8 @@ files:
34
34
  - README.md
35
35
  - Rakefile
36
36
  - app/assets/javascripts/es-module-shims.js
37
+ - app/assets/javascripts/es-module-shims.js.map
38
+ - app/assets/javascripts/es-module-shims.min.js
37
39
  - app/helpers/importmap/importmap_tags_helper.rb
38
40
  - lib/importmap-rails.rb
39
41
  - lib/importmap/commands.rb
@@ -53,7 +55,7 @@ licenses:
53
55
  metadata:
54
56
  homepage_uri: https://github.com/rails/importmap-rails
55
57
  source_code_uri: https://github.com/rails/importmap-rails
56
- post_install_message:
58
+ post_install_message:
57
59
  rdoc_options: []
58
60
  require_paths:
59
61
  - lib
@@ -61,15 +63,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
63
  requirements:
62
64
  - - ">="
63
65
  - !ruby/object:Gem::Version
64
- version: '0'
66
+ version: 2.7.0
65
67
  required_rubygems_version: !ruby/object:Gem::Requirement
66
68
  requirements:
67
69
  - - ">="
68
70
  - !ruby/object:Gem::Version
69
71
  version: '0'
70
72
  requirements: []
71
- rubygems_version: 3.1.4
72
- signing_key:
73
+ rubygems_version: 3.2.22
74
+ signing_key:
73
75
  specification_version: 4
74
76
  summary: Use ESM with importmap to manage modern JavaScript in Rails without transpiling
75
77
  or bundling.