Skip to content
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

Caching does not work properly #7

Closed
naranjamecanica opened this issue Nov 16, 2023 · 7 comments
Closed

Caching does not work properly #7

naranjamecanica opened this issue Nov 16, 2023 · 7 comments

Comments

@naranjamecanica
Copy link
Contributor

Hi, just to let you know, i've found out that the caching is not working properly yet... 😱

  • When building for a second time, the uncompressed variant is copied instead of keeping the optimised
  • When using emptyOutDir in Vite we should use a cached version of the compressed image.

I'm going to look into it, but that will be probably beginning of next week.

A new release with cache:false as default could be a good idea...

@vHeemstra
Copy link
Owner

vHeemstra commented Nov 16, 2023

Yes, the problem is that (by default) Vite empties the outDir, so the optimized files are deleted on start of every build.

And although the cache recognizes that the input file has not changed between builds, it will still force re-process because the output files are no longer there (outputsStillExist check).

I'm not sure what happens when emptyOutDir is set to false. I suspect previous optimized files will get overwritten by their original input variants. Those would then be seen by the cache as unchanged from the last run, and since the files still are present, they will not re-process. Leaving the unoptimized files now instead of the optimized ones from the previous build.

I think there should be a custom caching put in place that stores the results for re-use. I will also look into this.

@vHeemstra
Copy link
Owner

vHeemstra commented Nov 16, 2023

So I made a solution. (Included in release version 1.1.1)

All output files are now also stored in a cache folder (node_modules/.vite-plugin-imagemin).
When processing all files, it:

  1. Checks if the input file has changed
  2. If unchanged, check each output file
  3. (Per output file) Check if it exists in the cache folder and if that file has changed
  4. If present and unchanged, use this cached version by copying it to the outDir
  5. All other situations: re-process the input file
  • This works for assets as well as static files in publicDir.
  • This works with both emptyOutDir on true and false.
  • Only downside: a duplicate folder with all assets after processing inside node_modules. And currently there is no cleanup of this folder, only adding new files and overwriting updated ones.

@naranjamecanica
Hope this works for you as well!
Do you have any ideas on how to implement a good cleanup routine for the cache folder?

@naranjamecanica
Copy link
Contributor Author

Hej, cool addition! Only in my setup the caching dir gets created in my dist folder, see screenshot... 😅
image

I'm working on a tight deadline right now, so can look in to it next week.. maybe using the same directory as the caching dictionary is also a good idea. 👍🏻

@vHeemstra
Copy link
Owner

vHeemstra commented Nov 17, 2023

Did you set a different <root> for the project?

The folder used for caching the output files is relative to the root folder as configured for Vite (so <root>/node_modules/.vite-plugin-imagemin/). This is the most likely place the node_modules folder will be per NPM/Vite convention, hence that is the path I used.

In default Vite configuration, this is also where the .cache folder is created for the cache module you introduced for tracking changes.

See also root configuration in the Vite documentation.

So this seems the most logical place to put it. But we could add a option to specify a different cache directory?

Using the directory of the cache module seems unwise, since I don't know whether or not (or when) this folder is deleted/emptied. So relying on our own cache folder is the best option.

@vHeemstra
Copy link
Owner

Note:
There was a different error in the cache checks (handling of async/await), which has been fixed in version 1.1.2.

@naranjamecanica
Copy link
Contributor Author

Yes, i specify an other root dir for my projects

const options: UserConfig = {
    root: path.resolve(cwd, 'source'),
	...
}

The root dir is not connected to the root of the whole project where node_modules resides but to the directory where the entry points of the projects are located. The caching module uses an different approach, I'll make a pull request to mimic that behaviour.

About a cleanup routine for the cache, that can be a tricky one, but i'll give it a shot later.

@vHeemstra
Copy link
Owner

See #10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants