Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/roger_sassc/sassc/asset_functions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "sassc"
require "base64"
require "mime/types"
require "digest"

module RogerSassc
Expand All @@ -21,6 +23,20 @@ def asset_path(path)
::SassC::Script::String.new("url(#{finger_printed_path})", :identifier)
end

def inline_base64(path)
file = find_file(path)

if File.exist?(file)
encoded_file = Base64.encode64(File.open(file, "rb").read)
mime_type = MIME::Types.type_for(File.basename(file)).first
base64_string = "data:#{mime_type};base64,#{encoded_file}"
else
base64_string = path.value
end

::SassC::Script::String.new("url(\"#{base64_string}\")", :identifier)
end

private

# Translate the given path to a path on disk
Expand Down
1 change: 1 addition & 0 deletions roger_sassc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "sassc", "~> 1.2.0"
spec.add_dependency "roger", "~> 1.0"
spec.add_dependency "mime-types", ["~> 2.2"]
spec.add_dependency "rack"

spec.add_development_dependency "rake", "~> 10.0"
Expand Down
7 changes: 7 additions & 0 deletions test/asset_functions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def test_asset_path_fingerprinting_absolute_path
# rubocop:enable LineLength
end

def test_inline_base_absolute_path
scss_path = "test/fixtures/asset_functions/inline_base_absolute_path.scss"
engine = get_engine_for(scss_path)

assert_match 'background-image: url("data:image/svg+xml;base64,', engine.render
end

private

def get_engine_for(scss_path)
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/asset_functions/inline_base_absolute_path.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div {
background-image: inline-base64('/images/logo.svg');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings

}