Sha256: 0877fb25d1e438d738dd271ee3d173a688a7fdb7d04049d0cb8df62125842dcb

Contents?: true

Size: 1.5 KB

Versions: 32

Compression:

Stored size: 1.5 KB

Contents

require 'pathname'

module AutoprefixerRails
  # Register autoprefixer postprocessor in Sprockets and fix common issues
  class Sprockets
    def initialize(processor)
      @processor = processor
    end

    # Add prefixes for `css`
    def process(context, css)
      root   = Pathname.new(context.root_path)
      input  = context.pathname.relative_path_from(root).to_s
      output = input.chomp(File.extname(input)) + '.css'

      @processor.process(css, from: input, to: output).css
    end

    # Add prefixes only if in `content` will be CSS.
    def process_only_css(context, content)
      begin
        process(context, content)
      rescue ExecJS::ProgramError => e
        if e.message =~ /Can't parse CSS/
          content
        else
          raise e
        end
      end
    end

    # Register postprocessor in Sprockets depend on issues with other gems
    def install(assets)
      if ignore_syntax_error?
        register(assets) { |context, css| process_only_css(context, css) }
      else
        register(assets) { |context, css| process(context, css) }
      end
    end

    private

    # Add `block` as `assets` postprocessor
    def register(assets, &block)
      assets.register_postprocessor('text/css', :autoprefixer, &block)
    end

    # Return true if broken sass-rails is loaded
    def ignore_syntax_error?
      return false unless defined? Sass::Rails

      fixed   = Gem::Version.new('4.0.1')
      current = Gem::Version.new(Sass::Rails::VERSION)

      current < fixed
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
autoprefixer-rails-2.2.0.20140804 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-2.2.0.20140727 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-2.1.1.20140710 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-2.1.0.20140628 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-2.0.2.20140628 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-2.0.1.20140626 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-2.0.0.20140622 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.3.1.20140619 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.3.0.20140619 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.2.0.20140609 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.1.20140605 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.1.20140523 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.1.20140521 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.1.20140512 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.1.20140510 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.1.20140430 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.1.20140429 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.1.20140410 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.1.20140403 lib/autoprefixer-rails/sprockets.rb
autoprefixer-rails-1.1.20140327 lib/autoprefixer-rails/sprockets.rb