-
Notifications
You must be signed in to change notification settings - Fork 10
/
gulpfile.js
108 lines (96 loc) · 3.67 KB
/
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
(function() {
'use strict';
var gulp = require('gulp'),
less = require('gulp-less'),
rename = require('gulp-rename'),
minifycss = require('gulp-minify-css'),
autoprefixer = require('gulp-autoprefixer'),
uglify = require('gulp-uglify'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish'),
notify = require('gulp-notify'),
plumber = require('gulp-plumber'),
htmlclean = require('gulp-htmlclean'),
htmlmin = require('gulp-htmlmin'),
rev = require('gulp-rev-append'),
sequence = require('gulp-sequence'),
path = require('path'),
paths = {
root: './',
source: './themes/hexo-theme-k8blog/source/' //主题下原文件
}
/*====================================================
开发主题
====================================================*/
// CSS预处理
gulp.task('less-task', function() {
return gulp.src(paths.source + 'css/less/_style.less')
.pipe(plumber({
errorHandler: notify.onError('Error: <%= error.message %>')
}))
.pipe(less())
.pipe(rename({ basename: "style" }))
.pipe(gulp.dest(paths.source + 'css'))
.pipe(notify({ message: 'less compile complete' }));
});
// 校验JS语法和风格
gulp.task('js-task', function() {
return gulp.src(paths.source + 'js/*.js')
.pipe(jshint())
.pipe(jshint.reporter(stylish))
.pipe(gulp.dest(paths.source + 'js/'))
.pipe(notify({ message: 'js compile complete' }));
});
// 监听任务-主题开发模式
gulp.task('dev', function() {
gulp.watch(paths.source + 'css/less/*.less', ['less-task']);
gulp.watch(paths.source + 'js/*.js', ['js-task']);
});
/*====================================================
部署前代码处理
====================================================*/
// 压缩处理 css
gulp.task('minify-css', function() {
return gulp.src('./public/**/*.css')
.pipe(autoprefixer({
browsers: ['last 10 versions', 'Firefox >= 20', 'Opera >= 36', 'ie >= 9', 'Android >= 4.0', ],
cascade: true, //是否美化格式
remove: false //是否删除不必要的前缀
}))
.pipe(minifycss())
.pipe(gulp.dest('./public'))
.pipe(notify({ message: 'css minify complete' }));
});
// 压缩处理 js
gulp.task('minify-js', function() {
return gulp.src('./public/js/*.js')
.pipe(uglify())
.pipe(gulp.dest('./public/js'))
.pipe(notify({ message: 'js minify complete' }));
});
// 压缩处理 html
gulp.task('minify-html', function() {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true, //清除HTML注释
collapseWhitespace: true, //压缩HTML
minifyJS: true, //压缩页面JS
minifyCSS: true, //压缩页面CSS
minifyURLs: true
}))
.pipe(gulp.dest('./public'));
});
// 添加版本号
gulp.task('rev', function() {
return gulp.src('./public/**/*.html')
.pipe(rev())
.pipe(gulp.dest('./public'));
});
// 同步执行task
gulp.task('deploy', sequence(['minify-css', 'minify-js'], 'rev', 'minify-html'));
// 部署前代码处理
gulp.task('default', ['deploy'], function(e) {
console.log("[complete] please execute: hexo d");
})
})();