gulp压缩

gulp压缩

三月 23, 2021

下载插件

    下载gulp-cli

1
npm install gulp-cli -g --save

    接着,确认一下版本:

1
2
3
$ gulp -v
CLI version: 2.3.0
Local version: 4.0.2

算了,你什么也没看见

压缩 html

    任选其一:

1
2
npm install gulp-htmlmin --save
npm install gulp-htmlclean --save

    gulp-htmlmin的配置:

1
2
3
4
5
6
7
8
9
10
11
.pipe(htmlmin({
removeComments: true, // 清除 HTML 註釋
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"
minifyJS: true, // 壓縮頁面 JS
minifyCSS: true, // 壓縮頁面 CSS
minifyURLs: true
}))

    gulp-htmlclean的配置:

1
2
3
4
.pipe(htmlclean({
protect: /<\!--%fooTemplate\b.*?%-->/g,
edit: function(html) { return html.replace(/\begg(s?)\b/ig, 'omelet$1'); }
}))

压缩 css

    任选其一:

1
npm install gulp-clean-css --save

    gulp-clean-css的配置:

1
2
3
4
5
gulp.task('minify-css', () => {
return gulp.src('./public/**/*.css')
.pipe(cleanCSS())
.pipe(gulp.dest('./public'))
})

压缩 js

    任选其一:

重要:gulp-babelgulp-uglify搭配食用!

1
2
npm install gulp-tester --save
npm install gulp-babel --save && npm install gulp-uglify --save

    gulp-tester的配置:

不好意思,terser配置没写,,哈哈哈哈。

1
.pipe(terser())

    gulp-babel+gulp-uglify的配置:

1
2
.pipe(babel({presets: ['@babel/preset-env']}))
.pipe(uglify().on('error', function (e) {console.log(e)}))

压缩 image

1
npm install gulp-imagemin --save

    gulp-imagemin的配置:

1
2
3
4
5
6
.pipe(imagemin({
optimizationLevel: 5, // 類型:Number 預設:3 取值範圍:0-7(優化等級)
progressive: true, 7// 類型:Boolean 預設:false 無失真壓縮jpg圖片
interlaced: false, // 類型:Boolean 預設:false 隔行掃描gif進行渲染
multipass: false // 類型:Boolean 預設:false 多次優化svg直到完全優化
}))

gulpflie

    在你的博客根目录创建一个gulpfile.js的文件,格式如下:
    并且根据你自己的插件安装情况,来进行配置:
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
/* Gulp 压缩 */
// gulp-cli

const gulp = require('gulp')

/* html */
/* 如果是使用 htmlclean,请注释掉下面一行 */
const htmlmin = require('gulp-html-minifier-terser')
/* 同理 */
//const htmlclean = require('gulp-htmlclean')

/* css */
const cleanCSS = require('gulp-clean-css')

/* js */
/* gulp-tester (如果使用 gulp-terser,把下面的//去掉) */
//const terser = require('gulp-terser');
/* babel (如果使用babel,把下面兩行註釋去掉) */
/* 重要:gulp-uglify + gulp-babel */
const uglify = require('gulp-uglify')
const babel = require('gulp-babel')

/* image */
const imagemin = require('gulp-imagemin')

/* 压缩 */
// ---------------

// 壓縮 html
gulp.task('minify-html', () => {
return gulp.src('./public/**/*.html', './public/**/**/*.html', './public/*.html')
// 如果使用的是 htmlclean,则保留如下。
/*.pipe(htmlclean({
protect: /<\!--%fooTemplate\b.*?%-->/g,
edit: function(html) { return html.replace(/\begg(s?)\b/ig, 'omelet$1'); }
}))*/
// 如果使用的是 htmlmin,则保留如下。
.pipe(htmlmin({
removeComments: true, // 清除 HTML 註釋
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"
minifyJS: true, // 壓縮頁面 JS
minifyCSS: true, // 壓縮頁面 CSS
minifyURLs: true
}))
.pipe(gulp.dest('./public'))
})

/* 压缩 css */
gulp.task('minify-css', () => {
return gulp.src('./public/**/*.css', './public/mysource/**/*.css')
.pipe(cleanCSS())
.pipe(gulp.dest('./public'))
})

/* 压缩 js */
// minify js - gulp-tester (如果使用 gulp-terser,把注释//去掉)
/* 重要:gulp-uglify + gulp-babel */
//gulp.task('compress', () =>
// gulp.src(['./public/**/*.js', '!./public/**/*.min.js'])
// .pipe(terser())
// .pipe(gulp.dest('./public'))
//)
// minify js - babel( 如果不是使用babel,把下面註釋掉)
gulp.task('compress', () =>
gulp.src(['./public/**/*.js', '!./public/**/*.min.js', './public/mysource/**/*.js', './public/*.js'])
.pipe(babel({
presets: ['@babel/preset-env']
}))
.pipe(uglify().on('error', function (e) {
console.log(e)
}))
.pipe(gulp.dest('./public'))
)

/* 压缩 image */
// 壓縮 public/uploads 目錄內圖片
gulp.task('minify-images', async () => {
gulp.src('./public/img/**/*.*', './public/mysource/image/*.*')
.pipe(imagemin({
optimizationLevel: 5, // 類型:Number 預設:3 取值範圍:0-7(優化等級)
progressive: true, 7// 類型:Boolean 預設:false 無失真壓縮jpg圖片
interlaced: false, // 類型:Boolean 預設:false 隔行掃描gif進行渲染
multipass: false // 類型:Boolean 預設:false 多次優化svg直到完全優化
}))
.pipe(gulp.dest('./public/img'))
})

// 執行 gulp 命令時執行的任務
gulp.task('default', gulp.parallel(
'compress', 'minify-css', 'minify-html', 'minify-images'
))

部署

    既然配置都设置好了!接下来就是真材实料的压缩了!
    在hexo g之后,hexo d之前,加一句命令行gulp即可。

1
hexo cl && hexo g && gulp && hexo d