1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
require 'rack/utils'
require 'sprockets/autoload'
require 'uri'
module Sprockets
# Processor engine class for the SASS/SCSS compiler. Depends on the `sass` gem.
#
# For more infomation see:
#
# https://github.com/sass/sass
# https://github.com/rails/sass-rails
#
class SassProcessor
autoload :CacheStore, 'sprockets/sass_cache_store'
# Internal: Defines default sass syntax to use. Exposed so the ScssProcessor
# may override it.
def self.syntax
:sass
end
# Public: Return singleton instance with default options.
#
# Returns SassProcessor object.
def self.instance
@instance ||= new
end
def self.call(input)
instance.call(input)
end
def self.cache_key
instance.cache_key
end
attr_reader :cache_key
# Public: Initialize template with custom options.
#
# options - Hash
# cache_version - String custom cache version. Used to force a cache
# change after code changes are made to Sass Functions.
#
def initialize(options = {}, &block)
@cache_version = options[:cache_version]
@cache_key = "#{self.class.name}:#{VERSION}:#{Autoload::Sass::VERSION}:#{@cache_version}".freeze
@functions = Module.new do
include Functions
include options[:functions] if options[:functions]
class_eval(&block) if block_given?
end
end
def call(input)
context = input[:environment].context_class.new(input)
options = {
filename: input[:filename],
syntax: self.class.syntax,
cache_store: build_cache_store(input, @cache_version),
load_paths: input[:environment].paths,
sprockets: {
context: context,
environment: input[:environment],
dependencies: context.metadata[:dependencies]
}
}
engine = Autoload::Sass::Engine.new(input[:data], options)
css = Utils.module_include(Autoload::Sass::Script::Functions, @functions) do
engine.render
end
# Track all imported files
sass_dependencies = Set.new([input[:filename]])
engine.dependencies.map do |dependency|
sass_dependencies << dependency.options[:filename]
context.metadata[:dependencies] << URIUtils.build_file_digest_uri(dependency.options[:filename])
end
context.metadata.merge(data: css, sass_dependencies: sass_dependencies)
end
# Public: Build the cache store to be used by the Sass engine.
#
# input - the input hash.
# version - the cache version.
#
# Override this method if you need to use a different cache than the
# Sprockets cache.
def build_cache_store(input, version)
CacheStore.new(input[:cache], version)
end
private :build_cache_store
# Public: Functions injected into Sass context during Sprockets evaluation.
#
# This module may be extended to add global functionality to all Sprockets
# Sass environments. Though, scoping your functions to just your environment
# is preferred.
#
# module Sprockets::SassProcessor::Functions
# def asset_path(path, options = {})
# end
# end
#
module Functions
# Public: Generate a url for asset path.
#
# Default implementation is deprecated. Currently defaults to
# Context#asset_path.
#
# Will raise NotImplementedError in the future. Users should provide their
# own base implementation.
#
# Returns a Sass::Script::String.
def asset_path(path, options = {})
path = path.value
path, _, query, fragment = URI.split(path)[5..8]
path = sprockets_context.asset_path(path, options)
query = "?#{query}" if query
fragment = "##{fragment}" if fragment
Autoload::Sass::Script::String.new("#{path}#{query}#{fragment}", :string)
end
# Public: Generate a asset url() link.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def asset_url(path, options = {})
Autoload::Sass::Script::String.new("url(#{asset_path(path, options).value})")
end
# Public: Generate url for image path.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def image_path(path)
asset_path(path, type: :image)
end
# Public: Generate a image url() link.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def image_url(path)
asset_url(path, type: :image)
end
# Public: Generate url for video path.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def video_path(path)
asset_path(path, type: :video)
end
# Public: Generate a video url() link.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def video_url(path)
asset_url(path, type: :video)
end
# Public: Generate url for audio path.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def audio_path(path)
asset_path(path, type: :audio)
end
# Public: Generate a audio url() link.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def audio_url(path)
asset_url(path, type: :audio)
end
# Public: Generate url for font path.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def font_path(path)
asset_path(path, type: :font)
end
# Public: Generate a font url() link.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def font_url(path)
asset_url(path, type: :font)
end
# Public: Generate url for javascript path.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def javascript_path(path)
asset_path(path, type: :javascript)
end
# Public: Generate a javascript url() link.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def javascript_url(path)
asset_url(path, type: :javascript)
end
# Public: Generate url for stylesheet path.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def stylesheet_path(path)
asset_path(path, type: :stylesheet)
end
# Public: Generate a stylesheet url() link.
#
# path - Sass::Script::String URL path
#
# Returns a Sass::Script::String.
def stylesheet_url(path)
asset_url(path, type: :stylesheet)
end
# Public: Generate a data URI for asset path.
#
# path - Sass::Script::String logical asset path
#
# Returns a Sass::Script::String.
def asset_data_url(path)
url = sprockets_context.asset_data_uri(path.value)
Autoload::Sass::Script::String.new("url(" + url + ")")
end
protected
# Public: The Environment.
#
# Returns Sprockets::Environment.
def sprockets_environment
options[:sprockets][:environment]
end
# Public: Mutatable set of dependencies.
#
# Returns a Set.
def sprockets_dependencies
options[:sprockets][:dependencies]
end
# Deprecated: Get the Context instance. Use APIs on
# sprockets_environment or sprockets_dependencies directly.
#
# Returns a Context instance.
def sprockets_context
options[:sprockets][:context]
end
end
end
class ScssProcessor < SassProcessor
def self.syntax
:scss
end
end
# Deprecated: Use Sprockets::SassProcessor::Functions instead.
SassFunctions = SassProcessor::Functions
end
|