注入package.json(此json文件需要自己在当前项目目录下创建即可--不能有注释)安装:
npm install gulp-uglify --save-dev
全局安装:
npm install gulp-uglify -g
整站静态文件压缩配置文件gulpfile.js(html/css/image/js; 前提是安装下列require用到的模块):
var gulp = require('gulp'),//基础库
htmlmin = require('gulp-htmlmin'),//html压缩
cssmin = require('gulp-minify-css'),//css压缩
jshint = require('gulp-jshint'),//js检查
uglify = require('gulp-uglify'),//js压缩
imagemin = require('gulp-imagemin'),//图片压缩
pngquant = require('imagemin-pngquant'),//图片深入压缩
imageminOptipng = require('imagemin-optipng'),
imageminSvgo = require('imagemin-svgo'),
imageminGifsicle = require('imagemin-gifsicle'),
imageminJpegtran = require('imagemin-jpegtran'),
domSrc = require('gulp-dom-src'),
cheerio = require('gulp-cheerio'),
processhtml = require('gulp-processhtml'),
Replace = require('gulp-replace'),
cache = require('gulp-cache'),//图片压缩缓存
clean = require('gulp-clean'),//清空文件夹
conCat = require('gulp-concat'),//文件合并
plumber=require('gulp-plumber'),//检测错误
gutil=require('gulp-util');//如果有自定义方法,会用到
var date = new Date().getTime();
gulp.task('clean',function(){
return gulp.src('min/**',{read:false})
.pipe(clean());
});
function errrHandler( e ){
// 控制台发声,错误时beep一下
gutil.beep();
gutil.log(e);
this.emit('end');
}
gulp.task('cleanCash', function (done) {//清除缓存
return cache.clearAll(done);
});
gulp.task('htmlmin', function () {
var options = {
removeComments: true,//清除HTML注释
collapseWhitespace: true,//压缩HTML
collapseBooleanAttributes: false,//省略布尔属性的值 ==>
removeEmptyAttributes: false,//删除所有空格作属性值 ==>
removeScriptTypeAttributes: true,//删除