Code Monkey home page Code Monkey logo

autoprefixer-rails's Introduction

Autoprefixer Rails Build Status

Autoprefixer is a tool to parse CSS and add vendor prefixes to CSS rules using values from the Can I Use. This gem provides Ruby and Ruby on Rails integration with this JavaScript tool.

Sponsored by Evil Martians

Differences

The best way to use Autoprefixer is with webpack or Gulp.

Autoprefixer Rails doesn’t support this Autoprefixer’s features:

  • Browsers in package.json.
  • Custom browsers usage statistics.

Usage

Windows users should install node.js. Autoprefixer Rails doesn’t work with old JScript in Windows.

Ruby on Rails

Add the autoprefixer-rails gem to your Gemfile:

gem "autoprefixer-rails"

Clear your cache:

rake tmp:clear

Write your CSS (Sass, Stylus, LESS) rules without vendor prefixes and Autoprefixer will apply prefixes for you. For example in app/assets/stylesheet/foobar.sass:

:fullscreen a
  display: flex

Autoprefixer uses Can I Use database with browser statistics and properties support to add vendor prefixes automatically using the Asset Pipeline:

:-webkit-full-screen a {
    display: -webkit-box;
    display: -webkit-flex;
    display: flex
}
:-moz-full-screen a {
    display: flex
}
:-ms-fullscreen a {
    display: -ms-flexbox;
    display: flex
}
:fullscreen a {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex
}

If you need to specify browsers for your Rails project, you can save them to

  • browserslist and place it under app/assets/stylesheets/ or any of its ancestor directories

    > 1%
    last 2 versions
    IE > 8 # comment
    
  • Or config/autoprefixer.yml

    flexbox: no-2009
    browsers:
      - "> 1%"
      - "last 2 versions"
      - "IE > 8"

See Browserslist docs for config format. But > 5% in US query is not supported in Rails, because of ExecJS limitations. You should migrate to webpack or Gulp if you want it.

Note: you have to clear cache (rake tmp:clear) for the configuration to take effect.

You can get what properties will be changed using a Rake task:

rake autoprefixer:info

To disable Autoprefixer just remove postprocessor:

AutoprefixerRails.uninstall(Rails.application.assets)

Sprockets

If you use Sinatra or other non-Rails frameworks with Sprockets, just connect your Sprockets environment with Autoprefixer and write CSS in the usual way:

assets = Sprockets::Environment.new do |env|
  # Your assets settings
end

require "autoprefixer-rails"
AutoprefixerRails.install(assets)

Ruby

If you need to call Autoprefixer from plain Ruby code, it’s very easy:

require "autoprefixer-rails"
prefixed = AutoprefixerRails.process(css, from: 'main.css').css

You can specify browsers by browsers option:

AutoprefixerRails.process(css, from: 'a.css', browsers: ['> 1%', 'ie 10']).css

Compass

You should consider using Gulp instead of Compass binary, because it has better Autoprefixer integration and many other awesome plugins.

But if you can’t move from Compass binary right now, there’s a hack to run Autoprefixer after compass compile.

Install autoprefixer-rails gem:

gem install autoprefixer-rails

and add post-compile hook to config.rb:

require 'autoprefixer-rails'

on_stylesheet_saved do |file|
  css = File.read(file)
  map = file + '.map'

  if File.exists? map
    result = AutoprefixerRails.process(css,
      from: file,
      to:   file,
      map:  { prev: File.read(map), inline: false })
    File.open(file, 'w') { |io| io << result.css }
    File.open(map,  'w') { |io| io << result.map }
  else
    File.open(file, 'w') { |io| io << AutoprefixerRails.process(css) }
  end
end

Visual Cascade

By default, Autoprefixer will change CSS indentation to create nice visual cascade of prefixes.

a {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box
}

You can disable it by cascade: false in config/autoprefixer.yml or in process() options.

Source Map

Autoprefixer will generate source map, if you set map option to true in process method.

You must set input and output CSS files paths (by from and to options) to generate correct map.

result = AutoprefixerRails.process(css,
    map:   true,
    from: 'main.css',
    to:   'main.out.css')

Autoprefixer can also modify previous source map (for example, from Sass compilation). Just set original source map content (as string) to map option:

result = AutoprefixerRails.process(css, {
    map:   File.read('main.sass.css.map'),
    from: 'main.sass.css',
    to:   'main.min.css')

result.map #=> Source map from main.sass to main.min.css

See all options in PostCSS docs. AutoprefixerRails will convert Ruby style to JS style, so you can use map: { sources_content: false } instead of camelcase sourcesContent.

autoprefixer-rails's People

Contributors

abemedia avatar ai avatar ajb avatar atambo avatar bianjp avatar deepj avatar diclophis avatar dinuz avatar edariedl avatar glebm avatar hnnsgstfssn avatar iainbeeston avatar jclay avatar jimryan avatar josacar avatar josh avatar jpurcell001 avatar kamen-hursev avatar kirs avatar kossnocorp avatar le0pard avatar marcandre avatar nhunzaker avatar porada avatar ptrikutam avatar richardvenneman avatar schneems avatar twalpole avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.