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

Feat: Improved cache handling #10

Merged
merged 12 commits into from
Nov 22, 2023
Merged

Feat: Improved cache handling #10

merged 12 commits into from
Nov 22, 2023

Conversation

vHeemstra
Copy link
Owner

@vHeemstra vHeemstra commented Nov 20, 2023

  • Adds option clearCache - Clears the cache before processing
  • Adds option cacheDir - Choose the directory to use for caching
    • cacheDir by default is under the package's root folder in node_modules/.cache/vite-plugin-imagemin/<packageName>
    • Relative cacheDir path will be relative to Vite's <root> dir
    • Absolute cacheDir path will be just that

TODO

  • Add tests for cache functionality and options

- Adds option `cacheDir` - Choose the directory to use for caching
- Adds option `clearCache` - Clears the cache before processing
- Cache dir by default is under the package's `node_modules` folder
- Relative `cacheDir` path will be relative to Vite's `<root>` dir
- Absolute `cacheDir` path will be just that
@vHeemstra vHeemstra force-pushed the improve-cache-handling branch from 08ab50b to 282eed4 Compare November 20, 2023 19:24
This was referenced Nov 20, 2023
…-modules

Feat: better cache handling without external modules
@vHeemstra vHeemstra self-assigned this Nov 21, 2023
* Adds logging stats for files taken from cache (will be marked blue)
* Adds cache key identifier (based on the used options) to distinguish caches.
* Adds support for partial caches. Only missing and changed files will process.
* Some cache logic adjustments
* Some code refactoring/cleanup
* Updated type definitions
* Updated docs
@vHeemstra
Copy link
Owner Author

Log preview when using cache

Performance

Logs from npm run playground:build

logs-using-cache

All blue marked items are taken from the cache. Evidently there's a huge time benefit using the cache.

Re-process errored files and re-evaluate skipped variants

Also note that errored/skipped files will re-process. Files that were skipped because of size are not. They are taken from the cache and re-evaluated for inclusion. If they are still larger, they will be removed from the output dir but kept in the cache (marked dimmed blue; see animated-transparent-1.gif.webp below).

Partial processing

When some of the output files are missing from the cache or have changed (for whatever reason), the cache feature can now handle partial processing for those variants alone. In this example I manually removed two files from the cache:

logs-using-cache-with-missing-files

@naranjamecanica
Copy link
Contributor

LGTM 👍🏻 I've tested it in multiple production projects and it works great!

@naranjamecanica
Copy link
Contributor

The cache key option is really nice so I can force a full optimisation when I change some compression settings.

@vHeemstra
Copy link
Owner Author

Awesome!

Yeah, it is somewhat impossible to serialize/stringify the plugins part of the options since those are instances of plugins. And so there's no other way to have compression configs included in the generated key. So I thought this was (at least for now) the best way to let people distinguish their caches when only changing non-serializable options.

Side note: some options (like verbose and cache) are ignored when hashing the cache key to avoid separate caches when the contents would be the same.

Side note 2: the cache entries are in a file named with the cache key, while all the actual files are just in one folder. As long as their contents stays the same between configs, they can still be re-used I think. If this turns out to have buggy behavior, we can revert to putting all cache entries and files in a subfolder with the key name.

@vHeemstra
Copy link
Owner Author

vHeemstra commented Nov 22, 2023

I will merge this with the main branch into version 1.1.4

Feel free to submit issues if you encounter them or PRs if you have improvements!

When I have more time I will make tests for the new feature to make sure all use cases are covered.

@vHeemstra vHeemstra merged commit 0dfcb65 into main Nov 22, 2023
2 checks passed
@vHeemstra vHeemstra deleted the improve-cache-handling branch November 22, 2023 12:36
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

Successfully merging this pull request may close these issues.

2 participants