rojcyk rojcyk - 7 months ago 32
Ruby Question

Sass::Engine.new causes Sass::SyntaxError on @import

I'm writing my own static site generator in ruby and I'm in the process of adding Sass compiler to my code.

def compile_sass
# system 'sass _sass/styles.scss styles.css'
options = {
syntax: :scss,
style: :compressed
}
render = Sass::Engine.new(File.read('_sass/styles.scss'), options).render
File.write('style.css', render)
end


But problem occurs when the styles.scss file has @import in it. Causing

(sass):1: File to import not found or unreadable: variables. (Sass::SyntaxError)


Both SCSS files are located in _sass folder, main script in root, and compile_sass is located in _generator. But when I uncomment the system call and comment the rest, everything works as expected.

styles.scss

@import 'variables';

html {
background-color: red;
}


_variables.scss

body {
background-color: blue;
}


I tried almost everything, checked how to import stuff, looked at the documentation, but I can't find anything that would helped me find and define the problem.

Answer

Turns out I had to load all _sass/*.scss files into Sass::Engine like this:

Sass::Engine::DEFAULT_OPTIONS[:load_paths].tap do |load_paths|
  load_paths << '_sass'
end