-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.mjs
More file actions
50 lines (45 loc) · 1.25 KB
/
Copy pathgulpfile.mjs
File metadata and controls
50 lines (45 loc) · 1.25 KB
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
import gulp from 'gulp'
const { series, parallel, src, dest } = gulp
import imagemin from 'gulp-imagemin' // 图片压缩
import pngquant from 'imagemin-pngquant' // 深度压缩
import gifsicle from 'imagemin-gifsicle'
import mozjpeg from 'imagemin-mozjpeg'
import svgo from 'imagemin-svgo'
import cache from 'gulp-cache'
import gulpif from 'gulp-if'
import minimist from 'minimist'
const argv = minimist(process.argv.slice(2)) // 获取命令行输入参数 eg: --env=dev
const isProduction = argv.env === 'production' || process.env.NODE_ENV === 'production'
// 帮助信息
export function help(cb) {
console.log('[imgCompress] #压缩图片')
}
// 压缩图片
export function imgCompress(cb) {
let imageOption = imagemin(
[
gifsicle({ interlaced: true }),
mozjpeg({ quality: 60 }),
pngquant({ quality: [0.3, 0.5] }),
svgo({
plugins: [{ removeViewBox: true }, { cleanupIDs: false }],
}),
],
{
verbose: true,
},
)
// 需要单独引入优化器
src('src/**/*.{png,jpg,jpeg,gif,svg,webp}')
.pipe(gulpif(!isProduction, cache(imageOption), imageOption))
.pipe(dest('dist'))
cb()
}
export function clear(cb) {
cache.clearAll()
cb()
}
export default function defaultTask(cb) {
help()
cb()
}