Skip to content

Instantly share code, notes, and snippets.

@dmolesUC
Last active February 2, 2022 16:56
Show Gist options
  • Save dmolesUC/2bc618c0837c8a391d00f164d61937d8 to your computer and use it in GitHub Desktop.
Save dmolesUC/2bc618c0837c8a391d00f164d61937d8 to your computer and use it in GitHub Desktop.
Quick and dirty Sprockets wrapper fror https://github.com/ntkme/sass-embedded-host-ruby
# Add this code to config/initializers/assets.rb
require 'berkeley_library/util/sprockets/embedded_sass_processor'
Sprockets.register_transformer('text/scss', 'text/css', BerkeleyLibrary::Util::Sprockets::EmbeddedSassProcessor)
Sprockets.register_transformer('text/sass', 'text/css', BerkeleyLibrary::Util::Sprockets::EmbeddedSassProcessor)
# TODO: move EmbeddedSassProcessor to a gem, put this in a railtie (cf. sassc-rails lib/sassc/rails/railtie.rb)
# put this in your Rails app's lib/berkeley_library/util/sprockets
# NOTE: *not* /app/lib/…
require 'sprockets/version'
require 'sprockets/sass_processor'
require 'sprockets/utils'
require 'sass'
module BerkeleyLibrary
module Util
module Sprockets
class ModuleInfo
VERSION = '0.0.1'.freeze
end
class EmbeddedSassProcessor < ::Sprockets::SassProcessor
include BerkeleyLibrary::Logging
# rubocop:disable Lint/MissingSuper
# NOTE: we deliberately don't call #super because we don't want Sprockets::SassProcessor
# trying to load a (nonexistent) Sass::Importers::Filesystem from ruby-sass
def initialize(options = {}, &block)
@cache_version = options[:cache_version]
@cache_key = "#{self.class.name}:#{ModuleInfo::VERSION}:#{Sass::VERSION}:#{@cache_version}".freeze
end
# rubocop:enable Lint/MissingSuper
def call(input)
sass_params = sass_params_from(input)
result = render(sass_params)
context = input[:environment].context_class.new(input)
context.metadata.merge(data: result.css)
end
private
def sass_params_from(input)
{
# NOTE: sass-embedded must be at least 0.7.16 or you'll get Encoding::UndefinedConversionError when
# :include_paths contains gem directories, see https://github.com/ntkme/sass-embedded-host-ruby/issues/2
include_paths: input[:environment].paths.dup, # dup b/c paths is frozen
file: input[:filename]
}
end
def render(sass_params)
Sass.render(**sass_params)
rescue StandardError
logger.warn('Sass.render failed', nil, { sass_params: sass_params })
raise
end
end
end
end
end

The MIT License (MIT)

Copyright © 2021 The Regents of the University of California

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@halo
Copy link

halo commented Feb 2, 2022

This is so promising, thank you. You were most likely the first to try! 😄

I was going to ask if you're aware of a railtie/gem for this, but then I already found it: https://github.com/rails/dartsass-rails

@dmolesUC
Copy link
Author

dmolesUC commented Feb 2, 2022

@halo Thanks for drawing my attention to that gem — I'm glad DHH & co are finally working on it. Their approach of bypassing Sprockets and just hooking a Rake task into assets:precompile seems much more foolproof, although I'm not excited about embedding the sass executable instead of using sass-embedded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment