forked from mzeht/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
66 lines (61 loc) · 2.29 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
'use strict';
var gulp = require('gulp');
var minifycss = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var htmlclean = require('gulp-htmlclean');
var imagemin = require('gulp-imagemin');
// 引入babel,万一用了ES6呢
var babel = require('gulp-babel');
// 压缩 public 目录 html
gulp.task('minify-html', async function() {
await gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true, //清除HTML注释
collapseWhitespace: true, //压缩HTML
collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input checked />
removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
removeStyleLinkTypeAttributes: true, //删除<style>和<link>的type="text/css"
minifyJS: true, //压缩页面JS
minifyCSS: true //压缩页面CSS
}))
.on('error', function(err) {
console.log('html Error!', err.message);
this.end();
})
.pipe(gulp.dest('./public'))
});
// 压缩 public 目录 css
gulp.task('minify-css', async function() {
await gulp.src('./public/**/*.css')
.pipe(minifycss())
.pipe(gulp.dest('./public'));
});
// 压缩js
gulp.task('minify-js', async function() {
await gulp.src(['./public/js/**/*.js', '!./public/js/**/*.{min,mini}.js'])
.pipe(babel())
.pipe(uglify())
.pipe(gulp.dest('./public/js'));
});
// 压缩图片
gulp.task('minify-images', async function() {
await gulp.src('./public/img/**/*.*')
.pipe(imagemin(
[imagemin.gifsicle({'optimizationLevel': 3}),
imagemin.mozjpeg({'progressive': true}),
imagemin.optipng({'optimizationLevel': 8}),
imagemin.svgo()],
{'verbose': true}))
.pipe(gulp.dest('./public/img'))
});
process.on('unhandledRejection', error => {
console.error('unhandledRejection', error);
process.exit(1) // To exit with a 'failure' code
});
// 默认任务
gulp.task('default', gulp.parallel('minify-html','minify-css','minify-js','minify-images', function(done){
done();
}));