博客开启Gulp压缩 | 字数总计: 1.5k | 阅读时长: 7分钟 | 阅读量: |
警告!我用这个压缩几次后出现 bug 导致网站 CSS 样式丢失,慎重使用
Gulp 压缩 Gulp 是一款自动化构建的工具,拥有众多的插件。而我们只需要使用到几个插件来压缩 Html/css/js。
安装 1 npm install --global gulp-cli
压缩 HTML 可以使用 gulp-htmlclean 和 gulp-htmlmin 来压缩 HTML
1 2 npm install gulp-htmlclean --save-dev npm install --save gulp-htmlmin
压缩 CSS 可以使用 gulp-clean-css 来压缩 CSS
1 npm install gulp-clean-css --save-dev
压缩 JS 由于 Butterfly 主题中的 JS 使用到了部分 ES6 语法,因此不能只使用 gulp-uglify 来压缩,还需要搭配其它的插件。两种搭配都可以有效的压缩 JS 代码,选一种适合自己的就行。
terser 是 ES6+ 的 JavaScript 解析器
gulp-babel 是一个 JavaScript 转换编译器,可以把 ES6 转换成 ES5
1 2 npm install --save-dev gulp-uglify npm install terser
或者:
1 2 npm install --save-dev gulp-uglify npm install --save-dev gulp-babel @babel/core @babel/preset-env
两种选一种即可
压缩图片 使用 gulp-imagemin 来压缩图片
1 cnpm install --save-dev gulp-imagemin
警告:亲身体验,用 npm
安装此插件会报错无法构建,但我用 cnpm
安装成功了!!!
创建 gulpfile 文件 在 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 79 80 var gulp = require ('gulp' )var cleanCSS = require ('gulp-clean-css' )var htmlmin = require ('gulp-htmlmin' )var htmlclean = require ('gulp-htmlclean' )var imagemin = require ('gulp-imagemin' )var uglify = require ('gulp-uglify' )var babel = require ('gulp-babel' )gulp.task ('compress' , () => gulp.src (['./public/**/*.js' , '!./public/**/*.min.js' ]) .pipe (babel ({ presets : ['@babel/preset-env' ] })) .pipe (uglify ().on ('error' , function (e ) { console .log (e) })) .pipe (gulp.dest ('./public' )) ) gulp.task ('minify-css' , () => { return gulp.src ('./public/**/*.css' ) .pipe (cleanCSS ()) .pipe (gulp.dest ('./public' )) }) gulp.task ('minify-html' , () => { return gulp.src ('./public/**/*.html' ) .pipe (htmlclean ()) .pipe (htmlmin ({ removeComments : true , collapseWhitespace : true , collapseBooleanAttributes : true , removeEmptyAttributes : true , removeScriptTypeAttributes : true , removeStyleLinkTypeAttributes : true , minifyJS : true , minifyCSS : true , minifyURLs : true })) .pipe (gulp.dest ('./public' )) }) gulp.task ('minify-images' , async () => { gulp.src ('./public/img/**/*.*' ) .pipe (imagemin ({ optimizationLevel : 5 , progressive : true , interlaced : false , multipass : false })) .pipe (gulp.dest ('./public/img' )) }) gulp.task ('default' , gulp.parallel ( 'compress' , 'minify-css' , 'minify-html' , 'minify-images' ))
注意: 如果有使用到 Butterfly 主题提供的 mermaid
标签外挂,那需要把 52 行 .pipe(htmlclean())
注释掉,同时,把 55 行的 collapseWhitespace: true
, 改为 false
运行 在执行 hexo g
之后执行 gulp
即可
Gulp + PWA 如果你开启了 PWA ,那么可以使用下面的 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 var gulp = require ('gulp' );var cleanCSS = require ('gulp-clean-css' );var htmlmin = require ('gulp-htmlmin' );var htmlclean = require ('gulp-htmlclean' );var imagemin = require ('gulp-imagemin' );var workbox = require ("workbox-build" );var uglify = require ('gulp-uglify' );var babel = require ('gulp-babel' );gulp.task ('generate-service-worker' , () => { return workbox.injectManifest ({ swSrc : './sw-template.js' , swDest : './public/sw.js' , globDirectory : './public' , globPatterns : [ "**/*.{html,css,js,json,woff2}" ], modifyURLPrefix : { "" : "./" } }); }); gulp.task ('compress' , () => gulp.src (['./public/**/*.js' , '!./public/**/*.min.js' ]) .pipe (babel ({ presets : ['@babel/preset-env' ] })) .pipe (uglify ().on ('error' , function (e ){ console .log (e); })) .pipe (gulp.dest ('./public' )) ); gulp.task ('minify-css' , () => { return gulp.src ('./public/**/*.css' ) .pipe (cleanCSS ({ compatibility : 'ie11' })) .pipe (gulp.dest ('./public' )); }); gulp.task ('minify-html' , () => { return gulp.src ('./public/**/*.html' ) .pipe (htmlclean ()) .pipe (htmlmin ({ removeComments : true , collapseWhitespace : true , collapseBooleanAttributes : true , removeEmptyAttributes : true , removeScriptTypeAttributes : true , removeStyleLinkTypeAttributes : true , minifyJS : true , minifyCSS : true , minifyURLs : true })) .pipe (gulp.dest ('./public' )) }); gulp.task ('minify-images' , async () => { gulp.src ('./public/img/**/*.*' ) .pipe (imagemin ({ optimizationLevel : 5 , progressive : true , interlaced : false , multipass : false , })) .pipe (gulp.dest ('./public/img' )); }); gulp.task ("default" , gulp.series ("generate-service-worker" , gulp.parallel ( 'compress' ,'minify-html' , 'minify-css' , 'minify-images' )));
作者: Jerry 連結: https://demo.jerryc.me/posts/4073eda/#%E5%8F%83%E8%80%83 來源: Butterfly 著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。