vargulp=require('gulp');//Plugins模块获取
varminifycss=require('gulp-minify-css');varuglify=require('gulp-uglify');varhtmlmin=require('gulp-htmlmin');varhtmlclean=require('gulp-htmlclean');//压缩css
gulp.task('minify-css',function(){returngulp.src('./public/**/*.css').pipe(minifycss()).pipe(gulp.dest('./public'));});//压缩html
gulp.task('minify-html',function(){returngulp.src('./public/**/*.html').pipe(htmlclean()).pipe(htmlmin({removeComments:true,minifyJS:true,minifyCSS:true,minifyURLs:true,})).pipe(gulp.dest('./public'))});//压缩js 不压缩min.js
gulp.task('minify-js',function(){returngulp.src(['./public/**/*.js','!./public/**/*.min.js']).pipe(uglify()).pipe(gulp.dest('./public'));});//4.0以前的写法
//gulp.task('default', [
// 'minify-html', 'minify-css', 'minify-js'
//]);
//4.0以后的写法
// 执行 gulp 命令时执行的任务
gulp.task('default',gulp.parallel('minify-html','minify-css','minify-js',function(){// Do something after a, b, and c are finished.
}));
3. 使用
使用时按照以下顺序就可以了:
1
2
3
4
5
6
7
8
# 先清理文件hexo clean
# 编译生成静态文件hexo g
# gulp插件执行压缩任务gulp
# 开启服务hexo s
assert.js:85
throw new assert.AssertionError({ ^
AssertionError: Task function must be specified
at Gulp.set [as _setTask](/home/hope/web/node_modules/undertaker/lib/set-task.js:10:3) at Gulp.task (/home/hope/web/node_modules/undertaker/lib/task.js:13:8).................