Module: AutoprefixerRails

Defined in:
lib/autoprefixer-rails.rb,
lib/autoprefixer-rails/version.rb

Overview

Ruby integration with Autoprefixer JS library, which parse CSS and adds only actual prefixed.

Constant Summary collapse

VERSION =
'0.5.20130615'.freeze

Class Method Summary collapse

Class Method Details

.compile(css, browsers = nil) ⇒ Object

Parse ‘css` and add vendor prefixes for `browsers`.



26
27
28
# File 'lib/autoprefixer-rails.rb', line 26

def self.compile(css, browsers = nil)
  compiler.call('compile', css, browsers)
end

.compilerObject

Get loaded JS contex with Autoprefixer



58
59
60
61
# File 'lib/autoprefixer-rails.rb', line 58

def self.compiler
  @compiler ||= ExecJS.compile("window = this;\n" + js_file.read +
                               proxy('compile') + proxy('inspect'))
end

.inspect(browsers = []) ⇒ Object

Return string with selected browsers and prefixed CSS properties and values



64
65
66
# File 'lib/autoprefixer-rails.rb', line 64

def self.inspect(browsers = [])
  compiler.call('inspect', browsers)
end

.install(assets, browsers = nil) ⇒ Object

Add Autoprefixer for Sprockets environment in ‘assets`. You can specify `browsers` actual in your project. Also, you can set options with whitelist of `dirs` to be autoprefxied.



33
34
35
36
37
38
39
40
41
# File 'lib/autoprefixer-rails.rb', line 33

def self.install(assets, browsers = nil)
  assets.register_postprocessor 'text/css', :autoprefixer do |context, css|
    if defined?(Sass::Rails) and Sass::Rails::VERSION == '4.0.0.rc2'
      AutoprefixerRails.compile(css, browsers) rescue css
    else
      AutoprefixerRails.compile(css, browsers)
    end
  end
end

.js_fileObject

Path to Autoprefixer JS library



44
45
46
# File 'lib/autoprefixer-rails.rb', line 44

def self.js_file
  Pathname(__FILE__).dirname.join('../vendor/autoprefixer.js')
end

.proxy(func) ⇒ Object

Return JS code for proxy function to save this in Autoprefixer



49
50
51
52
53
54
55
# File 'lib/autoprefixer-rails.rb', line 49

def self.proxy(func)
  <<-JS
    window.#{ func } = function() {
      return autoprefixer.#{ func }.apply(autoprefixer, arguments)
    };
  JS
end