diff --git a/packages/integrations/image/src/build/cache.ts b/packages/integrations/image/src/build/cache.ts index 4e0f87e7dd..cf4df48d0c 100644 --- a/packages/integrations/image/src/build/cache.ts +++ b/packages/integrations/image/src/build/cache.ts @@ -1,19 +1,19 @@ import fs from 'node:fs/promises'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; -import { debug, error, warn } from '../utils/logger.js'; import type { LoggerLevel } from '../utils/logger.js'; +import { debug, warn } from '../utils/logger.js'; const CACHE_FILE = `cache.json`; interface Cache { - [filename: string]: { expires: number } + [filename: string]: { expires: number }; } export class ImageCache { #cacheDir: URL; #cacheFile: URL; - #cache: Cache = { } + #cache: Cache = {}; #logLevel: LoggerLevel; constructor(dir: URL, logLevel: LoggerLevel) { diff --git a/packages/integrations/image/src/build/ssg.ts b/packages/integrations/image/src/build/ssg.ts index bb2e111625..ba920b4f97 100644 --- a/packages/integrations/image/src/build/ssg.ts +++ b/packages/integrations/image/src/build/ssg.ts @@ -1,7 +1,7 @@ import { doWork } from '@altano/tiny-async-pool'; import type { AstroConfig } from 'astro'; -import { bgGreen, black, cyan, dim, green } from 'kleur/colors'; import CachePolicy from 'http-cache-semantics'; +import { bgGreen, black, cyan, dim, green } from 'kleur/colors'; import fs from 'node:fs/promises'; import OS from 'node:os'; import path from 'node:path'; @@ -19,11 +19,11 @@ async function loadLocalImage(src: string | URL) { // we can safely cache local images here. const timeToLive = new Date(); timeToLive.setFullYear(timeToLive.getFullYear() + 1); - + return { data, expires: timeToLive.getTime(), - } + }; } catch { return undefined; } @@ -32,23 +32,23 @@ async function loadLocalImage(src: string | URL) { function webToCachePolicyRequest({ url, method, headers: _headers }: Request): CachePolicy.Request { const headers: CachePolicy.Headers = {}; for (const [key, value] of _headers) { - headers[key] = value; + headers[key] = value; } return { - method, - url, - headers, + method, + url, + headers, }; } function webToCachePolicyResponse({ status, headers: _headers }: Response): CachePolicy.Response { const headers: CachePolicy.Headers = {}; for (const [key, value] of _headers) { - headers[key] = value; + headers[key] = value; } return { - status, - headers, + status, + headers, }; } @@ -88,7 +88,14 @@ export interface SSGBuildParams { cacheDir?: URL; } -export async function ssgBuild({ loader, staticImages, config, outDir, logLevel, cacheDir }: SSGBuildParams) { +export async function ssgBuild({ + loader, + staticImages, + config, + outDir, + logLevel, + cacheDir, +}: SSGBuildParams) { let cache: ImageCache | undefined = undefined; if (cacheDir) { @@ -174,7 +181,7 @@ export async function ssgBuild({ loader, staticImages, config, outDir, logLevel, if (cache?.has(pathRelative)) { data = await cache.get(pathRelative); } - + // a valid cache file wasn't found, transform the image and cache it if (!data) { const transformed = await loader.transform(inputBuffer, transform); diff --git a/packages/integrations/image/src/index.ts b/packages/integrations/image/src/index.ts index 067e8d34f8..9b6a73ebfd 100644 --- a/packages/integrations/image/src/index.ts +++ b/packages/integrations/image/src/index.ts @@ -129,7 +129,9 @@ export default function integration(options: IntegrationOptions = {}): AstroInte } if (loader && 'transform' in loader && staticImages.size > 0) { - const cacheDir = !!resolvedOptions.cacheDir ? new URL(resolvedOptions.cacheDir, _config.root) : undefined; + const cacheDir = !!resolvedOptions.cacheDir + ? new URL(resolvedOptions.cacheDir, _config.root) + : undefined; await ssgBuild({ loader, diff --git a/packages/integrations/image/test/image-ssg.test.js b/packages/integrations/image/test/image-ssg.test.js index 46416f8749..e586070f4b 100644 --- a/packages/integrations/image/test/image-ssg.test.js +++ b/packages/integrations/image/test/image-ssg.test.js @@ -263,7 +263,10 @@ describe('SSG images - build', function () { verifyImage(image.attr('src'), size); - const url = new URL('./fixtures/basic-image/node_modules/.astro/image' + image.attr('src'), import.meta.url); + const url = new URL( + './fixtures/basic-image/node_modules/.astro/image' + image.attr('src'), + import.meta.url + ); expect(await fs.stat(url), 'transformed image was cached').to.not.be.undefined; }); });