Vendor prefix handling for j2c in the browser, built on top of Lea Verou's PrefixFree
npm install j2c-plugin-prefix-browserThis j2c plugin does client side vendor prefix insertion (and some polyfilling) for:
- properties and values (including flexbox).
- selectors,
- at-rules and at-rules parameters
See below for a detailed feature set.
Unlike Autoprefixer and the inline-style-prefixer, it relies on feature detection rather than browser sniffing. It weights ~3KB minified and gzipped.
You test it live in your browser.
The plugin is well tested (385 assertions, many of which are deepEquals on nested objects) and has 100% branch coverage (which doesn't mean it is bug free, off course).
----
- Usage
- Features
- Properties
- Values
- Keywords
- Functions
- Values that are property names
- Flexbox
- Flexbox 2009 (-moz-box and -webkit-box)
- Flexbox 2012 (-ms-flexbox)
- Final spec, prefixed (-webkit-flex)
- Selectors
- At rules
- At rules parameters (resolution and @supports)
- What about the server side?
- And what about speed?
- Thanks
- License: MIT
----
``BASH`
$ npm install --save j2c-plugin-prefix-browser
#or
$ yarn add j2c-plugin-prefix-browser
`JS
import {prefixPlugin} from "j2c-plugin-prefix-browser"
import {J2c} from "j2c"
// alternatively
const prefixPlugin = require("j2c-plugin-prefix-browser").prefixPlugin
const J2c = require("j2c")
// anyways...
const j2c = new J2c(prefixPlugin())
j2c.sheet({
"@keyframes foo": {
from: {size: "100px"},
to: {size: "200px"}
}
})
`
`CSS
/ Assuming that it ran in an old Safari version: /
@-webkit-keyframes foo_j2c-xxx {
from {
size: 100px;
}
to {
size: 200px;
}
}
`
Here's what supported, please file an issue if you see something missing.
Properties that need a prefix will get one. Properties supported both with and without won't.
The following values can get a prefix:
#### Keywords
##### All properties:
=> initial
##### cursor:
=> grab, grabbing, zoom-in, zoom-out
##### display:
=> grid, inline-grid and the flexbox zoo (see below)
##### position:
=> sticky
##### width:, column-width:, height:, max-height:, max-width:, min-height:, min-width:
=> contain-floats, fill-available, fit-content, max-content and min-content
#### Functions
calc(), element(), cross-fade() and gradients are prefixed, even if they are nested in other functions or part of a list of values.
Prefixed gradients also have their angle changed from trigonometric to clockwise coordinates.
#### Values that are property names
transiton:, transition-property: and will-change: take properties as arguments. The latter are prefixed where needed.
The plugin translates the final flexbox spec to its older versions as much as possible. Squigly arrows indicate an approximate mapping.
#### Flexbox 2009 (-moz-box and -webkit-box)
##### Properties
- align-content => ? no mapping AFAIK please chime in if you know any)align-self
- => ?align-items
- => -x-box-alignflex
- => -x-box-flexflex-basis
- => ?flex-direction
- => -x-box-direction + -x-box-orientflex-flow
- => -x-box-direction + -x-box-orient and/or -x-box-linesflex-grow
- => ?flex-shrink
- => ?flex-wrap
- => -x-box-linesjustify-content
- => -x-box-packorder
- => -x-box-ordinal-group
##### Values
- flex => -x-box
- inline-flex => -x-inline-box
- column => block-axis + normal (respectively for -x-box-orient and box-direction)column-reverse
- => block-axis + reverserow
- => inline-axis + normalrow-reverse
- => inline-axis + reverseflex-end
- => endflex-start
- => startnowrap
- => singlespace-around
- ~> justifyspace-between
- => justifywrap
- => multiplewrap-reverse
- ~> multiple
#### Flexbox 2012 (-ms-flexbox)
##### Properties
- align-content => -ms-flex-line-pack
- align-items => -ms-flex-align
- align-self => -ms-flex-item-align
- flex => -ms-flexflex-basis
- => -ms-preferred-sizeflex-direction
- => -ms-flex-directionflex-flow
- => -ms-flex-flowflex-grow
- => -ms-flex-positiveflex-shrink
- => -ms-flex-negativeflex-wrap
- => -ms-flex-wrapjustify-content
- => -ms-flex-packorder
- => -ms-flex-order
##### Values
- flex => -ms-flexbox
- inline-flex => -ms-inline-flexbox
- column => columncolumn-reverse
- => column-reverserow
- => rowrow-reverse
- => row-reverseflex-end
- => endflex-start
- => startspace-around
- => distributespace-between
- => justifywrap
- => wrapwrap-reverse
- => wrap-reverse
#### Final spec, prefixed (-webkit-flex)
No special treatment is necessary, prefixes are applied normally where needed.
- :any-link => :-x-any-link
- :read-only => :-x-read-only
- :read-write => :-x-read-write
- ::selection => ::-x-selection
- :fullscreen => :-x-fullscreen or :-x-fullscreen
- ::backdrop => ::-x-backdrop
- ::placeholder => ::-x-placeholder, :-x-placeholder:, ::-x-input-placeholder or :-x-input-placeholder
The :-webkit-scrollbar pseudo-element/pseudo-class family not implemented since it is webkit-only and not on a standard track AFAIK.
@keyframes, @document and @viewport get a prefix where needed.
#### At rules parameters (resolution and @supports)
`CSS`
@media (min-resolution: 2dppx){}
(resp. max-resolution and resolution) can be converted to one of the following, when appropriate:
`CSS
@media (-webkit-min-device-pixel-ratio:2){}
@media (min--moz-device-pixel-ratio:2){}
@media (-o-device-pixel-ratio 20/10){}
@media (min-resolution: 192dpi){} // Mostly IE
`
The parameters of @supports have also their properties and values prefixed automatically.
The plugin supports a way to bypass the detection code and provide fixers objects manually, but these would have to be constructed, either from the caniuse.com data set and cross-checking it by using our detector code with a service like SauceLabs, BrowserStack or Browserling.
That scheme would only work when the server knows the user agent. The plugin doesn't support inserting more than one prefix, or keeping the originals as Autoprefixer and inline-style-prefixer do. Thankfully, for that case (and until a proper DB is constructed), Autoprefixer can be used as a fallback.
The plugin is designed to be as fast as possible. Most of the feature detection is performed upfront to enable the fastest operation possible while applying prefixes. Allocations and branches are kept to a bare minimum after initialization (the prefixes are applied once and then cached). No objects beside strings are created while prefixing (with one exception when flex-flow has to be translated to the 2009 equivalent, the value is .split(' ')` into an array.). Some tweaks are still possible, benchmarks will be needed.
Many thanks to Lea Verou for PrefixFree from which most of the feature detection code originates, Robin Frischmann for the inline-style-prefixer whose plugin folder was an inspiration, Andrey Sitnik for the Autoprefixer which is also a resource I've used while building this, and [Ben Briggs]() who's always been helpful in the PostCSS-related chats.
Thanks as well to all the folks that hang out in the gitter channel for keeping my motivation up.