Rails 4:使用链轮动态编译资产

时间:2014-04-22 22:13:22

标签: ruby-on-rails sprockets

我想动态地预编译一些scss文件(在运行时)。在Rails 3中,我曾经使用过Sprockets :: StaticCompiler。

env = Rails.application.assets
target = File.join(Rails.public_path, config.assets.prefix)
compiler = Sprockets::StaticCompiler.new(env,
                                           target,
                                           config.assets.precompile,
                                           :manifest_path => config.assets.manifest,
                                           :digest => config.assets.digest,
                                           :manifest => digest.nil?)
compiler.compile

我应该如何在Rails 4中执行此操作?互联网上没有任何文档或任何内容。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题并通过使用Sass引擎编译我的资产然后将其写入public / assets文件夹来解决它。

asset = env.find_asset(self.sass_file_path)

compressed_body = ::Sass::Engine.new(asset.body, {
  :syntax => :scss,
  :cache => false,
  :read_cache => false,
  :style => :compressed
}).render

File.open(File.join(Rails.root, "public", "assets", self.stylesheet_file(asset.digest)), 'w') { |f| f.write(compressed_body) }

看一下这篇文章的例子:http://matteodepalo.github.io/blog/2013/01/31/how-to-create-custom-stylesheets-dynamically-with-rails-and-sass/