-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
70 lines (58 loc) · 1.54 KB
/
test.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
#!/usr/bin/env node
const imagemin = require('imagemin');
const imageminJpegtran = require('imagemin-jpegtran');
const imageminPngquant = require('imagemin-pngquant');
const fs = require('fs')
const path = require('path')
const exclude = ['node_modeles']
const cmdMap = {
overwrite: '-o', // default
build: '-b'
}
const type = process.argv[2]
const compressDir = process.argv[3]
let startPath = process.cwd()
if (compressDir) {
startPath = path.join(startPath, compressDir)
}
let outputPath
if (type === cmdMap.build) {
outputPath = path.join(path.resolve(startPath, '..'), 'build')
} else {
outputPath = startPath
}
class CompressImg {
constructor (startPath) {
this.status = 0
this.finder(startPath, outputPath)
}
finder(startPath, outputPath) {
const files = fs.readdirSync(startPath)
this.compress(startPath.replace(/\\/g, '/'), outputPath)
files.forEach((val, index) => {
const fPath = path.join(startPath, val)
const stats = fs.statSync(fPath)
if (stats.isDirectory() && !exclude.includes(val)) {
this.finder(fPath.replace(/\\/g, '/'), path.join(outputPath, val))
}
})
}
async compress (startPath, outputPath) {
const files = await imagemin([`${startPath}/*.{jpg,jpeg,png}`], {
destination: `${outputPath}`,
plugins: [
imageminJpegtran({
quality: [0.6, 0.8]
}),
imageminPngquant({
quality: [0.6, 0.8]
})
]
})
files && files.forEach(file => {
fs.writeFileSync(file.destinationPath, file.data)
console.log(`small ${file.destinationPath}`)
})
}
}
new CompressImg(startPath)