Skip to content

Commit

Permalink
fix: make urlFormat the fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Mar 8, 2021
1 parent 989988a commit 763cc3b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): 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',
Expand Down Expand Up @@ -116,7 +116,8 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): 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
Expand Down
11 changes: 10 additions & 1 deletion src/output.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dataToEsm } from "@rollup/pluginutils"
import { OutputFormat } from "./types"

export const metadataFormat: OutputFormat = (src: URL, outputMetadatas: Record<string, any>[]) => {
Expand All @@ -9,7 +10,15 @@ export const metadataFormat: OutputFormat = (src: URL, outputMetadatas: Record<s
export const srcsetFormat: OutputFormat = (src: URL, outputMetadatas: Record<string, any>[]) => {
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<string, any>[]) => {
Expand Down

0 comments on commit 763cc3b

Please sign in to comment.