基于songsiqi/postcss-adaptive 写的一个把rpx
转换成rem
的插件。基本用法和其一致。
- 该插件使用是为了简化原来插件使用注释的方法去做转化。因此如果改成:如果遇到
rpx
则换算成rem
单位。如果遇到px
则不换算。 - 配置方面,仅仅留下
baseDpr
,remUnit
和remPrecision
.selector {
height: 32px;
width: 75rpx;
}
/* �默认配置下转换之后 */
.selector {
height: 32px;
width: 1rem;
}
npm i --save-dev postcss-adaptive-rpx
var postcss = require('postcss');
var adaptive = require('postcss-adaptive');
var originCssText = '...';
var newCssText = postcss().use(adaptive({ remUnit: 75 })).process(originCssText).css;
var gulp = require('gulp');
var postcss = require('gulp-postcss');
var adaptive = require('postcss-adaptive');
gulp.task('default', function () {
var processors = [adaptive({ remUnit: 75 })];
return gulp.src('./src/*.css')
.pipe(postcss(processors))
.pipe(gulp.dest('./dest'));
});
var adaptive = require('postcss-adaptive');
module.exports = {
module: {
loaders: [
{
test: /\.css$/,
loader: "style-loader!css-loader!postcss-loader"
}
]
},
postcss: function () {
return [adaptive({ remUnit: 75 })];
}
}
module.exports = function (grunt) {
grunt.initConfig({
postcss: {
options: {
processors: [
adaptive({ remUnit: 75 })
]
},
dist: {
src: 'src/*.css',
dest: 'build'
}
}
});
grunt.loadNpmTasks('grunt-postcss');
grunt.registerTask('default', ['postcss']);
}
adaptive(config)
Config:
remUnit
: number, rem unit value (default: 75)baseDpr
: number, base device pixel ratio (default: 2)remPrecision
: number, rem value precision (default: 6)