Skip to content

Commit

Permalink
feat: add output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Mar 15, 2021
1 parent 9bb1f34 commit 70cd9cd
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/vite/src/output-formats.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 70cd9cd

Please sign in to comment.