Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sass-embedded (dart-sass-embedded) #737

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 0 additions & 30 deletions lib/sprockets/sass_cache_store.rb

This file was deleted.

21 changes: 10 additions & 11 deletions lib/sprockets/sass_compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Sprockets
# Sprockets::SassCompressor.new({ ... })
#
class SassCompressor
VERSION = '1'
VERSION = '2'

# Public: Return singleton instance with default options.
#
Expand All @@ -38,23 +38,22 @@ def self.cache_key

def initialize(options = {})
@options = {
syntax: :scss,
cache: false,
read_cache: false,
style: :compressed
syntax: :css,
style: :compressed,
source_map: true
}.merge(options).freeze
@cache_key = "#{self.class.name}:#{Autoload::Sass::VERSION}:#{VERSION}:#{DigestUtils.digest(options)}".freeze
@cache_key = "#{self.class.name}:#{Autoload::Sass::Embedded::VERSION}:#{VERSION}:#{DigestUtils.digest(options)}".freeze
end

def call(input)
css, map = Autoload::Sass::Engine.new(
result = Autoload::Sass.compile_string(
input[:data],
@options.merge(filename: input[:filename])
).render_with_sourcemap('')
**@options.merge(url: URIUtils.build_asset_uri(input[:filename]))
)

css = css.sub("/*# sourceMappingURL= */\n", '')
css = result.css

map = SourceMapUtils.format_source_map(JSON.parse(map.to_json(css_uri: '')), input)
map = SourceMapUtils.format_source_map(JSON.parse(result.source_map), input)
map = SourceMapUtils.combine_source_maps(input[:metadata][:map], map)

{ data: css, map: map }
Expand Down
3 changes: 0 additions & 3 deletions lib/sprockets/sass_functions.rb

This file was deleted.

3 changes: 0 additions & 3 deletions lib/sprockets/sass_importer.rb

This file was deleted.

102 changes: 57 additions & 45 deletions lib/sprockets/sass_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,30 @@ module Sprockets
# https://github.com/rails/sass-rails
#
class SassProcessor
autoload :CacheStore, 'sprockets/sass_cache_store'
VERSION = '2'

# Internal: Defines default sass syntax to use. Exposed so the ScssProcessor
# may override it.
def self.syntax
:sass
:indented
end

# Public: Convert ::Sass::Script::Functions to dart-sass functions option.
#
# Returns Hash object.
def self.functions(options = {})
functions = {}
instance = Class.new.extend(::Sass::Script::Functions)
instance.define_singleton_method(:options, ->() { options })
::Sass::Script::Functions.public_instance_methods.each do |symbol|
parameters = instance.method(symbol).parameters
.filter { |parameter| parameter.first == :req }
.map { |parameter| "$#{parameter.last}" }
functions["#{symbol}(#{parameters.join(', ')})"] = lambda do |args|
instance.send(symbol, *args)
end
end
functions
end

# Public: Return singleton instance with default options.
Expand Down Expand Up @@ -46,8 +64,7 @@ def self.cache_key
#
def initialize(options = {}, &block)
@cache_version = options[:cache_version]
@cache_key = "#{self.class.name}:#{VERSION}:#{Autoload::Sass::VERSION}:#{@cache_version}".freeze
@importer_class = options[:importer] || Sass::Importers::Filesystem
@cache_key = "#{self.class.name}:#{VERSION}:#{Autoload::Sass::Embedded::VERSION}:#{@cache_version}".freeze
@sass_config = options[:sass_config] || {}
@functions = Module.new do
include Functions
Expand All @@ -59,53 +76,44 @@ def initialize(options = {}, &block)
def call(input)
context = input[:environment].context_class.new(input)

engine_options = merge_options({
filename: input[:filename],
syntax: self.class.syntax,
cache_store: build_cache_store(input, @cache_version),
load_paths: context.environment.paths.map { |p| @importer_class.new(p.to_s) },
importer: @importer_class.new(Pathname.new(context.filename).to_s),
sprockets: {
context: context,
environment: input[:environment],
dependencies: context.metadata[:dependencies]
}
})

engine = Autoload::Sass::Engine.new(input[:data], engine_options)

css, map = Utils.module_include(Autoload::Sass::Script::Functions, @functions) do
engine.render_with_sourcemap('')
result = Utils.module_include(::Sass::Script::Functions, @functions) do
options = merge_options({
functions: self.class.functions({
sprockets: {
context: context,
environment: input[:environment],
dependencies: context.metadata[:dependencies]
}
}),
syntax: self.class.syntax,
source_map: true,
load_paths: context.environment.paths,
url: URIUtils.build_asset_uri(input[:filename])
})

Autoload::Sass.compile_string(input[:data], **options)
end

css = css.sub("\n/*# sourceMappingURL= */\n", '')
css = result.css

map = SourceMapUtils.format_source_map(JSON.parse(map.to_json(css_uri: '')), input)
map = SourceMapUtils.format_source_map(JSON.parse(result.source_map), input)
map = SourceMapUtils.combine_source_maps(input[:metadata][:map], map)

# 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])
sass_dependencies = Set.new
result.loaded_urls.each do |url|
scheme, _host, path, _query = URIUtils.split_file_uri url
if scheme == 'file'
sass_dependencies << path
context.metadata[:dependencies] << URIUtils.build_file_digest_uri(path)
end
end

context.metadata.merge(data: css, sass_dependencies: sass_dependencies, map: map)
end

private

# 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

def merge_options(options)
defaults = @sass_config.dup

Expand Down Expand Up @@ -139,14 +147,14 @@ module Functions
#
# Returns a Sass::Script::String.
def asset_path(path, options = {})
path = path.value
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::Sass::Script::String.new("#{path}#{query}#{fragment}", :string)
Autoload::Sass::Value::String.new("#{path}#{query}#{fragment}", quoted: true)
end

# Public: Generate a asset url() link.
Expand All @@ -155,7 +163,7 @@ def asset_path(path, options = {})
#
# Returns a Sass::Script::String.
def asset_url(path, options = {})
Autoload::Sass::Script::String.new("url(#{asset_path(path, options).value})")
Autoload::Sass::Value::String.new("url(#{asset_path(path, options).text})", quoted: false)
end

# Public: Generate url for image path.
Expand Down Expand Up @@ -272,8 +280,8 @@ def stylesheet_url(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 + ")")
url = sprockets_context.asset_data_uri(path.text)
Autoload::Sass::Value::String.new("url(" + url + ")", quoted: false)
end

protected
Expand Down Expand Up @@ -308,6 +316,10 @@ def self.syntax
end
end

# Deprecated: Use Sprockets::SassProcessor::Functions instead.
SassFunctions = SassProcessor::Functions
module ::Sass
module Script
module Functions
end
end
end
end
2 changes: 1 addition & 1 deletion sprockets.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "nokogiri", "~> 1.3"
s.add_development_dependency "rack-test", "~> 0.6"
s.add_development_dependency "rake", "~> 12.0"
s.add_development_dependency "sass", "~> 3.4"
s.add_development_dependency "sass-embedded", "~> 1.0"
s.add_development_dependency "sassc", "~> 2.0"
s.add_development_dependency "uglifier", ">= 2.3"
s.add_development_dependency "yui-compressor", "~> 0.12"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/octicons/octicons.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@font-face {
font-family: 'octicons';
font-family: octicons;
src: font-url('octicons.eot?#iefix') format('embedded-opentype'),
font-url('octicons.woff2') format('woff2'),
font-url('octicons.woff') format('woff'),
Expand Down
Loading