diff --git a/.changeset/witty-seahorses-rhyme.md b/.changeset/witty-seahorses-rhyme.md new file mode 100644 index 00000000..da7b2319 --- /dev/null +++ b/.changeset/witty-seahorses-rhyme.md @@ -0,0 +1,6 @@ +--- +'imagetools-core': minor +'vite-imagetools': minor +--- + +feat: allow usage of all supported sharp output formats diff --git a/packages/core/src/transforms/__tests__/format.spec.ts b/packages/core/src/transforms/__tests__/format.spec.ts index 621604a6..9ba3a1b3 100644 --- a/packages/core/src/transforms/__tests__/format.spec.ts +++ b/packages/core/src/transforms/__tests__/format.spec.ts @@ -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 }) @@ -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', () => { @@ -45,7 +45,7 @@ describe('format', () => { }) test('valid', () => { - const formats: FormatValue[] = ['avif', 'jpg', 'jpeg', 'png', 'heif', 'webp', 'tiff'] + const formats: Array = ['avif', 'jpg', 'jpeg', 'png', 'heif', 'webp', 'tiff'] for (const f of formats) { const res = format({ format: f }, dirCtx) diff --git a/packages/core/src/transforms/format.ts b/packages/core/src/transforms/format.ts index a77286fd..343f7cff 100644 --- a/packages/core/src/transforms/format.ts +++ b/packages/core/src/transforms/format.ts @@ -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 = (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