From 70cd9cd54ec227f9fe805cfff2b287baaba413bf Mon Sep 17 00:00:00 2001 From: JonasKruckenberg Date: Mon, 15 Mar 2021 15:20:37 +0100 Subject: [PATCH] feat: add output formats --- packages/vite/src/output-formats.ts | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 packages/vite/src/output-formats.ts diff --git a/packages/vite/src/output-formats.ts b/packages/vite/src/output-formats.ts new file mode 100644 index 00000000..b510537d --- /dev/null +++ b/packages/vite/src/output-formats.ts @@ -0,0 +1,31 @@ +import type { ImageConfig } from 'imagetools-core' +import { OutputFormat } from './types' + +export const urlFormat: OutputFormat = (metadatas) => { + const urls: string[] = metadatas.map(metadata => metadata.src) + + return urls.length == 1 ? urls[0] : urls +} + +export const srcsetFormat: OutputFormat = (metadatas: ImageConfig[]) => { + const sources = metadatas.reduce((prev, meta) => { + if (prev) { + return `${prev}, ${meta.src} ${meta.width}w` + } else { + return `${meta.src} ${meta.width}w` + } + }, '') + + return sources +} + +export const metadataFormat: OutputFormat = (metadatas: ImageConfig[]) => { + return metadatas.length === 1 ? metadatas[0] : metadatas +} + +export const builtinOutputFormats = { + url: urlFormat, + srcset: srcsetFormat, + metadata: metadataFormat, + meta: metadataFormat +} \ No newline at end of file