Hexo生成的静态站点默认没有做太多资源压缩优化,为了进一步优化访问速度,可以在Hexo生成public目录后,引入Gulp进行二次压缩处理
安装依赖
在Hexo项目根目录执行
1
| npm install gulp gulp-htmlmin gulp-clean-css gulp-uglify gulp-htmlclean
|
创建gulpfile.js
在Hexo根目录新建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 34 35 36 37 38 39
| const gulp = require('gulp'); const htmlmin = require('gulp-htmlmin'); const cleanCSS = require('gulp-clean-css'); const uglify = require('gulp-uglify'); const htmlclean = require('gulp-htmlclean');
gulp.task('minify-html', () => { return gulp.src('./public/**/*.html') .pipe(htmlclean()) .pipe(htmlmin({ collapseWhitespace: true, removeComments: true, minifyJS: true, minifyCSS: true })) .pipe(gulp.dest('./public')); });
gulp.task('minify-css', () => { return gulp.src('./public/**/*.css') .pipe(cleanCSS()) .pipe(gulp.dest('./public')); });
gulp.task('minify-js', () => { return gulp.src('./public/**/*.js') .pipe(uglify()) .pipe(gulp.dest('./public')); });
gulp.task('default', gulp.series( 'minify-html', 'minify-css', 'minify-js' ));
|
构建流程
手动执行
1
| hexo clean && hexo generate && gulp
|
写入package.json
1 2 3 4 5 6
| "scripts": { "build": "hexo generate && gulp", "clean": "hexo clean", "deploy": "hexo deploy", "server": "hexo server" }
|
任选其中一个你喜欢的方式进行使用即可