Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Toolchain
sgc
Commits
565f305d
Commit
565f305d
authored
Aug 29, 2018
by
Philipp Nowinski
Browse files
[FEATURE] complete images task
parent
cc84b82d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
3 deletions
+33
-3
core/tasks/images.js
core/tasks/images.js
+33
-3
No files found.
core/tasks/images.js
View file @
565f305d
...
...
@@ -17,10 +17,40 @@ module.exports = class Watch extends Task {
/**
* Runner function
*/
async
run
()
{
async
run
(
_subTask
)
{
if
(
_subTask
===
'
uploaded
'
)
{
this
.
_optimizeUploadedImages
();
}
else
{
this
.
_optimizeExtensionImages
();
}
}
/**
* Optimize all user uploaded images
*/
async
_optimizeUploadedImages
()
{
this
.
_config
.
images
.
optimize
.
forEach
(
async
imagesPath
=>
{
const
files
=
await
globby
([
`
${
imagesPath
}
/**/*.{png,jpg,gif,svg}`
]);
this
.
_optimize
(
files
,
imagesPath
);
});
}
/**
* Optimize images on extension level
*/
async
_optimizeExtensionImages
()
{
const
imagesPath
=
this
.
_getFullPath
(
this
.
_config
.
directories
.
images
);
const
files
=
await
globby
([
`
${
imagesPath
}
/**/*.{png,jpg,gif,svg}`
]);
files
.
forEach
(
async
_image
=>
{
this
.
_optimize
(
files
,
imagesPath
);
}
/**
* Optimize a set of given images
*
* @param {Array} _files An array of filenames
*/
async
_optimize
(
_files
,
_imagesPath
)
{
_files
.
forEach
(
async
_image
=>
{
let
buffer
=
fs
.
readFileSync
(
_image
);
let
result
=
await
imagemin
.
buffer
(
buffer
,
{
plugins
:
[
...
...
@@ -37,7 +67,7 @@ module.exports = class Watch extends Task {
let
saved
=
buffer
.
length
-
result
.
length
;
info
=
`(saved
${
prettyBytes
(
saved
)}
~
${
Math
.
round
((
saved
/
buffer
.
length
)
*
100
)}
%)`
;
}
let
message
=
`
${
chalk
.
white
(
_image
.
replace
(
`
${
imagesPath
}
/`
,
''
))}
${
chalk
.
magenta
(
`
${
info
}
`
)}
`
;
let
message
=
`
${
chalk
.
white
(
_image
.
replace
(
`
${
_
imagesPath
}
/`
,
''
))}
${
chalk
.
magenta
(
`
${
info
}
`
)}
`
;
fs
.
writeFileSync
(
_image
,
result
);
this
.
_logger
.
success
(
message
);
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment