|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | require "sprockets/version"
|
4 |
| - |
5 |
| -begin |
6 |
| - require 'sprockets/sass_processor' |
7 |
| -rescue LoadError |
8 |
| - require "sprockets/sass_template" |
9 |
| -end |
10 |
| - |
| 4 | +require 'sprockets/sass_processor' |
11 | 5 | require "sprockets/utils"
|
12 | 6 |
|
13 | 7 | module SassC::Rails
|
14 |
| - |
15 |
| - class SassTemplate < defined?(Sprockets::SassProcessor) ? Sprockets::SassProcessor : Sprockets::SassTemplate |
16 |
| - module Sprockets3 |
17 |
| - def call(input) |
18 |
| - context = input[:environment].context_class.new(input) |
19 |
| - |
20 |
| - options = { |
21 |
| - filename: input[:filename], |
22 |
| - line_comments: line_comments?, |
23 |
| - syntax: self.class.syntax, |
24 |
| - load_paths: input[:environment].paths, |
25 |
| - importer: SassC::Rails::Importer, |
26 |
| - sprockets: { |
27 |
| - context: context, |
28 |
| - environment: input[:environment], |
29 |
| - dependencies: context.metadata[:dependency_paths] |
30 |
| - } |
31 |
| - }.merge(config_options) { |*args| safe_merge(*args) } |
32 |
| - |
33 |
| - engine = ::SassC::Engine.new(input[:data], options) |
34 |
| - |
35 |
| - css = Sprockets::Utils.module_include(::SassC::Script::Functions, @functions) do |
36 |
| - engine.render |
37 |
| - end |
38 |
| - |
39 |
| - context.metadata.merge(data: css) |
| 8 | + class SassTemplate < Sprockets::SassProcessor |
| 9 | + def initialize(options = {}, &block) |
| 10 | + @cache_version = options[:cache_version] |
| 11 | + @cache_key = "#{self.class.name}:#{VERSION}:#{SassC::VERSION}:#{@cache_version}".freeze |
| 12 | + #@importer_class = options[:importer] || Sass::Importers::Filesystem |
| 13 | + @sass_config = options[:sass_config] || {} |
| 14 | + @functions = Module.new do |
| 15 | + include Functions |
| 16 | + include options[:functions] if options[:functions] |
| 17 | + class_eval(&block) if block_given? |
40 | 18 | end
|
41 | 19 | end
|
42 | 20 |
|
43 |
| - module Sprockets2 |
44 |
| - def self.included(base) |
45 |
| - base.class_eval do |
46 |
| - self.default_mime_type = "text/css" |
47 |
| - end |
| 21 | + def call(input) |
| 22 | + context = input[:environment].context_class.new(input) |
| 23 | + |
| 24 | + options = { |
| 25 | + filename: input[:filename], |
| 26 | + line_comments: line_comments?, |
| 27 | + syntax: self.class.syntax, |
| 28 | + load_paths: input[:environment].paths, |
| 29 | + importer: SassC::Rails::Importer, |
| 30 | + sprockets: { |
| 31 | + context: context, |
| 32 | + environment: input[:environment], |
| 33 | + dependencies: context.metadata[:dependency_paths] |
| 34 | + } |
| 35 | + }.merge(config_options) { |*args| safe_merge(*args) } |
| 36 | + |
| 37 | + engine = ::SassC::Engine.new(input[:data], options) |
| 38 | + |
| 39 | + css = Sprockets::Utils.module_include(::SassC::Script::Functions, @functions) do |
| 40 | + engine.render |
48 | 41 | end
|
49 | 42 |
|
50 |
| - def evaluate(context, locals, &block) |
51 |
| - options = { |
52 |
| - filename: eval_file, |
53 |
| - line_comments: line_comments?, |
54 |
| - syntax: syntax, |
55 |
| - load_paths: context.environment.paths, |
56 |
| - importer: SassC::Rails::Importer, |
57 |
| - sprockets: { |
58 |
| - context: context, |
59 |
| - environment: context.environment |
60 |
| - } |
61 |
| - }.merge(config_options, &method(:safe_merge)) |
62 |
| - |
63 |
| - ::SassC::Engine.new(data, options).render |
64 |
| - end |
65 |
| - end |
66 |
| - |
67 |
| - if Sprockets::VERSION > "3.0.0" |
68 |
| - include Sprockets3 |
69 |
| - else |
70 |
| - include Sprockets2 |
| 43 | + context.metadata.merge(data: css) |
71 | 44 | end
|
72 | 45 |
|
73 | 46 | def config_options
|
@@ -106,20 +79,34 @@ def safe_merge(key, left, right)
|
106 | 79 | right
|
107 | 80 | end
|
108 | 81 | end
|
109 |
| - end |
110 | 82 |
|
111 |
| - class ScssTemplate < SassTemplate |
112 |
| - unless Sprockets::VERSION > "3.0.0" |
113 |
| - self.default_mime_type = 'text/css' |
114 |
| - end |
| 83 | + # The methods in the Functions module were copied here from sprockets in order to |
| 84 | + # override the Value class names (e.g. ::SassC::Script::Value::String) |
| 85 | + module Functions |
| 86 | + def asset_path(path, options = {}) |
| 87 | + path = path.value |
115 | 88 |
|
116 |
| - # Sprockets 3 |
117 |
| - def self.syntax |
118 |
| - :scss |
| 89 | + path, _, query, fragment = URI.split(path)[5..8] |
| 90 | + path = sprockets_context.asset_path(path, options) |
| 91 | + query = "?#{query}" if query |
| 92 | + fragment = "##{fragment}" if fragment |
| 93 | + |
| 94 | + ::SassC::Script::Value::String.new("#{path}#{query}#{fragment}", :string) |
| 95 | + end |
| 96 | + |
| 97 | + def asset_url(path, options = {}) |
| 98 | + ::SassC::Script::Value::String.new("url(#{asset_path(path, options).value})") |
| 99 | + end |
| 100 | + |
| 101 | + def asset_data_url(path) |
| 102 | + url = sprockets_context.asset_data_uri(path.value) |
| 103 | + ::SassC::Script::Value::String.new("url(" + url + ")") |
| 104 | + end |
119 | 105 | end
|
| 106 | + end |
120 | 107 |
|
121 |
| - # Sprockets 2 |
122 |
| - def syntax |
| 108 | + class ScssTemplate < SassTemplate |
| 109 | + def self.syntax |
123 | 110 | :scss
|
124 | 111 | end
|
125 | 112 | end
|
|
0 commit comments