Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

sass - Using Compass from Ruby (not shell)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...