-
Notifications
You must be signed in to change notification settings - Fork 0
/
sass_processor.rb
325 lines (272 loc) · 9.16 KB
/
sass_processor.rb
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# frozen_string_literal: true
module Sprockets
module SassEmbedded
class SassProcessor
# @see https://sass-lang.com/documentation/js-api/interfaces/Options#functions
class DartSassFunctionsHash
# @param functions [Module]
# @param options [Hash{Symbol => Hash}]
def initialize(functions, options)
@functions = functions
instance.define_singleton_method(:options, -> { options })
end
# @return [Hash{String => Proc}]
def to_hash
@functions
.public_instance_methods
.each_with_object({}) do |symbol, obj|
parameters = instance
.method(symbol)
.parameters
.filter_map { |parameter| "$#{parameter.last}" if parameter.first == :req }
obj["#{symbol}(#{parameters.join(", ")})"] = ->(args) { instance.public_send(symbol, *args) }
end
end
alias to_h to_hash
private
def instance
@instance ||= Class.new.extend(@functions)
end
end
private_constant :DartSassFunctionsHash
VERSION = "1"
private_constant :VERSION
# @return [String]
def self.cache_key
instance.cache_key
end
def self.call(input)
instance.call(input)
end
# @return [Sprockets::SassEmbedded::SassProcessor]
def self.instance
@instance ||= new
end
# @return [Symbol]
def self.syntax
:indented
end
# 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(cache_version: nil, functions: nil, sass_config: {}, **_options)
@cache_version = cache_version
@sass_config = sass_config
@functions =
Module.new do
include Functions
include functions if functions
class_eval(&block) if block_given?
end
end
# @return [String]
def cache_key
@cache_key ||=
[
self.class.name,
VERSION,
Autoload::SassEmbedded::Embedded::VERSION,
@cache_version
].join(":")
end
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def call(input)
context = input[:environment].context_class.new(input)
options = merge_options(
functions: DartSassFunctionsHash.new(
@functions,
sprockets: {
context: context,
dependencies: context.metadata[:dependencies],
environment: input[:environment]
}
).to_h,
load_paths: context.environment.paths,
source_map: true,
syntax: self.class.syntax,
url: URIUtils.build_asset_uri(input[:filename])
)
result = Autoload::SassEmbedded.compile_string(input[:data], **options)
css = result.css
map = SourceMapUtils.combine_source_maps(
input[:metadata][:map],
SourceMapUtils.format_source_map(JSON.parse(result.source_map), input)
)
sass_dependencies = Set.new
result.loaded_urls.each do |url|
scheme, _host, path, _query = URIUtils.split_file_uri(url)
next unless scheme == "file"
sass_dependencies << path
context.metadata[:dependencies] << URIUtils.build_file_digest_uri(path)
end
context.metadata.merge(data: css, map: map, sass_dependencies: sass_dependencies)
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
private
def merge_options(options)
defaults = @sass_config.dup
if (load_paths = defaults.delete(:load_paths))
options[:load_paths] += load_paths
end
options.merge!(defaults)
end
# Functions injected into Sass context during Sprockets evaluation.
#
# This module may be extended to add global functionality to all Sprockets
# Sass environments. Scoping your functions to just your environment is
# preferred.
#
# @example
#
# module Sprockets::SassProcessor::Functions
# def asset_path(path, options = {})
# end
# end
#
module Functions
# Generate a URL for asset path.
#
# @param path [Sass::Script::String]
# @param options [Hash]
# @return [SassEmbedded::Value::String]
def asset_path(path, **options)
path = path.text
path, _, query, fragment = URI.split(path)[5..8]
path = sprockets_context.asset_path(path, options)
query = "?#{query}" if query
fragment = "##{fragment}" if fragment
Autoload::SassEmbedded::Value::String.new("#{path}#{query}#{fragment}", quoted: true)
end
# Generate an asset +url()+ link.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def asset_url(path, **options)
Autoload::SassEmbedded::Value::String.new("url(#{asset_path(path, options).text})", quoted: false)
end
# Generate a URL for image path.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def image_path(path)
asset_path(path, type: :image)
end
# Generate an image +url()+ link.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def image_url(path)
asset_url(path, type: :image)
end
# Generate a URL for video path.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def video_path(path)
asset_path(path, type: :video)
end
# Generate a video +url()+ link.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def video_url(path)
asset_url(path, type: :video)
end
# Generate a URL for audio path.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def audio_path(path)
asset_path(path, type: :audio)
end
# Generate an audio +url()+ link.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def audio_url(path)
asset_url(path, type: :audio)
end
# Generate a URL for font path.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def font_path(path)
asset_path(path, type: :font)
end
# Generate a font +url()+ link.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def font_url(path)
asset_url(path, type: :font)
end
# Generate a URL for JavaScript path.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def javascript_path(path)
asset_path(path, type: :javascript)
end
# Generate a JavaScript +url()+ link.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def javascript_url(path)
asset_url(path, type: :javascript)
end
# Generate a URL for stylesheet path.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def stylesheet_path(path)
asset_path(path, type: :stylesheet)
end
# Generate a stylesheet +url()+ link.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def stylesheet_url(path)
asset_url(path, type: :stylesheet)
end
# Generate a +data:+ URI for asset path.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def asset_data_uri(path)
url = sprockets_context.asset_data_uri(path.text)
Autoload::SassEmbedded::Value::String.new(url)
end
# Genearte a +data:+ +url()+ link.
#
# @param path [Sass::Script::String]
# @return [SassEmbedded::Value::String]
def asset_data_url(path)
Autoload::SassEmbedded::Value::String.new("url(#{asset_data_uri(path)})", quoted: false)
end
protected
# @deprecated Use #sprockets_environment or #sprockets_dependencies
# instead.
#
# @return [Sprockets::Context]
def sprockets_context
options[:sprockets][:context]
end
# A mutatable set of dependencies.
#
# @return [Set]
def sprockets_dependencies
options[:sprockets][:dependencies]
end
# The Environment.
#
# @return [Sprockets::Environment]
def sprockets_environment
options[:sprockets][:environment]
end
end
end
end
end