diff --git a/src/index.ts b/src/index.ts index a2cf0eb2..7a3d8ae2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,7 +38,7 @@ export function imagetools(userOptions: Partial = {}): Plugin { const directives = [...pluginOptions.customDirectives, ...Object.values(builtinDiretcives)] - const outputFormats = [...pluginOptions.customOutputFormats, ...Object.values(builtinOutputFormats)] + const outputFormats = [...pluginOptions.customOutputFormats, builtinOutputFormats.metadataFormat, builtinOutputFormats.srcsetFormat] return { name: 'imagetools', @@ -116,7 +116,8 @@ export function imagetools(userOptions: Partial = {}): Plugin { // go through all output formats to find the one to use const output = outputFormats .map(f => f(src, outputMetadatas)) - .find(res => !!res) + .find(res => !!res) || builtinOutputFormats.urlFormat(src, outputMetadatas) + // output as JSON or esm depending on the vite config return viteConfig.json?.stringify diff --git a/src/output.ts b/src/output.ts index cfeb0fda..d7766a8b 100644 --- a/src/output.ts +++ b/src/output.ts @@ -1,3 +1,4 @@ +import { dataToEsm } from "@rollup/pluginutils" import { OutputFormat } from "./types" export const metadataFormat: OutputFormat = (src: URL, outputMetadatas: Record[]) => { @@ -9,7 +10,15 @@ export const metadataFormat: OutputFormat = (src: URL, outputMetadatas: Record[]) => { if (!src.searchParams.has('srcset')) return null - return outputMetadatas.length === 1 ? outputMetadatas[0] : outputMetadatas + const sources = outputMetadatas.reduce((prev,meta) => { + if(prev) { + return `${prev}, ${meta.src} ${meta.width}w` + } else { + return `${meta.src} ${meta.width}w` + } + }, '') + + return sources } export const urlFormat: OutputFormat = (src: URL, outputMetadatas: Record[]) => {