imagemin
imagemin copied to clipboard
Add `preserveDirectories` option
Fixes #191
If the preserveDirectories
is set the output will match the directory structure from the input files.
Another possibility would be to introduce a breaking change and make this the default behaviour as that's what I would expect, but maybe i'm biased as I need this feature in a current project :smile:
Given: assets/images/1.jpg, assets/images/nested/2.svg, assets/images/nested/nested/3.webp
It will detect that assets/images
is the basePath and will not copy over these directories to the destination.
imagemin(['assets/images/**/*.{jpg,svg,webp}'], {
plugins: [imageminJpegtran(), imageminWebp(), imageminSvgo()],
destination: 'dest',
preserveDirectories: true
});
Our output would be dest/1.jpg, dest/nested/assets/2.svg, dest/nested/nested/3.webp
cc/ #293 #262 #225 #213 #196 #192
removed
@gijsroge Sweet, exactly what I was looking for! 🥳
However, I encountered a bug that would not preserve directory structure for the first subdirectory.
Script:
(async () => {
const files = await imagemin(['input_dir/**/*.{jpg,jpeg,png,svg}'], {
destination: 'output_dir',
preserveDirectories: true,
...
Files/ directories:
⨽ input_dir
⊢ sub_dir_1
∣ ⨽ files 1…N
⨽ sub_dir_2
⨽ files 1…N
All files in input_dir/sub_dir_1
would be written to output_dir/
whereas
files in input_dir/sub_dir_2
would be written to output_dir/sub_dir_2
.
Is this something you can reproduce?
I can't reproduce the skipping of the first directory as reported by @rolbr. For me it works great.
I can say that when I was on the master
branch of gijsroge/imagemin
, it did not function. It was necessary to check out the feature/preserve-directories
branch for this to work.
ty save my life <3 @gijsroge
What is the status with this PR? Can we have it merged soon?
@yellow1912 i'm picking this back up in the next few days.
Hey, just asking..What's the status of this PR?
Any news about PR?
This feature is essential 🎉
I use this command while waiting:
find ./static -type f | xargs -I % sh -c 'imagemin % -o $(dirname %)'
Thanks for imagemin :+1:
I've implemented something similar locally.
Concerning @sindresorhus's question on "Why does the first file path decide the base directory? This should be documented.", how about we only enable preserveDirectories
for glob inputs?
Then we rather than passing baseDirectory
, we just need to:
let destinationPath = destination ? path.join(destination, preserveDirectories? sourcePath: path.basename(sourcePath)) : undefined;
Any movement?
So yeah, days turned into weeks, weeks turned into months, months turned into years. Sorry about the delay :(
I addressed most of the issues + rebased with main, care to re-review? cc/ @1000ch @sindresorhus
I've implemented something similar locally.
Concerning @sindresorhus's question on "Why does the first file path decide the base directory? This should be documented.", how about we only enable
preserveDirectories
for glob inputs? Then we rather than passingbaseDirectory
, we just need to:let destinationPath = destination ? path.join(destination, preserveDirectories? sourcePath: path.basename(sourcePath)) : undefined;
There is no solid way to detect if someone is using a glob or not even if glob:true
they can still use ['image.jpg', 'assets/images/image.jpg']
. So the most obvious thing to do was make it configurable imo.
Tests are failing on Windows.
@gijsroge thank you for that very important feature! Hope it will be merged soon!
Very important feature! Hope it will be merged soon!
Any updates about it?
Very important feature! Hope it will be merged soon!!!
is this not being worked on anymore?
Any progress? This is really necessary.
@Shayan-To @eBaeza, @rtatarinov, etc
Please refrain from making such comments. There are plenty of people subscribed to threads and this kind of comment just spams their inbox. Please only comment if you have something to add to the discussion.
If this is so "necessary" feel free to contribute with valuable help. This might even include making a PR to the fork, helping the original author.
Thank you.
any update on this or a usable fork of imagemin since this seems like its no longer maintained (i mean the repo SAYS unmaintained) .... I am switching to this from using imagemin in gulp and i just ran across this issue.
also thank you to the community member(s?) working on a fix even if PRs aren't getting merged
Any progress on this?