Sha256: 70b6f0659ec5ed69b8f6057920f69d4356602c87be6c5465fa9322f10ed638f8

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

class Condenser::SassTransformer
  class Importer < SassC::Importer
    
    def imports(name, base)
      name = expand_path(name, base)
      env = options[:condenser][:environment]
      accept = extensions.keys.map { |x| options[:condenser][:environment].extensions[x] }

      options[:asset][:process_dependencies] << [name, accept.map{ |i| [i] }]

      imports = []
      env.resolve(name, accept: accept).sort_by(&:filename).each do |asset|
        next if asset.filename == options[:filename]
        imports << Import.new(asset.filename, source: asset.source, source_map_path: nil)
      end

      if imports.empty? && env.npm_path
        package = File.join(env.npm_path, name, 'package.json')
        if File.exists?(package)
          package = JSON.parse(File.read(package))
          if package['style']
            imports << Import.new(name, source: File.read(File.join(env.npm_path, name, package['style'])), source_map_path: nil)
          end
        end
      end

      raise Condenser::FileNotFound, "couldn't find file '#{name}'" if imports.empty?

      imports
    end
    
    # Allow .css files to be @imported
    def extensions
      { '.sass' => :sass, '.scss' => :scss, '.css' => :scss }
    end
    
    private

      def expand_path(path, base=nil)
        if path.start_with?('.')
          File.expand_path(path, File.dirname(base)).delete_prefix(File.expand_path('.') + '/')
        else
          File.expand_path(path).delete_prefix(File.expand_path('.') + '/')
        end
      end
        
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
condenser-0.3 lib/condenser/transformers/sass_transformer/importer.rb
condenser-0.2 lib/condenser/transformers/sass_transformer/importer.rb
condenser-0.1 lib/condenser/transformers/sass_transformer/importer.rb
condenser-0.0.12 lib/condenser/transformers/sass_transformer/importer.rb
condenser-0.0.11 lib/condenser/transformers/sass_transformer/importer.rb