利用gulp对Hexo博客压缩并一键之部署

说在前面

压缩页面无非就是减小页面大小加快博客访问时间

实现

安装依赖

1
npm i gulp gulp-htmlclean gulp-htmlmin gulp-minify-css gulp-uglify -s

gulp配置

在根目录下创建gulpfile.js文件

gulpfile.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var gulp = require('gulp');
var minifycss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var htmlclean = require('gulp-htmlclean');
// 压缩 public 目录 css
gulp.task('minify-css', function () {
return gulp.src('./public/**/*.css')
.pipe(minifycss())
.pipe(gulp.dest('./public'));
});
// 压缩 public 目录 html
gulp.task('minify-html', function () {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
}))
.pipe(gulp.dest('./public'))
});
// 压缩 public/js 目录 js
gulp.task('minify-js', function () {
return gulp.src('./public/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('./public'));
});
// 执行 gulp 命令时执行的任务
gulp.task('default', [
'minify-html', 'minify-css', 'minify-js'
]);

一键部署

package.json中加入如下script

1
2
3
"scripts": {
"push": "hexo cl && hexo g && gulp && hexo d"
}

然后在部署的时候只需要运行

1
npm run push

我就想试试,没别的意思!