是否可以使用Compass in rails为开发和生产设置不同的SASS输出样式?

Vol*_*ldy 7 ruby-on-rails sass compass-sass

假设我想为开发设置嵌套样式并为生产压缩.Compass配置文件中只有一个选项:

output_style = :compact # or :nested, :expanded, :compressed
Run Code Online (Sandbox Code Playgroud)

Vol*_*ldy 7

它看起来很简单:

output_style = RAILS_ENV == "production" ? :compressed : :nested
Run Code Online (Sandbox Code Playgroud)

为了检查它,我在不同的环境中运行这个rake任务(我必须在运行此任务之前更改sass源):

namespace :sass do
  desc 'Updates stylesheets if necessary from their Sass templates.'
  task :update => :environment do
    Sass::Plugin.update_stylesheets
  end
end
Run Code Online (Sandbox Code Playgroud)

您可以将此任务放在lib/tasks/sass.rake中.

另外,我在Capistrano deploy.rb中运行此任务,以在部署期间自动更新生产中的样式表:

after 'deploy:restart', 'sass:update'

namespace :sass do
  desc 'Updates the stylesheets generated by Sass'
  task :update, :roles => :app do
    invoke_command "cd #{current_release}; rake sass:update RAILS_ENV=production"
  end
end
Run Code Online (Sandbox Code Playgroud)


Dae*_*min 6

除了Voldy的回答之外,我通过创建一个名为sass_config的初始化程序并将其放入其中来解决了这个问题:

Sass::Plugin.options[:style] = case RAILS_ENV
  when 'production' then :compressed
  when 'staging' then :compact
  when 'development' then :expanded
  else
    :nested
end
Run Code Online (Sandbox Code Playgroud)