by Tachun LIN | @tachunL
$brandColor: red;
body {
color: $brandColor;
}
.foo {
.bar {
background-color: red;
}
}
@mixin vertical-gradient( $top, $bottom ) {
background: linear-gradient( top, $top 0%, $bottom 100% );
}
npm install --save gulp-postcss
// With preprocessor:
@mixin font-size($sizeValue: 16) {
font-size: $sizeValue + px; // IE fallback
font-size: ($sizeValue / 16) + rem;
}
.convert {
@include font-size(24); // return font-size: 1.5rem;
}
// Always someone will do this:
.con {
font-size: 24px; // should use @include font-size(24);
}
// With PostCSS & pxtorem plugin
.convert {
font-size: 16px; // converted to 1rem
}
.logo {
background: url('/images/logo.png') no-repeat 0 0 at-2x;
}
Output in css:
.logo {
background: url('/public/images/logo.png') no-repeat 0 0;
}
@media (min--moz-device-pixel-ratio: 1.5),
(-o-min-device-pixel-ratio: 3/2),
(-webkit-min-device-pixel-ratio: 1.5),
(min-device-pixel-ratio: 1.5),
(min-resolution: 144dpi),
(min-resolution: 1.5dppx)
{
.logo {
background: url('/public/images/logo@2x.png') no-repeat 0 0;
}
}
h1::before, h1:before {
margin: 10px 20px 10px 20px;
color: #ff0000;
-webkit-border-radius: 16px;
border-radius: 16px;
font-weight: normal;
font-weight: normal;
}
h1:before{margin:10px 20px;color:red;border-radius:16px;font-weight:400}
10,000 selectors, each with 5 properties using the function. This makes 50,000 things to process
Sass: 13s
PostCss: 372ms
gulpfile.js
var processors = [
require('postcss-import')({
from: 'src/css/main.css'
}),
require('postcss-simple-vars'),
require('postcss-mixins'),
require('postcss-nested'),
require('autoprefixer')({
browsers: ['last 2 versions', '> 2%']
}),
require('cssnano')
];
gulp.task('postcss', function() {
return gulp.src('src/css/**/*.css')
.pipe(postCSS(processors))
.pipe(gulp.dest('build/css'))
});
postcss-font-magician
body {
font-family: "Alice";
}
@font-face {
font-family: "Alice";
font-style: normal;
font-weight: 400;
src: local("Alice"), local("Alice-Regular"),
url("http://fonts.gstatic.com/s/alice/v7/sZyKh5NKrCk1xkCk_F1S8A.eot?#") format("eot"),
url("http://fonts.gstatic.com/s/alice/v7/l5RFQT5MQiajQkFxjDLySg.woff2") format("woff2"),
url("http://fonts.gstatic.com/s/alice/v7/_H4kMcdhHr0B8RDaQcqpTA.woff") format("woff"),
url("http://fonts.gstatic.com/s/alice/v7/acf9XsUhgp1k2j79ATk2cw.ttf") format("truetype")
}
body {
font-family: "Alice";
}
require('postcss-font-magician')({
hosted: '../fonts'
});
postcss-simple-vars
// colors.js
module.exports = {
blue: '#056ef0'
}
// gulpfile.js
var colors = require('./config/colors');
var vars = require('postcss-simple-vars')
gulp.task('css', function () {
return gulp.src('./src/*.css')
.pipe(postcss([ vars({ variables: colors }) ]))
.pipe(gulp.dest('./dest'));
});
// css
body{
background-color: $colors.blue;
}