从Ruby使用Compass(不是shell) [英] Using Compass from Ruby (not shell)

查看:62
本文介绍了从Ruby使用Compass(不是shell)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ruby中构建脚本,我想在其中使用Compass编译单个SCSS文件。我正在尝试使其尽可能简单,并希望避免使用config.rb文件。我只想通过简单的Ruby设置一些设置,并告诉Compass将单个SCSS文件编译为CSS文件。

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.

推荐答案

您是对的,实际上没有关于如何使用Ruby中的Compass的全面文档。不幸的是,但是不要让文档之类的细节阻止我们!

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!

当我想要做同样的事情,我只是在指北针(<< href = https://github.com/Compass/compass/tree/stable/cli/lib/compass rel = nofollow noreferrer> source ,并且可以将这个小的Ruby脚本放在一起。乍一看,它似乎可以解决问题:

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')

但是显然Compass有很多直接调用编译器构造函数时不会自动包含的默认配置选项(其中SASS load_path 是其中之一)。尝试导入Compass函数和mixins时,这可能导致错误,例如:

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:


错误:找不到要导入的文件或不可读:指南针/ css3

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



指南针< 1.0.0(又名旧方法)



以下是在没有覆盖这些默认值的情况下调用编译器的方法:

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')

不过,从指南针1.0.0版开始, Compass.compiler 已被弃用,而推荐使用 Compass.sass_compiler ,从而导致...

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

感谢 @philipp 查找如何使用新API ,我们可以再次更新此代码段以与 Compass.sass_compiler

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!

这篇关于从Ruby使用Compass(不是shell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆