BirjuVachhani / spider

A small dart library to generate Assets dart code from assets folder.
https://spider.birju.dev/
Apache License 2.0
190 stars 19 forks source link

[feature_request] A way to find all unused assets #69

Open kaciula opened 1 year ago

kaciula commented 1 year ago

I wish there was a way to find out if an asset is not being used anywhere in the app. If it's not being used, it makes sense for me to manually delete it and shrink the app's size.

Is this possible to be implemented? I am imagining we should somehow rely on the compiler warning that a particular generated field for an asset is not being used anywhere inside the app. What do you think?

BirjuVachhani commented 1 year ago

Hey, @kaciula

Thank you for opening this issue.

This sounds like a pretty useful thing. However, this is quite complicated. We can't rely on compiler warnings but we will have to run dart analyzer on the whole project and then process the result of it and look for issues in generated files to determine which assets are not being used.

I wish there was a way to find out if an asset is not being used anywhere in the app.

The analyzer shows this if you open the generated files. [If implemented] we can also show this in the terminal when build is complete (or could have separate command as well) but that's it, unless, we delete those files and references (which seems handy but I am not sure).

Despite all the complications, this sounds like a fun project and seems almost feasible. However, I don't have bandwidth to do this in near future. If someone is willing to implement then I am more than happy to guide and review it. If I get some time and by then if no one has done it then I may pick it up and do it. Or maybe not. I can't promise anything solid yet.

For that, I am keeping this issue open.

Thanks.

kaciula commented 1 year ago

@BirjuVachhani Thanks for the response.

The analyzer shows this if you open the generated files.

I don't see any warnings for unused assets when I open the generated file. Please see the attachment. The playing.png asset is not being used anywhere and I do not see any warning. Am I missing something? Should it show?

Also, you don't have to manually run the analyzer. It runs automatically on the whole project. The idea however is that the analyzer does not show unused variable warning for public variables anywhere in the project. It only shows for unused private variables. So I don't know how this limitation can be overcome.

Screenshot 2022-11-18 at 14 04 16
BirjuVachhani commented 1 year ago

Ah! You're right! I never noticed it. I looked up this on internet and found these threads:

https://github.com/dart-lang/linter/issues/1034

https://github.com/dart-lang/sdk/issues/42358

Haven't read all of them comments but seems like it is kind of a big deal that no one has yet solved perfectly(except dart metrics package)

Chaitanyabsprip commented 1 year ago

Can you not just search for Images.email in the project. It's not an optimal solution, but it is a solution.

kaciula commented 1 year ago

Can you not just search for Images.email in the project. It's not an optimal solution, but it is a solution.

Of course. But when you have 100 images you do not have the time to go through each and every one.

BirjuVachhani commented 1 year ago

@kaciula is right! It may take huge amount of time to evaluate all the assets to see if they are being used or not!

Another challenge is, this cannot be done efficiently by just looking into project files programmatically. It could be that Images.email is being used in a dart file but that dart file in turn is not being used anywhere! It would be nearly impossible account for cases like this without using dart analyzer!