Skip to content

Commit 545d3c6

Browse files
authored
Merge pull request #96 from jamesmartin/webpacker-support
Support Webpacker
2 parents 9cdc503 + 2dc6d21 commit 545d3c6

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased][unreleased]
6-
- Nothing
6+
### Added
7+
- Support for finding assets bundled by Webpacker
8+
[#96](https://github.com/jamesmartin/inline_svg/pull/96)
79

810
## [1.4.0] - 2019-04-19
911
### Fixed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Styling a SVG document with CSS for use on the web is most reliably achieved by
66
[adding classes to the document and
77
embedding](http://css-tricks.com/using-svg/) it inline in the HTML.
88

9-
This gem adds a Rails helper method (`inline_svg`) that reads an SVG document (via Sprockets, so works with the Rails Asset Pipeline), applies a CSS class attribute to the root of the document and
9+
This gem adds a Rails helper method (`inline_svg`) that reads an SVG document (via Sprockets or Webpacker, so works with the Rails Asset Pipeline), applies a CSS class attribute to the root of the document and
1010
then embeds it into a view.
1111

1212
Inline SVG supports [Rails 3](http://weblog.rubyonrails.org/2010/8/29/rails-3-0-it-s-done/) (from [v0.12.0](https://github.com/jamesmartin/inline_svg/releases/tag/v0.12.0)), [Rails 4](http://weblog.rubyonrails.org/2013/6/25/Rails-4-0-final/) and [Rails 5](http://weblog.rubyonrails.org/2016/6/30/Rails-5-0-final/) (from [v0.10.0](https://github.com/jamesmartin/inline_svg/releases/tag/v0.10.0)).
@@ -303,6 +303,29 @@ InlineSvg.configure do |config|
303303
end
304304
```
305305

306+
## Sprockets and Webpacker
307+
308+
Inline SVG supports SVGs bundled by either Sprockets or Webpacker, however, be
309+
aware that the gem will *always* attempt to find SVGs using Sprockts if it is
310+
enabled.
311+
312+
By default, Inline SVG will use Sprockets to find SVG files if it is enabled in
313+
your Rails project.
314+
315+
If you have upgraded an older Rails project from Sprockets to Webpacker and you
316+
no longer want to use Sprockets at all, you should disable the Asset Pipeline
317+
and Inline SVG will use Webpacker automatically.
318+
319+
If you have both Sprockets *and* Webpacker enabled for some reason and you want
320+
Inline SVG to use Webpacker to find SVGs then you should configure the
321+
`asset_finder` appropriately:
322+
323+
```ruby
324+
InlineSvg.configure do |config|
325+
config.asset_finder = InlineSvg::WebpackAssetFinder
326+
end
327+
```
328+
306329
## Contributing
307330

308331
1. Fork it ( [http://github.com/jamesmartin/inline_svg/fork](http://github.com/jamesmartin/inline_svg/fork) )

lib/inline_svg.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "inline_svg/cached_asset_file"
55
require "inline_svg/finds_asset_paths"
66
require "inline_svg/static_asset_finder"
7+
require "inline_svg/webpack_asset_finder"
78
require "inline_svg/transform_pipeline"
89
require "inline_svg/io_resource"
910

lib/inline_svg/railtie.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ class Railtie < ::Rails::Railtie
1515
# Only set this when a user-configured asset finder has not been
1616
# configured already.
1717
if config.asset_finder.nil?
18-
config.asset_finder = app.instance_variable_get(:@assets)
18+
if assets = app.instance_variable_get(:@assets)
19+
config.asset_finder = assets
20+
elsif defined?(Webpacker)
21+
config.asset_finder = InlineSvg::WebpackAssetFinder
22+
end
1923
end
2024
end
2125
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module InlineSvg
2+
class WebpackAssetFinder
3+
def self.find_asset(filename)
4+
new(filename)
5+
end
6+
7+
def initialize(filename)
8+
@filename = filename
9+
end
10+
11+
def pathname
12+
public_path = Webpacker.config.public_path
13+
file_path = Webpacker.instance.manifest.lookup(@filename)
14+
return unless public_path && file_path
15+
16+
File.join(public_path, file_path)
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)