Module: Sass::Files

Extended by:
Files
Included in:
Files
Defined in:
lib/sass/files.rb

Overview

This module contains various bits of functionality related to finding and caching Sass files.

Instance Method Summary collapse

Instance Method Details

#find_file_to_import(filename, load_paths) ⇒ String

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.

Parameters:

  • filename (String)

    The filename to search for

  • load_paths (Array<String>)

    The set of filesystem paths to search for Sass/SCSS files.

Returns:

  • (String)

    The filename of the imported file. This is an absolute path if the file is a ".sass" or ".scss" file.

Raises:

  • (Sass::SyntaxError)

    if filename ends in ".sass" or ".scss" and no corresponding Sass/SCSS file could be found.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sass/files.rb', line 67

def find_file_to_import(filename, load_paths)
  was_sass = was_scss = false
  original_filename = filename

  if [".sass", ".scss"].include?(filename[-5..-1])
    was_sass = filename[-5..-1] == ".sass"
    was_scss = filename[-5..-1] == ".scss"
    filename = filename[0...-5]
  elsif filename[-4..-1] == ".css"
    return filename
  end

  new_filename = nil
  load_paths.each do |load_path|
    new_filename ||= find_full_path("#{filename}.sass", load_path) unless was_scss
    new_filename ||= find_full_path("#{filename}.scss", load_path) unless was_sass
  end

  return new_filename if new_filename
  unless was_sass || was_scss
    Haml::Util.haml_warn <<END
WARNING: Neither #{filename}.sass nor .scss found. Using #{filename}.css instead.
This behavior is deprecated and will be removed in a future version.
If you really need #{filename}.css, import it explicitly.
END
    return filename + '.css'
  end

  message = "File to import not found or unreadable: #{original_filename}.\n"
  if load_paths.size == 1
    message << "Load path: #{load_paths.first}"
  else
    message << "Load paths:\n  " << load_paths.join("\n  ")
  end

  raise SyntaxError.new(message)
end

#tree_for(filename, options)

Returns the Tree for the given file, reading it from the Sass cache if possible.

Parameters:

  • filename (String)

    The path to the Sass or SCSS file

  • options ({Symbol => Object})

    The options hash. Only the :cache_location option is used

Raises:

  • (Sass::SyntaxError)

    if there's an error in the document. The caller has responsibility for setting backtrace information, if necessary



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sass/files.rb', line 19

def tree_for(filename, options)
  options = Sass::Engine::DEFAULT_OPTIONS.merge(options)
  text = File.read(filename)

  if options[:cache] || options[:read_cache]
    compiled_filename = sassc_filename(filename, options)
    sha = Digest::SHA1.hexdigest(text)

    if root = try_to_read_sassc(filename, compiled_filename, sha)
      root.options = options.merge(:filename => filename)
      return root
    end
  end

  options = options.merge(:filename => filename)
  if filename =~ /\.scss$/
    options = options.merge(:syntax => :scss)
  elsif filename =~ /\.sass$/
    options = options.merge(:syntax => :sass)
  end

  engine = Sass::Engine.new(text, options)

  root = engine.to_tree
  try_to_write_sassc(root, compiled_filename, sha, options) if options[:cache]
  root
end