本文仅用来记录Hexo博客,使用glup的过程,用来压缩静态资源,提高博客加载速度。
如果想深入学习,请访问glup官网
1、安装glup 1 npm install --global gulp-cli
2、安装所需模块 1 2 3 4 npm install gulp --save npm install gulp-htmlclean gulp-htmlmin gulp-clean-css gulp-uglify gulp-imagemin --save npm install gulp-babel babel-preset-env babel-preset-mobx --save npm install -D @babel/core @babel/preset-react @babel/preset-env --save
3、配置hexo 在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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 let gulp = require ('gulp' )let cleanCSS = require ('gulp-clean-css' )let htmlmin = require ('gulp-htmlmin' )let htmlclean = require ('gulp-htmlclean' )let babel = require ('gulp-babel' ) let uglify = require ('gulp-uglify' )let imagemin = require ('gulp-imagemin' )const root = './public' const pattern = '**/*' gulp.task('minify-html' , function ( ) { return gulp .src(`${root} /${pattern} .html` ) .pipe(htmlclean()) .pipe( htmlmin({ removeComments: true , minifyJS: true , minifyCSS: true , minifyURLs: true }) ) .pipe(gulp.dest('./public' )) }) gulp.task('minify-css' , function ( ) { return gulp .src(`${root} /${pattern} .css` ) .pipe( cleanCSS({ compatibility: 'ie8' }) ) .pipe(gulp.dest('./public' )) }) gulp.task('minify-js' , function ( ) { return gulp .src(`${root} /${pattern} .js` ) .pipe( babel({ presets: ['env' ] }) ) .pipe(uglify()) .pipe(gulp.dest('./public' )) }) gulp.task('minify-images' , function ( ) { return gulp .src(`${root} /images/${pattern} ` ) .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.series('minify-html' , 'minify-css' , 'minify-js' ))
安装完成,执行glup
来进行压缩。
4、命令精简 hexo一般会用到hexo cl hexo g hexo s hexo d
使用命令精简,一句命令实现本地预览或发布
在hexo根目录中找到package.json
将其中的scripts
节点中build
和deploy
进行修改
1 2 3 4 "scripts": { "build": "hexo clean && hexo g && gulp", "deploy": "hexo clean && hexo g && gulp && hexo d" }
修改后,想要本地预览就执行npm run build
想要发布就执行npm run deploy
以上就是glup的安装与使用,当然,提高站点加载速度的方法有很多,这只是其中一种。用多种方式了组合使用,效果更佳。