转载 rozbo开发的 Hexo-Neat 压缩插件
Hexo-Neat 压缩静态资源 Hexo-Neat 能够帮助用户自动压缩静态资源,配合各类下属插件,能够压缩包括css、js、html乃至各类格式的图片文件
安装Gulp插件 安装Hexo-Neat插件:在博客根目录[Blog]打开终端,输入:
在站点配置文件中末尾添加以下相关配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 neat_enable: true neat_html: enable: true exclude: neat_css: enable: true exclude: - '*/*.min.css' neat_js: enable: true mangle: true output: compress: exclude: - '**/*.min.js' - '**/jquery.fancybox.pack.js' - '**/index.js'
Gulp压缩 安装插件
1 2 npm install gulp -g npm install gulp-minify-css gulp-uglify gulp-htmlmin gulp-htmlclean gulp --save
创建任务脚本 为Gulp创建gulpfile.js任务脚本。在博客根目录[Blogroot]下新建gulpfile.js,打开[Blogroot]\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 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' );var imagemin = require ('gulp-imagemin' );gulp.task ('minify-html' , function ( ) { return gulp.src ('./public/**/*.html' ) .pipe (htmlclean ()) .pipe (htmlmin ({ collapseWhitespace : true , collapseBooleanAttributes : true , removeComments : true , removeEmptyAttributes : true , removeScriptTypeAttributes : true , removeStyleLinkTypeAttributes : true , minifyJS : true , minifyCSS : true , minifyURLs : true , })) .pipe (gulp.dest ('./public' )); }); gulp.task ('minify-css' , function ( ) { return gulp.src ('./public/**/*.css' ) .pipe (minifycss ({ compatibility : 'ie8' })) .pipe (gulp.dest ('./public' )); }); 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.series (gulp.parallel ('minify-html' ,'minify-css' ,'minify-js' ,'minify-images' )));
在每次运行完hexo generate生成静态页面后,运行gulp对其进行压缩。指令流程如下:
1 2 3 4 hexo clean hexo generate gulp hexo server 或 hexo deploy