Skip to content

Commit

Permalink
cache optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
simonihmig committed Sep 23, 2024
1 parent f2365d7 commit 2c5ed95
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
14 changes: 8 additions & 6 deletions packages/build-utils/src/resize.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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,
ImageOptions,
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,
Expand All @@ -34,16 +33,19 @@ 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 {
const buffer = await readFile(cacheFile);
// console.log(`file read from cache: ${cacheFile}`);

return buffer;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
// do nothing
}
Expand Down
18 changes: 17 additions & 1 deletion packages/build-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -78,6 +80,7 @@ export function getOptions<BUILDOPTIONS>(

export function normalizeInput(
input: string | Buffer | ImageLoaderChainedResult,
options: { generateHash?: boolean } = {},
): ImageLoaderChainedResult {
if (typeof input === 'string') {
const filename = getPathname(input);
Expand All @@ -89,7 +92,7 @@ export function normalizeInput(
images: [],
sharp: sharp(input),
imports: [],
hash: hash([input]),
hash: options.generateHash ? hash([input]) : undefined,
};
}

Expand Down Expand Up @@ -129,3 +132,16 @@ export function hash(datas: Array<string | object | Buffer>): 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}`);
}
5 changes: 3 additions & 2 deletions packages/vite-plugin/src/loader.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 2c5ed95

Please sign in to comment.