Skip to content

Commit

Permalink
feat: allow usage of all supported sharp output formats (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Oct 26, 2023
1 parent e78ab93 commit b84b271
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/witty-seahorses-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'imagetools-core': minor
'vite-imagetools': minor
---

feat: allow usage of all supported sharp output formats
14 changes: 7 additions & 7 deletions packages/core/src/transforms/__tests__/format.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { format, FormatValue } from '../format'
import { TransformFactoryContext } from '../../types'
import { format } from '../format'
import { applyTransforms } from '../../index'
import sharp, { Sharp } from 'sharp'
import { join } from 'path'
import { consoleLogger } from '../../lib/logger'
import { TransformFactoryContext } from '../../types'
import { join } from 'node:path'
import { toMatchFile } from 'jest-file-snapshot'
import { toMatchImageSnapshot } from 'jest-image-snapshot'
import sharp, { FormatEnum, Sharp } from 'sharp'
import { describe, beforeAll, beforeEach, test, expect, vi } from 'vitest'
import { consoleLogger } from '../../lib/logger'

expect.extend({ toMatchFile, toMatchImageSnapshot })

Expand All @@ -34,7 +34,7 @@ describe('format', () => {
//@ts-expect-error invalid args
const res = format({ format: 'invalid' }, dirCtx)

expect(res).toBeUndefined()
expect(res).toThrow()
})

test('empty', () => {
Expand All @@ -45,7 +45,7 @@ describe('format', () => {
})

test('valid', () => {
const formats: FormatValue[] = ['avif', 'jpg', 'jpeg', 'png', 'heif', 'webp', 'tiff']
const formats: Array<keyof FormatEnum> = ['avif', 'jpg', 'jpeg', 'png', 'heif', 'webp', 'tiff']

for (const f of formats) {
const res = format({ format: f }, dirCtx)
Expand Down
18 changes: 6 additions & 12 deletions packages/core/src/transforms/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,23 @@ import { getProgressive } from './progressive.js'
import { getLossless } from './lossless.js'
import { FormatEnum } from 'sharp'

export const formatValues = ['avif', 'heif', 'jpeg', 'jpg', 'png', 'tiff', 'webp'] as const

export type FormatValue = (typeof formatValues)[number]

export interface FormatOptions {
format: FormatValue
format: keyof FormatEnum
}

export const format: TransformFactory<FormatOptions> = (config) => {
let format: FormatValue | undefined = undefined
let format: keyof FormatEnum

if (config.format && formatValues.includes(config.format)) {
if (!config.format) {
return
} else {
format = config.format
}
if (!format) return

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fixedFormat: keyof FormatEnum = format as any

return function formatTransform(image) {
image[METADATA].format = format

return image.toFormat(fixedFormat, {
return image.toFormat(format, {
quality: getQuality(config, image),
lossless: getLossless(config, image) as boolean,
progressive: getProgressive(config, image) as boolean
Expand Down

0 comments on commit b84b271

Please sign in to comment.