Last active
November 29, 2016 09:32
-
-
Save kares/0614a2d5c019b0b9d84b375c90da2236 to your computer and use it in GitHub Desktop.
SASS (asset) path/url functions patch to make sure non absolute /assets URLs are returned (e.g. for bootstrap-sass)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if File.basename($0) == 'rake' && ARGV.include?('assets:precompile') # Rails.env.production? | |
puts "patching sass functions to return relative asset paths" if $VERBOSE | |
begin | |
require 'sprockets/sass/functions' | |
sass_functions = Sprockets::Sass::Functions | |
Autoload::Sass::Script rescue nil # loading | |
rescue LoadError # 2.x | |
require 'sprockets/sass_functions' | |
sass_functions = Sprockets::SassFunctions | |
end | |
sass_functions.module_eval do | |
# relativize path - called from sass (assuming a flat public/assets structure) : | |
# e.g. src:url("/assets/glyphicons-halflings-regular-xxx.eot") will end up | |
# as src:url("glyphicons-halflings-regular-xxx.eot") | |
# NOTE: currently this likely won't work for target assets not on assets.paths | |
# ... or when an (including) asset that ends up in the /assets sub-directory ! | |
%w{ image_path font_path audio_path video_path javascript_path stylesheet_path }.each do |method| | |
class_eval <<-RUBY, __FILE__, __LINE__ + 1 | |
def #{method}(source, options = {}) | |
asset_prefix = Rails.application.config.assets.prefix | |
path = sprockets_context.font_path(source.value) | |
::Sass::Script::String.new(path.sub(File.join(asset_prefix, '/'), ''), :string) | |
end | |
RUBY | |
end | |
%w{ image_url font_url audio_url video_url javascript_url stylesheet_url }.each do |method| | |
class_eval <<-RUBY, __FILE__, __LINE__ + 1 | |
def #{method}(source, options = {}) | |
asset_prefix = Rails.application.config.assets.prefix | |
path = sprockets_context.font_path(source.value) | |
::Sass::Script::String.new("url(" + path.sub(File.join(asset_prefix, '/'), '') + ")") | |
end | |
RUBY | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment