| 1 | # Autoprefixer Rails [![Build Status][ci-img]][ci] |
| 2 | |
| 3 | <img align="right" width="94" height="71" |
| 4 | src="http://postcss.github.io/autoprefixer/logo.svg" |
| 5 | title="Autoprefixer logo by Anton Lovchikov"> |
| 6 | |
| 7 | [Autoprefixer] is a tool to parse CSS and add vendor prefixes to CSS rules |
| 8 | using values from the [Can I Use] database. This gem provides Ruby |
| 9 | and Ruby on Rails integration with this JavaScript tool. |
| 10 | |
| 11 | <a href="https://evilmartians.com/?utm_source=autoprefixer-rails"> |
| 12 | <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"> |
| 13 | </a> |
| 14 | |
| 15 | [Autoprefixer]: https://github.com/postcss/autoprefixer |
| 16 | [Can I Use]: http://caniuse.com/ |
| 17 | [PostCSS]: https://postcss.org/ |
| 18 | [ci-img]: https://github.com/ai/autoprefixer-rails/actions/workflows/ci.yml/badge.svg |
| 19 | [ci]: https://github.com/ai/autoprefixer-rails/actions/workflows/ci.yml |
| 20 | |
| 21 | ## Differences |
| 22 | |
| 23 | The best way to use Autoprefixer is with webpack or Gulp. |
| 24 | |
| 25 | Autoprefixer Rails doesn’t support these Autoprefixer features: |
| 26 | |
| 27 | * Browsers in `package.json`. |
| 28 | * Custom browsers usage statistics. |
| 29 | |
| 30 | ## Usage |
| 31 | |
| 32 | Windows users should install [Node.js]. Autoprefixer Rails doesn’t work with |
| 33 | old JScript in Windows. |
| 34 | |
| 35 | Autoprefixer Rails uses [ExecJS] that will use the best available JavaScript |
| 36 | runtime. Currently this gem is tested to work with Node.js version 10 and up |
| 37 | and with [mini_racer], but will not work with [therubyracer]. |
| 38 | |
| 39 | [Node.js]: http://nodejs.org/ |
| 40 | [ExecJS]: https://github.com/rails/execjs |
| 41 | [mini_racer]: https://github.com/discourse/mini_racer |
| 42 | [therubyracer]: https://github.com/cowboyd/therubyracer |
| 43 | |
| 44 | ### Ruby on Rails |
| 45 | |
| 46 | Add the `autoprefixer-rails` gem to your `Gemfile`: |
| 47 | |
| 48 | ```ruby |
| 49 | gem "autoprefixer-rails" |
| 50 | ``` |
| 51 | |
| 52 | Clear your cache: |
| 53 | |
| 54 | ```sh |
| 55 | rake tmp:clear |
| 56 | ``` |
| 57 | |
| 58 | Write your CSS (Sass, Stylus, LESS) rules without vendor prefixes |
| 59 | and Autoprefixer will apply prefixes for you. |
| 60 | For example in `app/assets/stylesheet/foobar.sass`: |
| 61 | |
| 62 | ```sass |
| 63 | :fullscreen a |
| 64 | display: flex |
| 65 | ``` |
| 66 | |
| 67 | Autoprefixer uses the Can I Use database with browser statistics and properties |
| 68 | support to add vendor prefixes automatically using the Asset Pipeline: |
| 69 | |
| 70 | ```css |
| 71 | :-webkit-full-screen a { |
| 72 | display: -webkit-box; |
| 73 | display: -webkit-flex; |
| 74 | display: flex |
| 75 | } |
| 76 | :-moz-full-screen a { |
| 77 | display: flex |
| 78 | } |
| 79 | :-ms-fullscreen a { |
| 80 | display: -ms-flexbox; |
| 81 | display: flex |
| 82 | } |
| 83 | :fullscreen a { |
| 84 | display: -webkit-box; |
| 85 | display: -webkit-flex; |
| 86 | display: -ms-flexbox; |
| 87 | display: flex |
| 88 | } |
| 89 | ``` |
| 90 | |
| 91 | If you need to specify browsers for your Rails project, you can save them |
| 92 | to `.browserslistrc` and place it under `app/assets/stylesheets/` |
| 93 | or any of its ancestor directories |
| 94 | |
| 95 | ``` |
| 96 | > 1% |
| 97 | last 2 versions |
| 98 | IE > 8 # comment |
| 99 | ``` |
| 100 | |
| 101 | See [Browserslist docs] for config format. But `> 5% in US` query is not |
| 102 | supported in Rails, because of ExecJS limitations. You should migrate to webpack |
| 103 | or Gulp if you want it. |
| 104 | |
| 105 | __Note: you have to clear cache (`rake tmp:clear`) for the configuration |
| 106 | to take effect.__ |
| 107 | |
| 108 | You can get what properties will be changed using a Rake task: |
| 109 | |
| 110 | ```sh |
| 111 | rake autoprefixer:info |
| 112 | ``` |
| 113 | |
| 114 | To disable Autoprefixer just remove postprocessor: |
| 115 | |
| 116 | ```ruby |
| 117 | AutoprefixerRails.uninstall(Rails.application.assets) |
| 118 | ``` |
| 119 | |
| 120 | [Browserslist docs]: https://github.com/ai/browserslist |
| 121 | [Firefox ESR]: http://www.mozilla.org/en/firefox/organizations/faq/ |
| 122 | |
| 123 | ### Sprockets |
| 124 | |
| 125 | If you use Sinatra or another non-Rails framework with Sprockets, |
| 126 | just connect your Sprockets environment with Autoprefixer and write CSS |
| 127 | in the usual way: |
| 128 | |
| 129 | ```ruby |
| 130 | assets = Sprockets::Environment.new do |env| |
| 131 | # Your assets settings |
| 132 | end |
| 133 | |
| 134 | require "autoprefixer-rails" |
| 135 | AutoprefixerRails.install(assets) |
| 136 | ``` |
| 137 | |
| 138 | ### Ruby |
| 139 | |
| 140 | If you need to call Autoprefixer from plain Ruby code, it’s very easy: |
| 141 | |
| 142 | ```ruby |
| 143 | require "autoprefixer-rails" |
| 144 | prefixed = AutoprefixerRails.process(css, from: 'main.css').css |
| 145 | ``` |
| 146 | |
| 147 | ### Compass |
| 148 | |
| 149 | You should consider using Gulp instead of Compass binary, |
| 150 | because it has better Autoprefixer integration and many other awesome plugins. |
| 151 | |
| 152 | But if you can’t move from Compass binary right now, there’s a hack |
| 153 | to run Autoprefixer after `compass compile`. |
| 154 | |
| 155 | Install `autoprefixer-rails` gem: |
| 156 | |
| 157 | ``` |
| 158 | gem install autoprefixer-rails |
| 159 | ``` |
| 160 | |
| 161 | and add post-compile hook to `config.rb`: |
| 162 | |
| 163 | ```ruby |
| 164 | require 'autoprefixer-rails' |
| 165 | |
| 166 | on_stylesheet_saved do |file| |
| 167 | css = File.read(file) |
| 168 | map = file + '.map' |
| 169 | |
| 170 | if File.exists? map |
| 171 | result = AutoprefixerRails.process(css, |
| 172 | from: file, |
| 173 | to: file, |
| 174 | map: { prev: File.read(map), inline: false }) |
| 175 | File.open(file, 'w') { |io| io << result.css } |
| 176 | File.open(map, 'w') { |io| io << result.map } |
| 177 | else |
| 178 | File.open(file, 'w') { |io| io << AutoprefixerRails.process(css) } |
| 179 | end |
| 180 | end |
| 181 | ``` |
| 182 | |
| 183 | ## Visual Cascade |
| 184 | |
| 185 | By default, Autoprefixer will change CSS indentation to create nice visual |
| 186 | cascade of prefixes. |
| 187 | |
| 188 | ```css |
| 189 | a { |
| 190 | -webkit-box-sizing: border-box; |
| 191 | -moz-box-sizing: border-box; |
| 192 | box-sizing: border-box |
| 193 | } |
| 194 | ``` |
| 195 | |
| 196 | You can disable this by specifying `cascade: false` in `config/autoprefixer.yml` |
| 197 | or in `process()` options. |
| 198 | |
| 199 | ## Source Map |
| 200 | |
| 201 | Autoprefixer will generate a source map if you set `map` option to `true` in |
| 202 | `process` method. |
| 203 | |
| 204 | You must set input and output CSS files paths (by `from` and `to` options) |
| 205 | to generate a correct map. |
| 206 | |
| 207 | ```ruby |
| 208 | result = AutoprefixerRails.process(css, |
| 209 | map: true, |
| 210 | from: 'main.css', |
| 211 | to: 'main.out.css') |
| 212 | ``` |
| 213 | |
| 214 | Autoprefixer can also modify previous source map (for example, from Sass |
| 215 | compilation). Just set original source map content (as string) as `prev` |
| 216 | in the `map` option. |
| 217 | |
| 218 | ```ruby |
| 219 | result = AutoprefixerRails.process(css, { |
| 220 | map: { prev: File.read('main.sass.css.map') }, |
| 221 | from: 'main.sass.css', |
| 222 | to: 'main.min.css') |
| 223 | |
| 224 | result.map #=> Source map from main.sass to main.min.css |
| 225 | ``` |
| 226 | |
| 227 | See all options in [PostCSS docs]. AutoprefixerRails will convert Ruby style |
| 228 | to JS style, so you can use `map: { sources_content: false }` |
| 229 | instead of camelcase `sourcesContent`. |
| 230 | |
| 231 | [PostCSS docs]: https://github.com/postcss/postcss#source-map-1 |
| 232 | |
| 233 | |
| 234 | ## Security Contact |
| 235 | |
| 236 | To report a security vulnerability, please use the [Tidelift security contact]. |
| 237 | Tidelift will coordinate the fix and disclosure. |
| 238 | |
| 239 | [Tidelift security contact]: https://tidelift.com/security |