網上關於
postcss
是什麼的介紹真的是太多了。。。
簡單粗暴的說,postcss
就是一款相似babel
的樣式轉換器!
多說無益,下面就用一個demo來演示一下postcss
的用法。css
cd ~/workspace/postcss #進入你本身的工具目錄 mkdir postcss cd postcss mkidr css npm init #初始化package.json,一路回車便可
進入到css目錄,新建一個index.css
文件,鍵入以下內容:web
button { border-radius: 4px; transition: all 0.8s; color: $red; width: 100px; }
npm install gulp gulp-postcss --save #安裝須要的包,這裏使用gulp來構建、打包
postcss
文件夾根目錄新建一個gulpfile.js
文件,鍵入以下內容:npm
var gulp = require('gulp'); var postcss = require('gulp-postcss'); gulp.task('css', function() { //postcss處理器列表,暫時爲空 var processors = []; return gulp.src('./css/*.css') .pipe(postcss(processors)) .pipe(gulp.dest('./build/')); });
執行gulp css
,測試一下打包是否正常!json
查看生成的
build/index.css
文件,和原始文件同樣。
由於目前processors
數組尚未加入任何插件!gulp
修改gulpfile.js
,完成後以下:數組
var gulp = require('gulp'); var postcss = require('gulp-postcss'); gulp.task('css', function() { var processors = [ autoprefixer ]; return gulp.src('./css/*.css') .pipe(postcss(processors)) .pipe(gulp.dest('./build/')); }); function autoprefixer(css) { css.walkDecls(function(decl) { if (decl.prop === 'border-radius' || decl.prop === 'transition') { decl.cloneBefore({prop: '-moz-' + decl.prop}); decl.cloneBefore({prop: '-o-' + decl.prop}); decl.cloneBefore({prop: '-webkit-' + decl.prop}); } return decl; }); }
從新執行gulp css
打包,完成後查看`build/index.css',以下:babel
button { -moz-border-radius: 4px; -o-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px; -moz-transition: all 0.8s; -o-transition: all 0.8s; -webkit-transition: all 0.8s; transition: all 0.8s; color: $red; width: 100px; }
修改gulpfile.js
,完成後以下:工具
var gulp = require('gulp'); var postcss = require('gulp-postcss'); gulp.task('css', function() { var processors = [ autoprefixer, resoleVar ]; return gulp.src('./css/*.css') .pipe(postcss(processors)) .pipe(gulp.dest('./build/')); }); function autoprefixer(css) { css.walkDecls(function(decl) { if (decl.prop === 'border-radius' || decl.prop === 'transition') { decl.cloneBefore({prop: '-moz-' + decl.prop}); decl.cloneBefore({prop: '-o-' + decl.prop}); decl.cloneBefore({prop: '-webkit-' + decl.prop}); } return decl; }); } function resoleVar(css) { css.walkDecls(function(decl) { if (decl.prop === 'color' && decl.value.indexOf('$red') > -1) { decl.value = decl.value.replace('$red', '#f00'); } return decl; }); }
從新執行gulp css
打包,完成後查看`build/index.css',以下:post
button { -moz-border-radius: 4px; -o-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px; -moz-transition: all 0.8s; -o-transition: all 0.8s; -webkit-transition: all 0.8s; transition: all 0.8s; color: #f00; width: 100px; }
修改gulpfile.js
,完成後以下:學習
var gulp = require('gulp'); var postcss = require('gulp-postcss'); gulp.task('css', function() { var processors = [ autoprefixer, resoleVar, px2rem ]; return gulp.src('./css/*.css') .pipe(postcss(processors)) .pipe(gulp.dest('./build/')); }); function autoprefixer(css) { css.walkDecls(function(decl) { if (decl.prop === 'border-radius' || decl.prop === 'transition') { decl.cloneBefore({prop: '-moz-' + decl.prop}); decl.cloneBefore({prop: '-o-' + decl.prop}); decl.cloneBefore({prop: '-webkit-' + decl.prop}); } return decl; }); } function resoleVar(css) { css.walkDecls(function(decl) { if (decl.prop === 'color' && decl.value.indexOf('$red') > -1) { decl.value = decl.value.replace('$red', '#f00'); } return decl; }); } function px2rem(css) { css.walkDecls(function(decl) { if(/\d+px/.test(decl.value)) { decl.value = decl.value.replace(/\d+px/, function(dest) { return parseInt(dest) / 20 + 'rem'; }) } return decl; }); }
從新執行gulp css
打包,完成後查看`build/index.css',以下:
button { -moz-border-radius: 0.2rem; -o-border-radius: 0.2rem; -webkit-border-radius: 0.2rem; border-radius: 0.2rem; -moz-transition: all 0.8s; -o-transition: all 0.8s; -webkit-transition: all 0.8s; transition: all 0.8s; color: #f00; width: 5rem; }
通過了上面這三個簡單的
processor
以後,相信你們對postcss
的認識會更直白一點了吧。。。
npm install autoprefixer --save
和以前咱們本身實現
autoprefixer
的相似,只不過這個插件作的更好更全一點。
npm install precss --save
press語法和Sass極其類似,你能夠絕不費力的使用它。
和上面的同樣,加入到processors
便可,以下:
/** * 此處省略N行 */ var autoprefixer = require('autoprefixer'); var precss = require('precss'); /** * 此處省略N行 */ var processors = [ autoprefixer({browsers: ['last 10 version'], cascade: false, remove: false}), resoleVar, px2rem, precss ]; /** * 此處省略N行 */
爲了驗證插件確實生效了,修改一下css文件,以下:
button { border-radius: 4px; transition: all 0.8s; color: $red; width: 100px; box-sizing: border-box; } .menu { a { text-decoration: none; } }
執行gulp css
再次從新打包,結果以下:
button { -webkit-border-radius: 0.2rem; border-radius: 0.2rem; -webkit-transition: all 0.8s; transition: all 0.8s; color: #f00; width: 5rem; -webkit-box-sizing: border-box; box-sizing: border-box; } .menu a { text-decoration: none; }
這裏就介紹這兩個插件來拋磚引玉一下!其實大部分都會使用postcss已有的一些插件,而不多本身去造輪子。固然,若是你有這樣的特殊需求或者想學習一下,但願上面那三個例子能夠幫到你,你們加油!