diff --git a/packages/integrations/image/src/build/ssg.ts b/packages/integrations/image/src/build/ssg.ts index cda7f76ef..c3cc75642 100644 --- a/packages/integrations/image/src/build/ssg.ts +++ b/packages/integrations/image/src/build/ssg.ts @@ -6,10 +6,31 @@ import OS from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import type { SSRImageService, TransformOptions } from '../loaders/index.js'; -import { loadLocalImage, loadRemoteImage } from '../utils/images.js'; import { debug, info, LoggerLevel, warn } from '../utils/logger.js'; import { isRemoteImage } from '../utils/paths.js'; +async function loadLocalImage(src: string | URL) { + try { + return await fs.readFile(src); + } catch { + return undefined; + } +} + +async function loadRemoteImage(src: string) { + try { + const res = await fetch(src); + + if (!res.ok) { + return undefined; + } + + return Buffer.from(await res.arrayBuffer()); + } catch { + return undefined; + } +} + function getTimeStat(timeStart: number, timeEnd: number) { const buildTime = timeEnd - timeStart; return buildTime < 750 ? `${Math.round(buildTime)}ms` : `${(buildTime / 1000).toFixed(2)}s`; diff --git a/packages/integrations/image/src/lib/get-picture.ts b/packages/integrations/image/src/lib/get-picture.ts index 132d93ee1..e3b07b505 100644 --- a/packages/integrations/image/src/lib/get-picture.ts +++ b/packages/integrations/image/src/lib/get-picture.ts @@ -1,9 +1,9 @@ /// import mime from 'mime'; -import { extname } from 'node:path'; import { OutputFormat, parseAspectRatio, TransformOptions } from '../loaders/index.js'; import { ImageMetadata } from '../vite-plugin-astro-image.js'; import { getImage } from './get-image.js'; +import { extname } from '../utils/paths.js'; export interface GetPictureParams { src: string | ImageMetadata | Promise<{ default: ImageMetadata }>; diff --git a/packages/integrations/image/src/utils/images.ts b/packages/integrations/image/src/utils/images.ts deleted file mode 100644 index f9b94b1e8..000000000 --- a/packages/integrations/image/src/utils/images.ts +++ /dev/null @@ -1,23 +0,0 @@ -import fs from 'node:fs/promises'; - -export async function loadLocalImage(src: string | URL) { - try { - return await fs.readFile(src); - } catch { - return undefined; - } -} - -export async function loadRemoteImage(src: string) { - try { - const res = await fetch(src); - - if (!res.ok) { - return undefined; - } - - return Buffer.from(await res.arrayBuffer()); - } catch { - return undefined; - } -} diff --git a/packages/integrations/image/src/utils/paths.ts b/packages/integrations/image/src/utils/paths.ts index 1f691f35a..1ebf3ece7 100644 --- a/packages/integrations/image/src/utils/paths.ts +++ b/packages/integrations/image/src/utils/paths.ts @@ -10,14 +10,15 @@ function removeQueryString(src: string) { return index > 0 ? src.substring(0, index) : src; } -function extname(src: string, format?: OutputFormat) { - const index = src.lastIndexOf('.'); +export function extname(src: string) { + const base = basename(src); + const index = base.lastIndexOf('.'); if (index <= 0) { return ''; } - return src.substring(index); + return src.substring(src.length - (base.length - index)); } function removeExtname(src: string) {