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.20130626'.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



66
67
68
69
# File 'lib/autoprefixer-rails.rb', line 66

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



72
73
74
# File 'lib/autoprefixer-rails.rb', line 72

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
42
43
44
45
46
47
48
49
# 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)
      begin
        AutoprefixerRails.compile(css, browsers)
      rescue ExecJS::ProgramError => e
        if e.message =~ /Can't parse CSS/
          css
        else
          raise e
        end
      end
    else
      AutoprefixerRails.compile(css, browsers)
    end
  end
end

.js_fileObject

Path to Autoprefixer JS library



52
53
54
# File 'lib/autoprefixer-rails.rb', line 52

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



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

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