class Stylus::ImportProcessor

Internal: A Tilt template that tracks down '@import' declarations and marks them as a dependency on the current asset.

Example

@import 'dashboard'
# => A '//= depend_on dashboard' directive can be ommited from the stylesheet.

Constants

IMPORT_SCANNER

Public Instance Methods

evaluate(context, locals, &block) click to toggle source

Public: Scans the current stylesheet to track down Stylus '@import' calls as dependencies.

Returns the stylesheet content, unmodified.

# File lib/epuber-stylus/import_processor.rb, line 24
def evaluate(context, locals, &block)
  return data unless stylus_file?(context)

  depend_on(context, data)
  data
end
prepare() click to toggle source

Internal: Tilt default interface requirement.

Returns nothing.

# File lib/epuber-stylus/import_processor.rb, line 17
def prepare
end

Private Instance Methods

depend_on(context, data) click to toggle source

Internal: Scan the stylesheet body, looking for '@import' calls to declare them as a dependency on the context using 'depend_on' method. This method will recursively scans all dependency to ensure that the current stylesheet tracks down nested dependencies.

# File lib/epuber-stylus/import_processor.rb, line 43
def depend_on(context, data)
  dependencies = data.scan(IMPORT_SCANNER).flatten.compact.uniq

  dependencies.each do |path|
    asset = resolve(context, path)

    if asset
      context.depend_on(asset)
      depend_on(context, File.read(asset))
    end
  end
end
resolve(context, path) click to toggle source

Internal: Resolves the given 'path' with the Sprockets context, but avoids 'Sprockets::FileNotFound' exceptions since we might be importing files outside the Sprockets load path - like “nib”.

Returns the resolved 'Asset' or nil if it can't be found.

# File lib/epuber-stylus/import_processor.rb, line 61
def resolve(context, path)
  context.resolve(path, content_type: 'text/css')
rescue ::Sprockets::FileNotFound
  begin
    context.resolve(path + '/index', content_type: 'text/css')
  rescue
    nil
  end
end
stylus_file?(context) click to toggle source

Internal: Returns true if the file being processed counts as a Stylus file (That is, it has a .styl extension).

# File lib/epuber-stylus/import_processor.rb, line 35
def stylus_file?(context)
  File.fnmatch('*.styl', file) || File.fnmatch('*.styl.*', file)
end