Rails - How to pass stars :: Context in compilation manually sass

I use the following code snippet to manually compile the sass manifest with some added variable overrides.

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

The constructor of Sass :: Engine wants to create a context and environment for asterisks in the hash parameter settings. What do I put in context? The first thing I tried was ...

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

... but this gives me the following error: undefined method `font_path 'for #" when it gets into one of my sass helpers.

The second thing I tried was ...

:context => ActionController::Base.helpers,

... This fixed the problem with the asset helper, but when I try to import my glob import (for example, @import "mixins / *"), the following error is raised: undefined `depend_on 'method for #

I am using Rails 4.2 and sass-rails 5.0.3.

. !

+3
3

- - Sass:: Rails:: ScssTemplate render. , css Sass:: Rails:: ScssTemplate. . , .

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
+3

Sass:: Rails:: ScssTemplate sass :

template = '...' # Your sass code

logical_path = pathname = ''
environment = Rails.application.assets
context = environment.context_class.new(environment, logical_path, pathname)

template = Sass::Rails::ScssTemplate.new(pathname) { template }
output = template.render(context, {})

, .

Rails 4.2.5.1 sass-rails 5.0.4.

+3

All Articles