-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DevTools] Optimize Images yarn command (part 2) (#21968)
- Loading branch information
1 parent
27bf6f9
commit dfd9d62
Showing
3 changed files
with
1,460 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.