Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/types #49

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<Buffer>;

/**
* Imagemin plugin to create WebP images.
*
* @param options
* @returns An imagemin plugin
*/
export default function imageminWebp(options?: Options): Plugin;
22 changes: 22 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Buffer>(await imageminWebP()(buffer));
expectType<Buffer>(await imageminWebP({
preset: 'default',
quality: 75,
alphaQuality: 100,
method: 4,
sns: 50,
autoFilter: false,
sharpness: 0,
lossless: false,
nearLossless: 100,
metadata: 'none'
})(buffer));
})();
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +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",
Expand All @@ -34,8 +36,10 @@
"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",
"xo": "^0.53.1"
}
}