Module | Sass::Files |
In: |
lib/sass/files.rb
|
This module contains various bits of functionality related to finding and caching Sass files.
Find the full filename of a Sass or CSS file to import. This follows Sass‘s import rules: if the filename given ends in `".sass"` or `".css"`, it will try to find that type of file; otherwise, it will try to find the corresponding Sass file and fall back on CSS if it‘s not available.
Any Sass filename returned will correspond to an actual Sass file on the filesystem. CSS filenames, however, may not; they‘re expected to be put through directly to the stylesheet as CSS `@import` statements.
@param filename [String] The filename to search for @param load_paths [Array<String>] The set of filesystem paths
to search for Sass files.
@return [String] The filename of the imported file.
This is an absolute path if the file is a `".sass"` file.
@raise [Sass::SyntaxError] if `filename` ends in ``".sass"``
and no corresponding Sass file could be found.
# File lib/sass/files.rb, line 66 66: def find_file_to_import(filename, load_paths) 67: was_sass = false 68: original_filename = filename 69: 70: if filename[-5..-1] == ".sass" 71: filename = filename[0...-5] 72: was_sass = true 73: elsif filename[-4..-1] == ".css" 74: return filename 75: end 76: 77: new_filename = find_full_path("#{filename}.sass", load_paths) 78: 79: return new_filename if new_filename 80: unless was_sass 81: warn "WARNING: \#{filename}.sass not found. Using \#{filename}.css instead.\nThis behavior is deprecated and will be removed in a future version.\nIf you really need \#{filename}.css, import it explicitly.\n" 82: return filename + '.css' 83: end 84: raise SyntaxError.new("File to import not found or unreadable: #{original_filename}.", @line) 85: end
Returns the {Sass::Tree} for the given file, reading it from the Sass cache if possible.
@param filename [String] The path to the Sass file @param options [{Symbol => Object}] The options hash.
Only the {file:SASS_REFERENCE.md#cache-option `:cache_location`} option is used
@raise [Sass::SyntaxError] if there‘s an error in the document
# File lib/sass/files.rb, line 18 18: def tree_for(filename, options) 19: options = Sass::Engine::DEFAULT_OPTIONS.merge(options) 20: text = File.read(filename) 21: 22: if options[:cache] 23: compiled_filename = sassc_filename(filename, options) 24: sha = Digest::SHA1.hexdigest(text) 25: 26: if root = try_to_read_sassc(filename, compiled_filename, sha) 27: root.options = options.merge(:filename => filename) 28: return root 29: end 30: end 31: 32: engine = Sass::Engine.new(text, options.merge(:filename => filename)) 33: 34: begin 35: root = engine.to_tree 36: rescue Sass::SyntaxError => err 37: err.add_backtrace_entry(filename) 38: raise err 39: end 40: 41: try_to_write_sassc(root, compiled_filename, sha, options) if options[:cache] 42: 43: root 44: end