前端工具gulp2
阅读原文时间:2023年07月15日阅读:1

var gulp = require('gulp');
var less = require('gulp-less');
var htmlmin = require('gulp-htmlmin');
var imagemin = require('gulp-imagemin');
var cssmin = require('gulp-cssmin');
var rename = require('gulp-rename');

//生成的路径、文件夹
var dist = 'dist/';
//需要监控的文件定义为一个对象
var source = {
less: ['src/less/*.less'],
css:[dist+'origin/*.css'],
images: ['src/images/*.{png,jpg,gif,ico,jpeg}'],
html: ['src/*.html']

};

gulp.task('testCssmin',function() {
gulp.src(source.less) //多个文件以数组形式传入
.pipe(less())
.pipe(gulp.dest(dist+'css/origin'))
.pipe(cssmin())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest(dist+'css/'));
});

gulp.task('testImagemin', function () {
gulp.src(source.images)
.pipe(imagemin({
// optimizationLevel: 3, //类型:Number 默认:3 取值范围:0-7(优化等级)
// progressive: true, //类型:Boolean 默认:false 无损压缩jpg图片
// interlaced: true, //类型:Boolean 默认:false 隔行扫描gif进行渲染
// multipass: true //类型:Boolean 默认:false 多次优化svg直到完全优化
}))
.pipe(gulp.dest(dist+'images'));
});

gulp.task('testHtmlmin', function () {
var options = {
removeComments: true,//清除HTML注释
collapseWhitespace: true,//压缩HTML
collapseBooleanAttributes: true,//省略布尔属性的值 ==>
removeEmptyAttributes: true,//删除所有空格作属性值 ==>
removeScriptTypeAttributes: true,//删除