-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
56 lines (55 loc) · 1.95 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
const gulp = require('gulp');
const path = require('path');
const cleanCSS = require('gulp-clean-css');
const uglify = require('gulp-uglify');
const htmlmin = require('gulp-htmlmin');
const htmlclean = require('gulp-htmlclean');
const imagemin = require('gulp-imagemin');
// 压缩 public 目录 css
gulp.task('clean-css', function() {
return gulp.src('./public/**/*.css')
.pipe(cleanCSS({
rebase: false
}))
.pipe(gulp.dest('./public'));
});
// 压缩 public 目录 html
gulp.task('minify-html', function() {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
}))
.pipe(gulp.dest('./public'))
});
// 压缩 public 目录 js
gulp.task('minify-js', function() {
return gulp.src('./public/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('./public'));
});
// 压缩图片任务
function compressImgsFolder(imgFolder='.') {
return function () {
return gulp.src(path.join(imgFolder, '/**/*.{png,jpg,gif,svg}'))
// imagemin Usage at https://github.com/sindresorhus/gulp-imagemin#user-content-options
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}), // gif 转为交错格式
imagemin.jpegtran({progressive: true}), // jpeg 转为渐进式
imagemin.optipng({optimizationLevel: 4}),
imagemin.svgo({floatPrecision: 1}) // https://github.com/svg/svgo/issues/171
], {verbose: false}
))
.pipe(gulp.dest(imgFolder))
}
}
gulp.task('minify-imgs', compressImgsFolder('./public/images/'));
gulp.task('minify-postImgs', compressImgsFolder('./public/wiki/'));
// 执行 gulp 命令时执行的任务
gulp.task('default', [
'minify-html', 'clean-css', 'minify-js',
'minify-imgs', 'minify-postImgs'
]);