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, SCSS, or CSS file to import. This follows Sass‘s import rules: if the filename given ends in `".sass"`, `".scss"`, or `".css"`, it will try to find that type of file; otherwise, it will try to find the corresponding Sass/SCSS file and fall back on CSS if it‘s not available.
Any Sass/SCSS filename returned will correspond to an actual file of the corresponding type 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/SCSS files.
@return [String] The filename of the imported file.
This is an absolute path if the file is a `".sass"` or `".scss"` file.
@raise [Sass::SyntaxError] if `filename` ends in `".sass"` or `".scss"`
and no corresponding Sass/SCSS file could be found.
# File lib/sass/files.rb, line 67 67: def find_file_to_import(filename, load_paths) 68: was_sass = was_scss = false 69: original_filename = filename 70: 71: if [".sass", ".scss"].include?(filename[-5..-1]) 72: was_sass = filename[-5..-1] == ".sass" 73: was_scss = filename[-5..-1] == ".scss" 74: filename = filename[0...-5] 75: elsif filename[-4..-1] == ".css" 76: return filename 77: end 78: 79: new_filename = nil 80: load_paths = load_paths.uniq 81: load_paths.each do |load_path| 82: new_filename ||= find_full_path("#{filename}.sass", load_path) unless was_scss 83: new_filename ||= find_full_path("#{filename}.scss", load_path) unless was_sass 84: end 85: 86: return new_filename if new_filename 87: unless was_sass || was_scss 88: Haml::Util.haml_warn "WARNING: Neither \#{filename}.sass nor .scss 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" 89: return filename + '.css' 90: end 91: 92: message = "File to import not found or unreadable: #{original_filename}.\n" 93: if load_paths.size == 1 94: message << "Load path: #{load_paths.first}" 95: else 96: message << "Load paths:\n " << load_paths.join("\n ") 97: end 98: 99: raise SyntaxError.new(message) 100: 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 or SCSS 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.
The caller has responsibility for setting backtrace information, if necessary
# File lib/sass/files.rb, line 19 19: def tree_for(filename, options) 20: options = Sass::Engine::DEFAULT_OPTIONS.merge(options) 21: text = File.read(filename) 22: 23: if options[:cache] || options[:read_cache] 24: compiled_filename = sassc_filename(filename, options) 25: sha = Digest::SHA1.hexdigest(text) 26: 27: if root = try_to_read_sassc(filename, compiled_filename, sha) 28: root.options = options.merge(:filename => filename) 29: return root 30: end 31: end 32: 33: options = options.merge(:filename => filename) 34: if filename =~ /\.scss$/ 35: options = options.merge(:syntax => :scss) 36: elsif filename =~ /\.sass$/ 37: options = options.merge(:syntax => :sass) 38: end 39: 40: engine = Sass::Engine.new(text, options) 41: 42: root = engine.to_tree 43: try_to_write_sassc(root, compiled_filename, sha, options) if options[:cache] 44: root 45: end