rails_vite-inline_svg 0.1.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 +7 -0
- data/CHANGELOG.md +7 -0
- data/LICENSE +21 -0
- data/README.md +47 -0
- data/lib/rails_vite/inline_svg/asset_resolver.rb +43 -0
- data/lib/rails_vite/inline_svg/railtie.rb +11 -0
- data/lib/rails_vite/inline_svg/svg_file.rb +25 -0
- data/lib/rails_vite/inline_svg/version.rb +5 -0
- data/lib/rails_vite/inline_svg/view_helpers.rb +14 -0
- data/lib/rails_vite/inline_svg.rb +17 -0
- metadata +148 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 52bff9def29117ac53904da89ce29b02d15d1de1ffdbb8fc423e086a7f004a99
|
|
4
|
+
data.tar.gz: 16aacd5ae6d40da891cb500708fe9d5952ca7ce5400c6fe5bbe094ace75e6abd
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 35307c516ad96570be1431cbadcdaaea5b00de5e97445f6aed07b4f774999f53ade841eace47e640c72358d186434fa12b6094c1106423caf4180fa8cafc0817
|
|
7
|
+
data.tar.gz: 2459055054ad7415b2e9fe1afa61eb2e57731d70657d2514f7ac69e5b3c16ed499e0018ae90ff85275559be191d29cb1be12bd7276100ac8b6aa17ca2911ef26
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Displayful
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# rails_vite-inline_svg
|
|
2
|
+
|
|
3
|
+
Inline SVGs from a [rails_vite](https://github.com/skryukov/rails_vite) build. Each file is parsed once; a missing one raises.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem 'rails_vite-inline_svg'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The SVGs must be in the Vite build, so glob them from an entry point:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import.meta.glob(['../images/**', '/node_modules/lucide-static/icons/bell.svg'], { eager: true })
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Use
|
|
18
|
+
|
|
19
|
+
```erb
|
|
20
|
+
<%= inline_svg_tag 'images/logo.svg', class: 'size-5', aria_hidden: true %>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- Paths are under rails_vite's `source_dir`, except `node_modules/…`, used as is.
|
|
24
|
+
- Given attributes replace the file's own on the `<svg>` root; underscores in keys become dashes.
|
|
25
|
+
- Raises `RailsVite::InlineSvg::MissingAssetError` for a file that isn't in the build. In tests that run against a test build, an unlisted file fails the test.
|
|
26
|
+
|
|
27
|
+
The helper is added to every view. ViewComponent components don't get view helpers, so delegate it:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
class ApplicationComponent < ViewComponent::Base
|
|
31
|
+
delegate :inline_svg_tag, to: :helpers
|
|
32
|
+
end
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## How it works
|
|
36
|
+
|
|
37
|
+
- `AssetResolver.read(path)` returns a file's bytes: from the Vite dev server while it runs, otherwise the built file via the manifest, kept in memory.
|
|
38
|
+
- `SvgFile.read(path)` parses it into `attributes` and `inner_html`, cached by contents, so a built file is parsed once and an edited one in development afresh.
|
|
39
|
+
|
|
40
|
+
## Development
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
bundle install
|
|
44
|
+
bundle exec rake test
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The tests boot a minimal Rails app (`test/test_app.rb`) against a fixture build in `test/public/vite-test`.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require 'open-uri'
|
|
2
|
+
|
|
3
|
+
module RailsVite
|
|
4
|
+
module InlineSvg
|
|
5
|
+
# Raw bytes of a Vite-managed asset: the dev server in development, the built
|
|
6
|
+
# file via the manifest in production (memoized, since builds are immutable).
|
|
7
|
+
module AssetResolver
|
|
8
|
+
@cache = {}
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
def read(path)
|
|
12
|
+
source = source_path(path)
|
|
13
|
+
|
|
14
|
+
if config.dev_server_running?
|
|
15
|
+
fetch_from_dev_server(source)
|
|
16
|
+
else
|
|
17
|
+
@cache[source] ||= read_built_file(source).freeze
|
|
18
|
+
end
|
|
19
|
+
rescue RailsVite::MissingEntryError, OpenURI::HTTPError, SystemCallError, SocketError => e
|
|
20
|
+
raise MissingAssetError, "#{path} not found (#{e.class})"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def config
|
|
26
|
+
RailsVite.config
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def source_path(path)
|
|
30
|
+
path.start_with?('node_modules/') ? path : "#{config.source_dir}/#{path}"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def fetch_from_dev_server(source)
|
|
34
|
+
URI("#{config.dev_server_url}/#{source.delete_prefix('/')}").read
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def read_built_file(source)
|
|
38
|
+
Rails.public_path.join(config.build_dir, RailsVite.manifest.path_for(source)).read
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module RailsVite
|
|
2
|
+
module InlineSvg
|
|
3
|
+
SvgFile = Data.define(:attributes, :inner_html) do
|
|
4
|
+
def self.read(path)
|
|
5
|
+
parse(AssetResolver.read(path))
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# Keyed by the file's contents: a built file is one frozen string, so it's
|
|
9
|
+
# parsed once and the key is that same string, not a copy; in development
|
|
10
|
+
# an edited file is parsed afresh.
|
|
11
|
+
def self.parse(source)
|
|
12
|
+
@parsed ||= {}
|
|
13
|
+
@parsed[source] ||= begin
|
|
14
|
+
svg = Nokogiri::XML(source).root
|
|
15
|
+
|
|
16
|
+
new(
|
|
17
|
+
# Deduplicated, so icons share the attribute values they have in common.
|
|
18
|
+
attributes: svg.namespaces.merge(svg.to_h).transform_values(&:dedup).freeze,
|
|
19
|
+
inner_html: svg.inner_html.html_safe # rubocop:disable Rails/OutputSafety
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module RailsVite
|
|
2
|
+
module InlineSvg
|
|
3
|
+
module ViewHelpers
|
|
4
|
+
# Inlines a Vite-resolved SVG with the given attributes (underscores become
|
|
5
|
+
# dashes) set on its root. Raises MissingAssetError for an asset that isn't
|
|
6
|
+
# in the build.
|
|
7
|
+
def inline_svg_tag(source_path, attributes = {})
|
|
8
|
+
svg = SvgFile.read(source_path)
|
|
9
|
+
|
|
10
|
+
tag.svg(svg.inner_html, **svg.attributes, **attributes.transform_keys { |key| key.to_s.dasherize })
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'nokogiri'
|
|
2
|
+
require 'rails_vite'
|
|
3
|
+
|
|
4
|
+
module RailsVite
|
|
5
|
+
# Inlines SVGs from a rails_vite build: the bytes from the dev server or the
|
|
6
|
+
# built file, parsed once, rendered with the caller's attributes.
|
|
7
|
+
module InlineSvg
|
|
8
|
+
class Error < StandardError; end
|
|
9
|
+
class MissingAssetError < Error; end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
require_relative 'inline_svg/version'
|
|
14
|
+
require_relative 'inline_svg/asset_resolver'
|
|
15
|
+
require_relative 'inline_svg/svg_file'
|
|
16
|
+
require_relative 'inline_svg/view_helpers'
|
|
17
|
+
require_relative 'inline_svg/railtie' if defined?(Rails::Railtie)
|
metadata
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rails_vite-inline_svg
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Displayful
|
|
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: actionview
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '8.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '8.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: nokogiri
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '1.16'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '1.16'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rails_vite
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0.3'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0.3'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: actionpack
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: minitest
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
type: :development
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
- !ruby/object:Gem::Dependency
|
|
83
|
+
name: railties
|
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
type: :development
|
|
90
|
+
prerelease: false
|
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
- !ruby/object:Gem::Dependency
|
|
97
|
+
name: rake
|
|
98
|
+
requirement: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '0'
|
|
103
|
+
type: :development
|
|
104
|
+
prerelease: false
|
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0'
|
|
110
|
+
executables: []
|
|
111
|
+
extensions: []
|
|
112
|
+
extra_rdoc_files: []
|
|
113
|
+
files:
|
|
114
|
+
- CHANGELOG.md
|
|
115
|
+
- LICENSE
|
|
116
|
+
- README.md
|
|
117
|
+
- lib/rails_vite/inline_svg.rb
|
|
118
|
+
- lib/rails_vite/inline_svg/asset_resolver.rb
|
|
119
|
+
- lib/rails_vite/inline_svg/railtie.rb
|
|
120
|
+
- lib/rails_vite/inline_svg/svg_file.rb
|
|
121
|
+
- lib/rails_vite/inline_svg/version.rb
|
|
122
|
+
- lib/rails_vite/inline_svg/view_helpers.rb
|
|
123
|
+
homepage: https://github.com/displayful/rails_vite-inline_svg
|
|
124
|
+
licenses:
|
|
125
|
+
- MIT
|
|
126
|
+
metadata:
|
|
127
|
+
source_code_uri: https://github.com/displayful/rails_vite-inline_svg
|
|
128
|
+
changelog_uri: https://github.com/displayful/rails_vite-inline_svg/blob/main/CHANGELOG.md
|
|
129
|
+
bug_tracker_uri: https://github.com/displayful/rails_vite-inline_svg/issues
|
|
130
|
+
rubygems_mfa_required: 'true'
|
|
131
|
+
rdoc_options: []
|
|
132
|
+
require_paths:
|
|
133
|
+
- lib
|
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - ">="
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '3.4'
|
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
|
+
requirements:
|
|
141
|
+
- - ">="
|
|
142
|
+
- !ruby/object:Gem::Version
|
|
143
|
+
version: '0'
|
|
144
|
+
requirements: []
|
|
145
|
+
rubygems_version: 4.0.20
|
|
146
|
+
specification_version: 4
|
|
147
|
+
summary: Inline SVGs from a rails_vite build, parsed once.
|
|
148
|
+
test_files: []
|