gulp的插件使用
// 引入插件
var gulp = require('gulp'),
less = require('gulp-less'), //编译less
cssmin = require('gulp-cssmin') //压缩css
autoprefixer = require('gulp-autoprefixer'), //兼容css
rev = require('gulp-rev'), //重命名(加密) 解决浏览器端缓存
imagemin = require('gulp-imagemin'), //压缩图片
rename = require('gulp-rename'), //重命名
useref = require('gulp-useref'), //根据注释 合并或删除html文件中的内容
uglify = require('gulp-uglify'), //压缩js
gulpif = require('gulp-if'), //条件判断
revCollector = require('gulp-rev-collector'), //加密
htmlmin = require('gulp-htmlmin'); //压缩html
// 处理css
gulp.task('css', function () {
return gulp.src('./public/less/main.less')
.pipe(less())
.pipe(cssmin())
.pipe(autoprefixer())
.pipe(rev())
.pipe(gulp.dest('./release/public/css'))
.pipe(rev.manifest())
.pipe(rename('css-mainfest.json'))
.pipe(gulp.dest('./release/rev'));
});
// 图片处理
gulp.task('image', function () {
return gulp.src(['./public/images/**/*', './uploads/**/*'], {base: './'})
.pipe(imagemin())
.pipe(rev())
.pipe(gulp.dest('./release/'))
.pipe(rev.manifest())
.pipe(rename('image-manifest.json'))
.pipe(gulp.dest('./release/rev'));
});
// md5名
gulp.task('useref', function () {
return gulp.src('./index.html')
.pipe(useref())
.pipe(gulpif('*.js', uglify()))
.pipe(gulpif('*.js', rev()))
.pipe(gulp.dest('./release'))
.pipe(rev.manifest())
.pipe(rename('js-manifest.json'))
.pipe(gulp.dest('./release/rev'));
});
// 压缩html
gulp.task('html', function () {
return gulp.src('./views/*.html')
.pipe(htmlmin())
.pipe(gulp.dest('./release/views'));
});
// 其它内容
gulp.task('other', function () {
gulp.src(['./api/*', './public/fonts/*'], {base: './'})
.pipe(gulp.dest('./release'));
});
// 路径替换
gulp.task('rev', ['css', 'image', 'useref', 'html'], function () {
gulp.src(['./release/rev/*.json', './release/?(views)/*.html'], {base: './release'})
.pipe(revCollector())
.pipe(gulp.dest('./release'));
});
//终极调用
gulp.task('default', ['rev', 'other', 'html']);