importmap-rails-assets 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 67b596d323bc11732f89708c18e07718a128aed399be33850fedd93f95dc5eb6
4
+ data.tar.gz: b313e73217930ca3971c100d8a07e3cd898d6640ca9e69ea4788441427b6e652
5
+ SHA512:
6
+ metadata.gz: 60c612edb6cb19a60ad77aa543098931facf0a575bfc9f1379e1a785d6c6882c5edfe0d8a2ca2892789ccaca8392e46dc87215a21021873932f96076e2793862
7
+ data.tar.gz: 3e5e557203d8fc9d99775df1c93e4e8305025520e135782a32638720d951d03b6736091ded614135c4b30a0fdab366d8754a85a0954fb828b34e1b41b6751e5e
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # importmap-rails-assets
2
+
3
+ Vendor arbitrary assets (CSS, fonts, images, etc.) from packages pinned with [importmap-rails](https://github.com/rails/importmap-rails).
4
+
5
+ ## Installation
6
+
7
+ Add to your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'importmap-rails-assets'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Define asset mappings in `config/assets.yml`. Each key is an npm package name (as pinned in your importmap), and the value is a hash of source paths within the package to local destination paths:
16
+
17
+ ```yaml
18
+ tom-select:
19
+ dist/css/tom-select.bootstrap5.css: vendor/assets/stylesheets/tom-select.css
20
+
21
+ flatpickr:
22
+ dist/flatpickr.css: vendor/assets/stylesheets/flatpickr.css
23
+ dist/themes/dark.css: vendor/assets/stylesheets/flatpickr-dark.css
24
+ ```
25
+
26
+ Then pin the package as usual:
27
+
28
+ ```
29
+ $ bin/importmap pin flatpickr
30
+ Pinning "flatpickr" to vendor/javascript/flatpickr.js via download from https://ga.jspm.io/npm:flatpickr@4.6.13/dist/flatpickr.js
31
+ Downloading "flatpickr/dist/flatpickr.css" to vendor/assets/stylesheets/flatpickr.css
32
+ Downloading "flatpickr/dist/themes/dark.css" to vendor/assets/stylesheets/flatpickr-dark.css
33
+ ```
34
+
35
+ `bin/importmap update` and `bin/importmap pristine` also download assets in the same way.
36
+
37
+ `bin/importmap unpin` removes the corresponding asset files as well.
38
+
39
+ Packages without an entry in `config/assets.yml` are silently skipped.
40
+
41
+ ## License
42
+
43
+ MIT
@@ -0,0 +1,7 @@
1
+ require 'importmap-rails'
2
+ require 'importmap/packager'
3
+
4
+ require_relative 'importmap_rails_assets/version'
5
+ require_relative 'importmap_rails_assets/packager_extension'
6
+
7
+ Importmap::Packager.prepend ImportmapRailsAssets::PackagerExtension
@@ -0,0 +1,54 @@
1
+ require 'net/http'
2
+ require 'yaml'
3
+ require 'fileutils'
4
+
5
+ module ImportmapRailsAssets
6
+ module PackagerExtension
7
+ def download(package, url)
8
+ super
9
+
10
+ version = extract_package_version_from(url)&.delete_prefix('@')
11
+ return unless version
12
+
13
+ asset_mappings = load_asset_mappings(package)
14
+ return if asset_mappings.empty?
15
+
16
+ asset_mappings.each do |source, dest|
17
+ download_asset(package, version, source, dest)
18
+ end
19
+ end
20
+
21
+ def remove(package)
22
+ super
23
+
24
+ load_asset_mappings(package).each_value do |dest|
25
+ path = Pathname.new(dest)
26
+ FileUtils.rm_f(path)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def load_asset_mappings(package)
33
+ config_path = Pathname.new('config/assets.yml')
34
+ return {} unless config_path.exist?
35
+
36
+ config = YAML.safe_load_file(config_path) || {}
37
+ config[package] || {}
38
+ end
39
+
40
+ def download_asset(package, version, source, dest)
41
+ uri = URI("https://cdn.jsdelivr.net/npm/#{package}@#{version}/#{source}")
42
+
43
+ response = Net::HTTP.get_response(uri)
44
+ return unless response.is_a?(Net::HTTPSuccess)
45
+
46
+ dest_path = Pathname.new(dest)
47
+ FileUtils.mkdir_p dest_path.dirname
48
+
49
+ File.binwrite(dest_path, response.body)
50
+
51
+ puts %(Downloading "#{package}/#{source}" to #{dest})
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module ImportmapRailsAssets
2
+ VERSION = '0.2.0'
3
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: importmap-rails-assets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - ursm
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: importmap-rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '2.0'
26
+ executables: []
27
+ extensions: []
28
+ extra_rdoc_files: []
29
+ files:
30
+ - README.md
31
+ - lib/importmap-rails-assets.rb
32
+ - lib/importmap_rails_assets/packager_extension.rb
33
+ - lib/importmap_rails_assets/version.rb
34
+ homepage: https://github.com/ursm/importmap-rails-assets
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '3.3'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 4.0.6
53
+ specification_version: 4
54
+ summary: Vendor arbitrary assets from pinned importmap packages
55
+ test_files: []