From 2c5ed954e25e8c72097847db3e46d8cf2fd6311b Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Mon, 23 Sep 2024 22:41:40 +0200 Subject: [PATCH] cache optimizations --- packages/build-utils/src/resize.ts | 14 ++++++++------ packages/build-utils/src/utils.ts | 18 +++++++++++++++++- packages/vite-plugin/src/loader.ts | 5 +++-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/build-utils/src/resize.ts b/packages/build-utils/src/resize.ts index f11a40ae8..e30cdec4c 100644 --- a/packages/build-utils/src/resize.ts +++ b/packages/build-utils/src/resize.ts @@ -1,4 +1,6 @@ +import type { ImageType } from '@responsive-image/core'; import type { ImageConfig } from 'imagetools-core'; +import { readFile, writeFile } from 'node:fs/promises'; import type { Metadata } from 'sharp'; import type { ImageLoaderChainedResult, @@ -6,10 +8,7 @@ import type { ImageProcessingResult, OutputImageType, } from './types'; -import type { ImageType } from '@responsive-image/core'; -import { readFile, writeFile } from 'node:fs/promises'; -import { join } from 'node:path'; -import { hash } from './utils'; +import { getCacheFilename } from './utils'; export async function generateResizedImage( input: ImageLoaderChainedResult, @@ -34,9 +33,11 @@ export async function generateResizedImage( let cacheFile: string | undefined = undefined; if (options.cache && input.hash) { - cacheFile = join( + cacheFile = getCacheFilename( + input, + config, + format, options.cacheDir ?? './node_modules/.cache', - hash([config, input.hash]), ); try { @@ -44,6 +45,7 @@ export async function generateResizedImage( // console.log(`file read from cache: ${cacheFile}`); return buffer; + // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (e) { // do nothing } diff --git a/packages/build-utils/src/utils.ts b/packages/build-utils/src/utils.ts index d06d7ba24..8b9f55947 100644 --- a/packages/build-utils/src/utils.ts +++ b/packages/build-utils/src/utils.ts @@ -2,6 +2,8 @@ import sharp, { type Metadata } from 'sharp'; import type { ImageOptions, ImageLoaderChainedResult } from './types'; import { createHash } from 'node:crypto'; import { readFileSync } from 'node:fs'; +import type { ImageConfig } from 'imagetools-core'; +import { join } from 'node:path'; const defaultImageConfig: ImageOptions = { w: [640, 750, 828, 1080, 1200, 1920, 2048, 3840], @@ -78,6 +80,7 @@ export function getOptions( export function normalizeInput( input: string | Buffer | ImageLoaderChainedResult, + options: { generateHash?: boolean } = {}, ): ImageLoaderChainedResult { if (typeof input === 'string') { const filename = getPathname(input); @@ -89,7 +92,7 @@ export function normalizeInput( images: [], sharp: sharp(input), imports: [], - hash: hash([input]), + hash: options.generateHash ? hash([input]) : undefined, }; } @@ -129,3 +132,16 @@ export function hash(datas: Array): string { return hash.digest('hex'); } + +export function getCacheFilename( + input: ImageLoaderChainedResult, + config: ImageConfig, + extension: string, + cacheDir: string, +): string { + if (!input.hash) { + throw new Error('Expected hash to be availiable'); + } + + return join(cacheDir, `${hash([config, input.hash])}.${extension}`); +} diff --git a/packages/vite-plugin/src/loader.ts b/packages/vite-plugin/src/loader.ts index 4d43926e1..05d473568 100644 --- a/packages/vite-plugin/src/loader.ts +++ b/packages/vite-plugin/src/loader.ts @@ -1,7 +1,7 @@ import { createFilter } from '@rollup/pluginutils'; import type { Plugin } from 'vite'; import type { Options } from './types'; -import { META_KEY } from './utils'; +import { getViteOptions, META_KEY } from './utils'; import { normalizeInput } from '@responsive-image/build-utils'; export default function loaderPlugin( @@ -18,7 +18,8 @@ export default function loaderPlugin( return; } - const data = normalizeInput(id); + const options = getViteOptions(id, userOptions); + const data = normalizeInput(id, { generateHash: options.cache }); return { // Only the export plugin will actually return ESM code