Skip to content

Instantly share code, notes, and snippets.

@mundry
Last active October 19, 2016 17:55
Show Gist options
  • Save mundry/7565224d57a8ac668678 to your computer and use it in GitHub Desktop.
Save mundry/7565224d57a8ac668678 to your computer and use it in GitHub Desktop.
Script to generate a make-style list of dependencies for a SASS file.

sass-deps.rb

Generates a make-style list of dependencies for a SASS file and prints it to standard output. Nested import statements are supported.

Usage

Usage: sass-deps.rb [options] SASS-file
    -h, --help                       Display this screen and exit.
    -e, --empty-target               Optional flag to output the rule even if the target has no dependencies.
    -t, --target NAME                Optional. The name of the target. If omitted the name of the SASS file is used with the css extension.

Example

$ cat style.scss

@import "variables";
@import "reset";
@import "button";
$ ./sass-deps.rb style.scss

style.css: _variables.scss _reset.scss _button.scss

_variables.scss:

_reset.scss:

_button.scss:
$ ./sass-deps.rb style.scss -t build/styles/style.css

build/styles/style.css: _variables.scss _reset.scss _button.scss

_variables.scss:

_reset.scss:

_button.scss:

Limitations

  • Does not support multiple targets for the same set of dependencies.
source "https://rubygems.org"
gem "sass"
#!/usr/bin/env ruby
require 'optparse'
require 'sass'
options = {empty_target: false, target: nil}
optparse = OptionParser.new do|opts|
opts.banner = 'Usage: sass-deps.rb [options] SASS-file'
opts.on('-h', '--help', 'Display this screen and exit.') do
puts opts
exit
end
opts.on('-e', '--empty-target', 'Optional flag to output the rule even if the target has no dependencies.') do
options[:empty_target] = true
end
opts.on('-t', '--target NAME', 'Optional. The name of the target. If omitted the name of the SASS file is used with the css extension.') do |f|
options[:target] = f
end
end
optparse.parse!
if ARGV.empty?
$stderr.puts 'sass-deps.rb: SASS-file missing.'
exit 1
end
options[:target] = "#{ARGV[0][0..-5]}css" if options[:target].nil?
deps = Sass::Engine.for_file(ARGV[0], {cache: false}).dependencies.collect { |d| d.options[:filename] }
puts "#{options[:target]}: #{deps.join(" ")}" if options[:empty_target] or !deps.empty?
puts "\n#{deps.join(":\n\n")}:" unless deps.empty?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment