diff --git a/.changeset/bumpy-paws-unite.md b/.changeset/bumpy-paws-unite.md new file mode 100644 index 000000000000..4c6d261f8895 --- /dev/null +++ b/.changeset/bumpy-paws-unite.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/enhanced-img': minor +--- + +feat: Allow configuring cache directory diff --git a/packages/enhanced-img/src/index.js b/packages/enhanced-img/src/index.js index 485a8a3f1015..027a5c31ea25 100644 --- a/packages/enhanced-img/src/index.js +++ b/packages/enhanced-img/src/index.js @@ -4,10 +4,16 @@ import { imagetools } from 'vite-imagetools'; import { image_plugin } from './vite-plugin.js'; /** + * @typedef {{cache?: CacheOptions}} EnhancedImageOptions + * @typedef {{enabled?: boolean, directory?: string, retention?: number}} CacheOptions + */ + +/** + * @param { EnhancedImageOptions } opts * @returns {import('vite').Plugin[]} */ -export function enhancedImages() { - const imagetools_instance = imagetools_plugin(); +export function enhancedImages(opts = {}) { + const imagetools_instance = imagetools_plugin(opts); return !process.versions.webcontainer ? [image_plugin(imagetools_instance), imagetools_instance] : []; @@ -25,7 +31,11 @@ const fallback = { '.webp': 'png' }; -function imagetools_plugin() { +/** + * @param { EnhancedImageOptions } opts + * @returns {import('vite').Plugin} + */ +function imagetools_plugin(opts = {}) { /** @type {Partial} */ const imagetools_opts = { defaultDirectives: async ({ pathname, searchParams: qs }, metadata) => { @@ -49,6 +59,14 @@ function imagetools_plugin() { namedExports: false }; + if (opts.cache) { + imagetools_opts.cache = { + enabled: opts.cache.enabled, + dir: opts.cache.directory, + retention: opts.cache.retention + }; + } + // TODO: should we make formats or sizes configurable besides just letting people override defaultDirectives? // TODO: generate img rather than picture if only a single format is provided // by resolving the directives for the URL in the preprocessor