Rails - 如何在手动 sass 编译中传递 Sprockets::Context [英] Rails - How to pass Sprockets::Context in manual sass compiling

查看:42
本文介绍了Rails - 如何在手动 sass 编译中传递 Sprockets::Context的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码片段手动编译附加了一些变量覆盖的 sass 清单.

I'm using the following code snippet to manually compile a sass manifest with some variable overrides appended.

template = File.read("#{Rails.root}/app/assets/schemes/#{scheme}/css/styles.css.scss")

scheme_variables.each do |key, value|
  template << "$#{key}:#{value};\n"
end

engine = Sass::Engine.new(template, { 
  :syntax => :scss,
  :cache => false,
  :read_cache => false,
  :style => :compressed,
  :filesystem_importer => Sass::Rails::SassImporter,
  :load_paths => MyApp::Application.assets.paths,
  :sprockets => {
    :context => ?,
    :environment => MyApp::Application.assets
  }
})
output = engine.render

Sass::Engine 构造函数需要选项哈希中的链轮上下文和环境.我在上下文中放入什么?我尝试的第一件事是...

The Sass::Engine constructor wants a sprockets context and environment in the options hash. What do I put in for the context? The first thing I tried was...

:context => MyApp::Application.assets.context_class,

...但是当它遇到我的一个 sass 资产助手时,这给了我以下错误未定义的方法 `font_path' for #".

...but that gives me the following error "undefined method `font_path' for #" when it hits one of my sass asset helpers.

我尝试的第二件事是...

The second thing I tried was...

:context => ActionController::Base.helpers,

...这修复了资产助手问题,但是当它尝试通过我的全局导入(例如@importmixins/*")时,会抛出以下错误未定义的方法`depend_on' for #".

...That fixed the asset helper issue, but throws the following error "undefined method `depend_on' for #" when it tries to work through my glob imports (e.g. @import "mixins/*").

我使用的是 Rails 4.2 和 sass-rails 5.0.3.

I'm using Rails 4.2 and sass-rails 5.0.3.

对此的任何建议将不胜感激.谢谢!

Any advice on this would be much appreciated. Thanks!

推荐答案

我最终以稍微不同的方式解决了这个问题——使用 Sass::Rails::ScssTemplate 的渲染方法.基本上,我将更改后的 css 字符串写入文件并将其传递给 Sass::Rails::ScssTemplate 构造函数.完成后,我编译并删除临时文件.这感觉不太好,但对我来说效果很好.

I ended up solving this in a slightly different way - using Sass::Rails::ScssTemplate's render method. Basically, I write my altered css string out to a file and pass it into the Sass::Rails::ScssTemplate constructor. I then compile and remove the temp file when it's done. This doesn't feel great, but it's working well for me.

scheme_css_dir = "#{Rails.root}/app/assets/schemes/#{scheme}/css"
css = File.read("#{scheme_css_dir}/styles.css.scss")

variables_str = ''
scheme_variables.each do |key, value|
  variables_str << "$#{key}:#{value};\n"
end

css.gsub!('@import "./variables";', variables_str)

file = Tempfile.new(['styles', '.css.scss'], scheme_css_dir)
file.write(css)
file.close

abs_path = file.path
relative_path = abs_path[Rails.root.to_s.size + 1..-1]

template = Sass::Rails::ScssTemplate.new(abs_path)
environment = Evrconnect::Application.assets
context = environment.context_class.new(
  :environment => environment,
  :name => relative_path,
  :filename => abs_path,
  :metadata => {}
)
output = template.render(context)

file.unlink

这篇关于Rails - 如何在手动 sass 编译中传递 Sprockets::Context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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