gulp通过对站点使用的静态资源进行压缩,来优化网站的访问速度。
首先安装gulp以及所需要的模块:
npm install gulp -g
npm install gulp-htmlclean gulp-htmlmin gulp-minify-css gulp-uglify gulp-imagemin --save
然后在博客的根目录下创建gulpfile.js
文件写入代码:
var gulp = require('gulp'); var minifycss = require('gulp-minify-css'); var uglify = require('gulp-uglify'); var imagemin = require('gulp-imagemin'); // 压缩css gulp.task('minify-css', function() { return gulp.src('./public/**/*.css') .pipe(minifycss({ compatibility: 'ie8' })) .pipe(gulp.dest('./public')); }); // 压缩js gulp.task('minify-js', function() { return gulp.src('./public/js/**/*.js') .pipe(uglify()) .pipe(gulp.dest('./public')); }); // 压缩图片 gulp.task('minify-images', function() { return gulp.src('./public/images/**/*.*') .pipe(imagemin( [imagemin.gifsicle({'optimizationLevel': 3}), imagemin.jpegtran({'progressive': true}), imagemin.optipng({'optimizationLevel': 7}), imagemin.svgo()], {'verbose': true})) .pipe(gulp.dest('./public/images')) }); // 默认任务 gulp.task('default', gulp.parallel( 'minify-css','minify-js','minify-images' ));
最后执行命令,上传GitHub
hexo clean&&hexo g&&gulp&&hexo d