Sha256: 8000aa99c2d3f6e938c4d3baec7cd0f2fc4117312590d9c725e8e52d8f8f5a2a

Contents?: true

Size: 1.99 KB

Versions: 28

Compression:

Stored size: 1.99 KB

Contents

=begin
Copyright 2013 Andrey “A.I.” Sitnik <andrey@sitnik.ru>,
sponsored by Evil Martians.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
=end

require 'execjs'

module AutoprefixerRails
  # Ruby to JS wrapper for Autoprefixer compiler instance
  class Compiler
    def initialize(browsers=nil)
      @browsers = browsers || []
    end

    # Return prefixed `css`
    def compile(css)
      compiler.call('compile', css)
    end

    # Return, which browsers and prefixes will be used
    def inspect
      compiler.call('inspect')
    end

    # Lazy load for JS instance
    def compiler
      @compiler ||= ExecJS.compile(build_js)
    end

    private

    # Cache autoprefixer.js content
    def read_js
      @@js ||= Pathname(__FILE__).join("../../../vendor/autoprefixer.js").read
    end

    # Return compiler JS with some extra methods
    def build_js
      read_js + create_instance + add_proxy('compile') + add_proxy('inspect')
    end

    # Return JS code to create Autoprefixer instance
    def create_instance
      if @browsers.empty?
        "var compiler = autoprefixer;"
      else
        browsers = @browsers.map(&:to_s).join("', '")
        "var compiler = autoprefixer('#{browsers}');"
      end
    end

    # Return JS code to create proxy methods
    def add_proxy(func)
      <<-JS
        var #{ func } = function() {
          return compiler.#{ func }.apply(compiler, arguments)
        };
      JS
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
autoprefixer-rails-0.8.20131213 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131209 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131205 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131104 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131029 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131020 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131017 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131015 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131009 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131007 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131006 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20131001 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20130923 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20130919 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20130911 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20130906 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20130903 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.8.20130902 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.7.20130824 lib/autoprefixer-rails/compiler.rb
autoprefixer-rails-0.7.20130810 lib/autoprefixer-rails/compiler.rb