imagemin icon indicating copy to clipboard operation
imagemin copied to clipboard

Add `preserveDirectories` option

Open gijsroge opened this issue 4 years ago • 23 comments

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

gijsroge avatar Apr 29 '20 23:04 gijsroge

removed

gijsroge avatar Apr 29 '20 23:04 gijsroge

@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?

rolbr avatar Jun 17 '20 12:06 rolbr

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.

glassdimlygr avatar Jul 20 '20 18:07 glassdimlygr

ty save my life <3 @gijsroge

yuceltoluyag avatar Jul 21 '20 10:07 yuceltoluyag

What is the status with this PR? Can we have it merged soon?

yellow1912 avatar Sep 29 '20 06:09 yellow1912

@yellow1912 i'm picking this back up in the next few days.

gijsroge avatar Sep 29 '20 10:09 gijsroge

Hey, just asking..What's the status of this PR?

misaka00251 avatar Mar 07 '21 07:03 misaka00251

Any news about PR?

illycz avatar Mar 07 '21 21:03 illycz

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:

kosssi avatar Apr 29 '21 09:04 kosssi

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;

leesei avatar Jun 03 '21 04:06 leesei

Any movement?

price84 avatar Oct 27 '21 06:10 price84

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

gijsroge avatar Oct 27 '21 23:10 gijsroge

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;

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.

gijsroge avatar Oct 27 '21 23:10 gijsroge

Tests are failing on Windows.

sindresorhus avatar Nov 10 '21 08:11 sindresorhus

@gijsroge thank you for that very important feature! Hope it will be merged soon!

pumano avatar Nov 23 '21 15:11 pumano

Very important feature! Hope it will be merged soon!

eBaeza avatar Mar 25 '22 17:03 eBaeza

Any updates about it?

rtatarinov avatar Apr 02 '22 20:04 rtatarinov

Very important feature! Hope it will be merged soon!!!

okinawaa avatar Jul 28 '22 04:07 okinawaa

is this not being worked on anymore?

1ewiswarren avatar Nov 14 '22 14:11 1ewiswarren

Any progress? This is really necessary.

Shayan-To avatar Mar 07 '23 16:03 Shayan-To

@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.

tofran avatar Mar 07 '23 22:03 tofran

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

Xmetalfanx avatar May 05 '23 18:05 Xmetalfanx

Any progress on this?

saeidzebardast avatar Aug 03 '23 15:08 saeidzebardast