Skip to content

Instantly share code, notes, and snippets.

@jarrodldavis
Last active August 17, 2019 08:26
Show Gist options
  • Save jarrodldavis/72d450c636ffed67a0a8a3c31d261387 to your computer and use it in GitHub Desktop.
Save jarrodldavis/72d450c636ffed67a0a8a3c31d261387 to your computer and use it in GitHub Desktop.
Nanoc `sassc` filter
#!/usr/bin/env ruby
ignore '/**/_*.scss'
ignore '/**/_*.scss', rep: :source_map
compile '/**/*.scss' do
filter :sassc
write ext: '.css'
end
compile '/**/*.scss', rep: :source_map do
filter :sassc_source_map
write ext: '.css.map'
end
require 'sassc'
module Nanoc::Filters
module SassCCommon
def render(content, params = {})
dir = File.dirname(@item.raw_filename)
options = params.merge(
load_paths: [dir],
filename: @item.raw_filename,
cache: false
)
source_map_rep = @item.reps[:source_map]
unless source_map_rep.nil?
options[:source_map_file] = File.join(dir, File.basename(source_map_rep.path))
options[:source_map_contents] = true
end
engine = ::SassC::Engine.new(content, options)
css = engine.render
dependencies = engine.dependencies
if dependencies.any?
depend_on(
@items.to_h {|item| [item.raw_filename, item]}
.fetch_values(*dependencies.map(&:filename))
)
end
[css, engine.source_map]
end
end
class SassCFilter < Nanoc::Filter
identifier :sassc
include SassCCommon
def run(content, params = {})
css, _ = render(content, params)
css
end
end
class SassCSourceMapFilter < Nanoc::Filter
identifier :sassc_source_map
include SassCCommon
def run(content, params = {})
_, source_map = render(content, params)
source_map
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment