forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimproveImages.mjs
90 lines (71 loc) · 2.11 KB
/
improveImages.mjs
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
import fs from 'fs'
import find from 'find'
import filesize from 'filesize'
import imagemin from 'imagemin'
import imageminGifsicle from 'imagemin-gifsicle'
import imageminJpegtran from 'imagemin-jpegtran'
import imageminOptipng from 'imagemin-optipng'
import imageminSvgo from 'imagemin-svgo'
import parseFilepath from 'parse-filepath'
import chalk from 'chalk'
const plugins = [
imageminGifsicle({}),
imageminJpegtran({}),
imageminOptipng({}),
imageminSvgo({})
]
let savedSize = 0
const run = async () => {
const regex = new RegExp(/\.gif|\.jpeg|\.jpg|\.png$/)
const files = find.fileSync(regex, 'icons/');
for (const file of files) {
await optimized(file)
}
if (savedSize > 0) {
console.info(`\n🎉 You saved ${readableSize(savedSize)}.`)
} else {
console.info(`\n🎉 Nothing to optimize.`)
}
}
const size = (filename) => {
return fs.statSync(filename).size
}
const readableSize = (size) => {
return filesize(size, { round: 5 })
}
const optimized = async (filename) => {
let output = parseFilepath(filename).dir || './'
const fileSizeBefore = size(filename)
if (fileSizeBefore === 0){
console.info(chalk.blue(`Skipping ${filename}, it has ${readableSize(fileSizeBefore)}`))
return
}
const pluginsOptions = {
destination: output,
plugins
}
const filenameBackup = `${filename}.bak`
fs.copyFileSync(filename, filenameBackup)
try {
await imagemin([filename], pluginsOptions)
const fileSizeAfter = size(filename)
const fileSizeDiff = fileSizeBefore - fileSizeAfter
if (fileSizeDiff > 0){
savedSize += fileSizeDiff
console.info(chalk.green(`Optimized ${filename}: ${chalk.yellow(readableSize(fileSizeAfter))}`))
} else { // file after same or bigger
// restore previous file
fs.renameSync(filenameBackup, filename)
console.info(`${filename} ${chalk.red(`already optimized`)}`)
}
} catch (err) {
console.info(chalk.red(`Skip ${filename} due to error when optimizing`));
}
// delete backup file
if (fs.existsSync(filenameBackup)) {
fs.unlinkSync(filenameBackup)
}
}
(async () => {
await run();
})();