hexo优化-利用gulp压缩代码

因为想偷懒一键实现 hexo clean -> hexo g -> gulp压缩html、css、js、图片 -> 部署 所以这里用gulp偷个懒

写个大概吧。。空了再详细写

npm install gulp@next -g # gulp4.0 因为gulp-debug需要gulp版本大于等于4.0

npm install –save-dev gulp-debug

npm install –save-dev gulp-clean-css

npm install –save-dev gulp-uglify

npm install –save-dev gulp-htmlmin

npm install –save-dev gulp-htmlclean

npm install –save-dev gulp-imagemin

npm install –save-dev gulp-changed

npm install –save-dev gulp-if

npm install –save-dev gulp-plumber

npm install –save-dev run-sequence

npm install –save-dev del

附上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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var gulp        = require('gulp');
var debug = require('gulp-debug');
var cleancss = require('gulp-clean-css'); //css压缩组件
var uglify = require('gulp-uglify'); //js压缩组件
var htmlmin = require('gulp-htmlmin'); //html压缩组件
var htmlclean = require('gulp-htmlclean'); //html清理组件
var imagemin = require('gulp-imagemin'); //图片压缩组件
var changed = require('gulp-changed'); //文件更改校验组件
var gulpif = require('gulp-if') //任务 帮助调用组件
var plumber = require('gulp-plumber'); //容错组件(发生错误不跳出任务,并报出错误内容)
var runSequence = require('run-sequence'); //异步执行组件
var isScriptAll = true; //是否处理所有文件,(true|处理所有文件)(false|只处理有更改的文件)
var isDebug = true; //是否调试显示 编译通过的文件
var del = require('del');
var Hexo = require('hexo');
var hexo = new Hexo(process.cwd(), {}); // 初始化一个hexo对象

// 清除public文件夹
gulp.task('clean', function() {
return del(['public/**/*']);
});

// 下面几个跟hexo有关的操作,主要通过hexo.call()去执行,注意return

// 创建静态页面 (等同 hexo generate)
gulp.task('generate', function() {
return hexo.init().then(function() {
return hexo.call('generate', {
watch: false
}).then(function() {
return hexo.exit();
}).catch(function(err) {
return hexo.exit(err);
});
});
});

// 启动Hexo服务器
gulp.task('server', function() {
return hexo.init().then(function() {
return hexo.call('server', {});
}).catch(function(err) {
console.log(err);
});
});

// 部署到服务器
gulp.task('deploy', function() {
return hexo.init().then(function() {
return hexo.call('deploy', {
watch: false
}).then(function() {
return hexo.exit();
}).catch(function(err) {
return hexo.exit(err);
});
});
});

// 压缩public目录下的js文件
gulp.task('compressJs', function () {
var option = {
// preserveComments: 'all',//保留所有注释
mangle: true, //类型:Boolean 默认:true 是否修改变量名
compress: true //类型:Boolean 默认:true 是否完全压缩
}
return gulp.src(['./public/**/*.js','!./public/**/*.min.js']) //排除的js
.pipe(gulpif(!isScriptAll, changed('./public')))
.pipe(gulpif(isDebug,debug({title: 'Compress JS:'})))
.pipe(plumber())
.pipe(uglify(option)) //调用压缩组件方法uglify(),对合并的文件进行压缩
.pipe(gulp.dest('./public')); //输出到目标目录
});

// 压缩public目录下的css文件
gulp.task('compressCss', function () {
var option = {
rebase: false,
//advanced: true, //类型:Boolean 默认:true [是否开启高级优化(合并选择器等)]
compatibility: 'ie7', //保留ie7及以下兼容写法 类型:String 默认:''or'*' [启用兼容模式; 'ie7':IE7兼容模式,'ie8':IE8兼容模式,'*':IE9+兼容模式]
//keepBreaks: true, //类型:Boolean 默认:false [是否保留换行]
//keepSpecialComments: '*' //保留所有特殊前缀 当你用autoprefixer生成的浏览器前缀,如果不加这个参数,有可能将会删除你的部分前缀
}
return gulp.src(['./public/**/*.css','!./public/**/*.min.css']) //排除的css
.pipe(gulpif(!isScriptAll, changed('./public')))
.pipe(gulpif(isDebug,debug({title: 'Compress CSS:'})))
.pipe(plumber())
.pipe(cleancss(option))
.pipe(gulp.dest('./public'));
});

// 压缩public目录下的html文件
gulp.task('compressHtml', function () {
var cleanOptions = {
protect: /<\!--%fooTemplate\b.*?%-->/g, //忽略处理
unprotect: /<script [^>]*\btype="text\/x-handlebars-template"[\s\S]+?<\/script>/ig //特殊处理
}
var minOption = {
collapseWhitespace: true, //压缩HTML
collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input />
removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
removeComments: true, //清除HTML注释
minifyJS: true, //压缩页面JS
minifyCSS: true, //压缩页面CSS
minifyURLs: true //替换页面URL
};
return gulp.src('./public/**/*.html')
.pipe(gulpif(isDebug,debug({title: 'Compress HTML:'})))
.pipe(plumber())
.pipe(htmlclean(cleanOptions))
.pipe(htmlmin(minOption))
.pipe(gulp.dest('./public'));
});

// 压缩 public/uploads 目录内图片
gulp.task('compressImage', function() {
var option = {
optimizationLevel: 5, //类型:Number 默认:3 取值范围:0-7(优化等级)
progressive: true, //类型:Boolean 默认:false 无损压缩jpg图片
interlaced: false, //类型:Boolean 默认:false 隔行扫描gif进行渲染
multipass: false //类型:Boolean 默认:false 多次优化svg直到完全优化
}
return gulp.src('./public/uploads/**/*.*')
.pipe(gulpif(!isScriptAll, changed('./public/uploads')))
.pipe(gulpif(isDebug,debug({title: 'Compress Images:'})))
.pipe(plumber())
.pipe(imagemin(option))
.pipe(gulp.dest('./public/uploads'));
});

// 用run-sequence并发执行,同时处理html,css,js,img
gulp.task('compress', function(cb) {
runSequence.options.ignoreUndefinedTasks = true;
runSequence(['compressHtml', 'compressCss', 'compressJs'],cb);
});

// 执行顺序: 清除public目录 -> 产生原始博客内容 -> 执行压缩混淆 -> 部署到服务器
gulp.task('build', function(cb) {
runSequence.options.ignoreUndefinedTasks = true;
runSequence('clean', 'generate', 'compress', 'deploy', cb);
});

// 默认任务
gulp.task('default',
gulp.series('clean','generate',
gulp.parallel('compressHtml','compressCss','compressImage')
)
);
//Gulp4最大的一个改变就是gulp.task函数现在只支持两个参数,分别是任务名和运行任务的函数

这里我只启用了html css 图片 的压缩,需要同时做其他任务的自行在最后添加,任务已经写好,自己看下名字

通过示例迁移到gulp 4
【译】相对完整的Gulp4升级指南
走进gulp4的世界

1
2
gulp.series:将按顺序运行任务
gulp.parallel:将并行运行任务
大爷,赏点?
0%