10

I am building a script in Ruby where I would like to compile a single SCSS file using Compass. I am trying to make this as simple as possible, and would like to avoid using a config.rb file. I just want to set a couple settings via straight Ruby and tell Compass to compile a single SCSS file into a CSS file.

I know that this has to be possible but I have not been able to find any decent documentation on how to do it. Any help would be appreciated.

hopper
  • 13,060
  • 7
  • 49
  • 53
Joe Workman
  • 341
  • 1
  • 16

2 Answers2

10

You're right, there's not really any comprehensive documentation on how to use Compass from Ruby. This is unfortunate, but let's not let little details like documentation stop us!

A First Attempt

When I was looking to do the same thing, I just poked around the Compass source and was able put together this little Ruby script. At first glance it seems to do the trick:

require 'compass'
require 'sass/plugin'

compiler = Compass::Compiler.new(
    # Compass working directory
    '.',
    # Input directory
    'styles/scss',
    # Output directory
    'styles/css',
    # Compass options
    { :style => :compressed }
)

compiler.compile('test.scss', 'test.css')

But apparently Compass has a bunch of default configuration options that aren't automatically included when invoking the compiler constructor directly (of which the SASS load_path is one). This can lead to errors when trying to import Compass functions and mixins, such as:

error: File to import not found or unreadable: compass/css3

Compass <1.0.0 (a.k.a. "the old way")

Here's how to call the compiler without overriding those defaults:

require 'compass'

Compass.add_configuration(
    {
        :project_path => '.',
        :sass_path => 'styles/scss',
        :css_path => 'styles/css'
    },
    'custom' # A name for the configuration, can be anything you want
)
Compass.compiler.compile('test.scss', 'test.css')

However, as of Compass version 1.0.0, Compass.compiler has been deprecated in favor of Compass.sass_compiler, leading to...

Compass >=1.0.0 (a.k.a. "the new way")

With thanks to @philipp for finding how to use the new API, we can update this snippet again to work with Compass.sass_compiler:

require 'compass'
require 'compass/sass_compiler'

Compass.add_configuration(
    {
        :project_path => '.',
        :sass_path => 'styles/scss',
        :css_path => 'styles/css'
    },
    'custom' # A name for the configuration, can be anything you want
)

compiler = Compass.sass_compiler({
  :only_sass_files => [
    'styles/scss/test.scss'
  ]
})

compiler.compile!
Community
  • 1
  • 1
hopper
  • 13,060
  • 7
  • 49
  • 53
  • Awesome! This is perfect! I am running into a little glitch when using it with `bundle exec`. I keep getting errors that compass cannot load any mixins. I assume that its an environment variable that needs to be set. Would you happen to know what needs to get set? – Joe Workman Oct 31 '12 at 08:11
  • I should have added the error that I was getting... `error: File to import not found or unreadable: compass/css3.` – Joe Workman Oct 31 '12 at 08:32
  • I had started another question. I thought it was a different issue. However, I now think that its directly related. http://stackoverflow.com/questions/13160660/compass-mixins-not-found-when-using-directly-from-ruby. – Joe Workman Nov 01 '12 at 16:38
  • Yep, you're right. The options hash in the explicit constructor doesn't get merged with the default Compass options, wiping out the sass load path configuration. I've updated my answer. – hopper Nov 02 '12 at 14:55
  • Is there a way to pass in variables from ruby when compiling like this? – Jon Doe Sep 06 '15 at 01:56
  • @Christian You can use [custom functions](http://sass-lang.com/documentation/Sass/Script/Functions.html#adding_custom_functions) to access Ruby values from your stylesheet via function call. For example, [this answer](http://stackoverflow.com/questions/13022461/add-timestamps-to-compiled-sass-scss/13031025#13031025) of mine demonstrates passing a timestamp this way, just replace the implementation to return whatever value you need. – hopper Sep 10 '15 at 19:13
0

Just call the compile method from the command line. You can specify every option there. To see all of the options, run compass help compile.

Below's an example. It will output the compiled css file in the same directory as the test.scss file.

file_name = "absolute/path/to/test.scss"
system "compass compile #{file_name} --sass-dir . --css-dir ."

You could specify and interpolate as many options as you wish. Also check this for running commands in ruby:

Running command line commands within Ruby script

Community
  • 1
  • 1
AJcodez
  • 31,780
  • 20
  • 84
  • 118