Skip to content

Commit b705756

Browse files
committed
Add param to webp option
1 parent aa7403f commit b705756

3 files changed

Lines changed: 84 additions & 2 deletions

File tree

src/options/webpOptions.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@ const correctOptions = {
77
lossless: true,
88
};
99

10+
const correctPresets = {
11+
default: true,
12+
photo: true,
13+
picture: true,
14+
drawing: true,
15+
icon: true,
16+
text: true,
17+
};
18+
1019
const getOpt = (options: WebpOptionsPartial): WebpOptions | undefined =>
1120
options.webp_options || options.webpo;
1221

1322
const test = (options: WebpOptionsPartial): boolean => Boolean(getOpt(options));
1423

1524
const build = (options: WebpOptionsPartial): string => {
1625
const webpOptions = getOpt(options);
17-
let compression: string, smart_subsample: boolean | undefined;
26+
let compression: string,
27+
smart_subsample: boolean | undefined,
28+
preset: string | undefined;
1829

1930
guardIsUndef(webpOptions, "webp_options");
2031

@@ -27,11 +38,26 @@ const build = (options: WebpOptionsPartial): string => {
2738
smart_subsample = webpOptions.smart_subsample;
2839
guardIsNotBool(smart_subsample, "webp_options.smart_subsample");
2940
}
41+
42+
if (webpOptions.preset !== undefined) {
43+
preset = webpOptions.preset;
44+
guardIsValidVal(correctPresets, preset, "webp_options.preset");
45+
}
3046
}
3147

3248
guardIsValidVal(correctOptions, compression, "webp_options");
3349

34-
return `webpo:${compression}${smart_subsample !== undefined ? `:${smart_subsample}` : ""}`;
50+
let result = `webpo:${compression}`;
51+
52+
if (smart_subsample !== undefined || preset !== undefined) {
53+
result += `:${smart_subsample !== undefined ? smart_subsample : ""}`;
54+
}
55+
56+
if (preset !== undefined) {
57+
result += `:${preset}`;
58+
}
59+
60+
return result;
3561
};
3662

3763
export { test, build };

src/types/webpOptions.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@
1414
*/
1515
type WebPCompressionOptions = "lossy" | "near_lossless" | "lossless";
1616

17+
/**
18+
* Available WebP preset values:
19+
* - `"default"` - A general-purpose preset (default setting)
20+
* - `"photo"` - A digital picture, like a portrait or indoor shot
21+
* - `"picture"` - An outdoor photograph with natural lighting
22+
* - `"drawing"` - A hand or line drawing with high-contrast details
23+
* - `"icon"` - A small-sized colorful image
24+
* - `"text"` - A text image with large areas of uniform color
25+
*/
26+
type WebPPresetOptions =
27+
| "default"
28+
| "photo"
29+
| "picture"
30+
| "drawing"
31+
| "icon"
32+
| "text";
33+
1734
/**
1835
* *WEBP options*. **PRO feature**
1936
*
@@ -31,6 +48,10 @@ type WebpOptions =
3148
* when `true`, enables smart subsampling. Smart subsampling increases the resulting file size and compression time but improves quality. Default: `false`
3249
*/
3350
smart_subsample?: boolean;
51+
/**
52+
* A hint to the encoder about the type of image being compressed. This helps optimize WebP compression based on the specific type of image being processed.
53+
*/
54+
preset?: WebPPresetOptions;
3455
};
3556

3657
/**

tests/optionsBasic/webpOptions.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,40 @@ describe("webpOptions", () => {
5858
})
5959
).toEqual("webpo:lossy:false");
6060
});
61+
62+
it("should support `preset` option", () => {
63+
expect(
64+
build({ webp_options: { compression: "lossy", preset: "photo" } })
65+
).toEqual("webpo:lossy::photo");
66+
67+
expect(
68+
build({
69+
webp_options: {
70+
compression: "lossy",
71+
smart_subsample: true,
72+
preset: "picture",
73+
},
74+
})
75+
).toEqual("webpo:lossy:true:picture");
76+
77+
expect(
78+
build({
79+
webp_options: {
80+
compression: "lossless",
81+
smart_subsample: false,
82+
preset: "drawing",
83+
},
84+
})
85+
).toEqual("webpo:lossless:false:drawing");
86+
});
87+
88+
it("should throw an error if preset is invalid", () => {
89+
expect(() =>
90+
// @ts-expect-error: Let's ignore an error (check for users with vanilla js).
91+
build({ webp_options: { compression: "lossy", preset: "invalid" } })
92+
).toThrow(
93+
"webp_options.preset is invalid. Valid values are: default, photo, picture, drawing, icon, text"
94+
);
95+
});
6196
});
6297
});

0 commit comments

Comments
 (0)