简介:
使用gulp-less插件将less文件编译成css,当有less文件发生改变自动编译less,并保证less语法错误或出现异常时能正常工作并提示错误信息。
安装:命令提示符执行 npm install gulp-less –save-dev
配置gulpfile.js
1、基本使用
var gulp = require('gulp'), less = require('gulp-less'); gulp.task('testLess', function () { gulp.src('src/less/index.less') .pipe(less()) .pipe(gulp.dest('src/css')); });
2、编译多个less文件
var gulp = require('gulp'), less = require('gulp-less'); gulp.task('testLess', function () { gulp.src(['src/less/index.less','src/less/detail.less']) //多个文件以数组形式传入 .pipe(less()) .pipe(gulp.dest('src/css')); //将会在src/css下生成index.css以及detail.css });
3、匹配符“!”,“*”,“**”,“{}”
var gulp = require('gulp'), less = require('gulp-less'); gulp.task('testLess', function () { //编译src目录下的所有less文件 //除了reset.less和test.less(**匹配src/less的0个或多个子文件夹) gulp.src(['src/less/*.less', '!src/less/**/{reset,test}.less']) .pipe(less()) .pipe(gulp.dest('src/css')); });
4、调用多模块(编译less后压缩css)
var gulp = require('gulp'), less = require('gulp-less'), //确保本地已安装gulp-minify-css [cnpm install gulp-minfy-css --save-dev] cssmin = require('gulp-minify-css'); gulp.task('testLess', function () { gulp.src('src/less/index.less') .pipe(less()) .pipe(cssmin()) //兼容IE7及以下需设置compatibility属性 .pipe(cssmin({compatibility: 'ie7'})) .pipe(gulp.dest('src/css')); });
转载请注明:OnlyLing - Web 前端开发者 » gulp教程之gulp-less