Created
January 13, 2018 12:18
-
-
Save agross/263f4dcd38f56a02efca0f68e4c42b96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'nanoc' | |
module Nanoc | |
module Filters | |
class SassWithCompass < Nanoc::Filter | |
identifier :sass_with_compass | |
type :text | |
requires 'configatron', 'compass', 'sass' | |
def run(content, params = {}) | |
define_dependencies | |
render_with_sourcemap(sourcemap_uri) unless configatron.web.compress | |
css, sourcemap = Sass.new(@assigns).setup_and_run(content, | |
configure_compass(params)) | |
save(sourcemap) | |
css | |
end | |
private | |
def define_dependencies | |
depend_on(@assigns[:items].find_all('/**/*.{jpg,png}')) | |
depend_on(@assigns[:items].find_all('/**/*.{eot,ttf}')) | |
end | |
def configure_compass(params) | |
p = params.dup | |
Compass.add_project_configuration(p.delete(:config_file)) | |
p.merge(Compass.sass_engine_options) | |
end | |
def render_with_sourcemap(uri) | |
::Sass::Engine.send(:define_method, | |
:render, | |
proc { render_with_sourcemap(uri) }) | |
end | |
def save(sourcemap) | |
return unless sourcemap | |
FileUtils.mkdir_p(File.dirname(sourcemap_path), verbose: false) | |
IO.write(sourcemap_path, sourcemap.to_json(css_uri: @item.path)) | |
end | |
def sourcemap_file | |
@item.path + '.map' | |
end | |
def sourcemap_path | |
File.join(@config[:output_dir], sourcemap_file) | |
end | |
def sourcemap_uri | |
File.basename(sourcemap_file) | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compile '/assets/css/app.sass' do | |
filter :sass_with_compass, config_file: 'compass-config.rb' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment