-
Notifications
You must be signed in to change notification settings - Fork 49.1k
[DevTools] Optimize Images yarn command (part 2) #21968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
628364f
automate improve images
ilhamsyahids 7e872fd
backup and restore file when automated improve images
ilhamsyahids 98c2620
add missing dependencies
ilhamsyahids e774555
add error handling when optimizing images
ilhamsyahids 9594f54
fix error handling
ilhamsyahids 1bfc71b
rename file
ilhamsyahids 6cb2f22
commit yarn lock
ilhamsyahids 33208a0
revert yarn lock
ilhamsyahids 7b76d4b
update lock file
ilhamsyahids 14d053b
Merge branch 'main' into main
ilhamsyahids 62f67c6
move script file
ilhamsyahids 969a244
only icons folder to be optimized
ilhamsyahids 92bb404
move file and dependencies
ilhamsyahids 812fcfc
fix dependencies
ilhamsyahids File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,92 @@ | ||
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, '.') | ||
|
||
const filteredFiles = files.filter(file => !file.match(/node_modules\//)) | ||
|
||
for (const file of filteredFiles) { | ||
await optimized(file) | ||
} | ||
ilhamsyahids marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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(); | ||
})(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.