From b3cffe44c1d967250cd7ba764561b49ee112d177 Mon Sep 17 00:00:00 2001 From: Llewellyn Collins Date: Thu, 29 Jun 2023 20:14:28 +0100 Subject: [PATCH 1/5] feat: add definition file --- index.d.ts | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..f9294a9 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,140 @@ +export interface Crop { + /** + * Crop x coordinate. + */ + x: number; + + /** + * Crop y coordinate. + */ + y: number; + + /** + * Crop width. + */ + width: number; + + /** + * Crop height. + */ + height: number; +} + +export interface Resize { + /** + * Resize width. + */ + width: number; + + /** + * Resize height. + */ + height: number; +} + +type Metadata = 'all' | 'none' | 'exif' | 'icc' | 'xmp'; + +export interface Options { + /** + * Preset setting, one of `default`, `photo`, `picture`, `drawing`, `icon` and `text`. + * + * @default 'default' + */ + preset?: 'default' | 'photo' | 'picture' | 'drawing' | 'icon' | 'text'; + + /** + * Set quality factor between `0` and `100`. + * + * @default 75 + */ + quality?: number; + + /** + * Set transparency-compression quality between `0` and `100`. + * + * @default 100 + */ + alphaQuality?: number; + + /** + * Specify the compression method to use, between `0` (fastest) and `6` (slowest). This parameter controls the trade off between encoding speed and the compressed file size and quality. + * + * @default 4 + */ + method?: number; + + /** + * Set target size in bytes. + */ + size?: number; + + /** + * Set the amplitude of spatial noise shaping between `0` and `100`. + * + * @default 50 + */ + sns?: number; + + /** + * Set deblocking filter strength between `0` (off) and `100`. + */ + filter?: number; + + /** + * Adjust filter strength automatically. + * + * @default false + */ + autoFilter?: boolean; + + /** + * Set filter sharpness between `0` (sharpest) and `7` (least sharp). + * + * @default 0 + */ + sharpness?: number; + + /** + * Encode images losslessly. If set to a number, activates lossless preset with given level between `0` (fastest, larger files) and `9` (slowest, smaller files). + * + * @default false + */ + lossless?: boolean | number; + + /** + * Encode losslessly with an additional lossy pre-processing step, with a quality factor between `0` (maximum pre-processing) and `100` (same as lossless). + * + * @default 100 + */ + nearLossless?: number; + + /** + * Crop the image. + */ + crop?: Crop; + + /** + * Resize the image. Happens after the `crop`. + */ + resize?: Resize; + + /** + * A list of metadata to copy from the input to the output if present. + * + * @default 'none' + */ + metadata?: Metadata | Metadata[]; +} + +/** +Buffer or stream to optimize. +*/ +export type Plugin = (input: Buffer | NodeJS.ReadableStream) => Promise; + +/** + * Imagemin plugin to create WebP images. + * + * @param options + * @returns An imagemin plugin + */ +export default function imageminWebp(options: Options): Plugin; From 4446cc9c124d27063131ddbd337b674c4d7a2c5c Mon Sep 17 00:00:00 2001 From: Llewellyn Collins Date: Thu, 29 Jun 2023 20:22:36 +0100 Subject: [PATCH 2/5] chore: add types to package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 57b29e3..4e6169b 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "scripts": { "test": "xo && ava" }, + "types": "./index.d.ts", "files": [ "index.js" ], From 249e597252a952ee1c649729fc0da767e8ffec04 Mon Sep 17 00:00:00 2001 From: Llewellyn Collins Date: Thu, 29 Jun 2023 20:36:55 +0100 Subject: [PATCH 3/5] chore: add tsd package --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 4e6169b..1b8cdae 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "devDependencies": { "ava": "^5.1.1", "is-webp": "^2.0.0", + "tsd": "^0.28.1", "xo": "^0.53.1" } } From 3e667248c909cba807161498b9b26093715828aa Mon Sep 17 00:00:00 2001 From: Llewellyn Collins Date: Thu, 29 Jun 2023 20:37:47 +0100 Subject: [PATCH 4/5] chore: add node types --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 1b8cdae..5dcadfa 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "is-cwebp-readable": "^3.0.0" }, "devDependencies": { + "@types/node": "^20.3.2", "ava": "^5.1.1", "is-webp": "^2.0.0", "tsd": "^0.28.1", From a7404a995b5ae66532550f3116d549a10de8dcfd Mon Sep 17 00:00:00 2001 From: Llewellyn Collins Date: Thu, 29 Jun 2023 20:44:20 +0100 Subject: [PATCH 5/5] test: add definition test --- index.d.ts | 2 +- index.test-d.ts | 22 ++++++++++++++++++++++ package.json | 5 +++-- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 index.test-d.ts diff --git a/index.d.ts b/index.d.ts index f9294a9..e8b57cf 100644 --- a/index.d.ts +++ b/index.d.ts @@ -137,4 +137,4 @@ export type Plugin = (input: Buffer | NodeJS.ReadableStream) => Promise; * @param options * @returns An imagemin plugin */ -export default function imageminWebp(options: Options): Plugin; +export default function imageminWebp(options?: Options): Plugin; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..e02b3a2 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,22 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { expectType } from 'tsd'; +import imageminWebP from '.'; + +const buffer = fs.readFileSync(path.join(__dirname, 'fixures', 'test.png')); + +(async () => { + expectType(await imageminWebP()(buffer)); + expectType(await imageminWebP({ + preset: 'default', + quality: 75, + alphaQuality: 100, + method: 4, + sns: 50, + autoFilter: false, + sharpness: 0, + lossless: false, + nearLossless: 100, + metadata: 'none' + })(buffer)); +})(); diff --git a/package.json b/package.json index 5dcadfa..e5f03b8 100644 --- a/package.json +++ b/package.json @@ -10,11 +10,12 @@ "node": ">=14.16" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd" }, "types": "./index.d.ts", "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "compress",