Skip to content

Commit ace744c

Browse files
committed
chore: remove unwanted changes
1 parent 2c20f0f commit ace744c

File tree

2 files changed

+43
-70
lines changed

2 files changed

+43
-70
lines changed

src/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@ class ImageMinimizerPlugin {
235235
: [];
236236

237237
const generators = Array.isArray(this.options.generator)
238-
? this.options.generator.filter(
239-
(item) => !item.type || item.type === "asset",
240-
)
238+
? this.options.generator.filter((item) => item.type === "asset")
241239
: [];
242240

243241
if (minimizers.length === 0 && generators.length === 0) {

src/utils.js

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,7 @@ function fileTypeFromBuffer(input) {
247247
};
248248
}
249249

250-
if (
251-
check([0x52, 0x49, 0x46, 0x46]) &&
252-
check([0x57, 0x45, 0x42, 0x50], { offset: 8 })
253-
) {
250+
if (check([0x57, 0x45, 0x42, 0x50], { offset: 8 })) {
254251
return {
255252
ext: "webp",
256253
mime: "image/webp",
@@ -734,13 +731,29 @@ async function imageminMinify(original, options) {
734731
}
735732

736733
/**
737-
* @type {unknown}
734+
* @type {any}
738735
*/
739736
let pool;
740737

738+
/**
739+
* @typedef {Record<string, Record<string, unknown>>} SquooshEncodeOptions
740+
*/
741+
742+
/**
743+
* @typedef {{
744+
* ingestImage(data: Uint8Array): {
745+
* preprocess(options: Record<string, unknown>): Promise<void>,
746+
* encode(options: Record<string, unknown>): Promise<void>,
747+
* encodedWith: Record<string, {binary: Uint8Array, extension: string}>,
748+
* decoded: {bitmap: {width: number, height: number}}
749+
* },
750+
* close(): Promise<void>
751+
* }} SquooshImagePool
752+
*/
753+
741754
/**
742755
* @param {number} threads The number of threads
743-
* @returns {unknown} The image pool
756+
* @returns {SquooshImagePool} The image pool
744757
*/
745758
function squooshImagePoolCreate(threads = 1) {
746759
const { ImagePool } = require("@squoosh/lib");
@@ -760,32 +773,18 @@ function squooshImagePoolCreate(threads = 1) {
760773
* Sets up the squoosh image pool
761774
*/
762775
function squooshImagePoolSetup() {
763-
if (!pool) {
764-
const os = require("node:os");
765-
766-
// In some cases cpus() returns undefined
767-
// https://github.com/nodejs/node/issues/19022
768-
const threads = os.cpus()?.length ?? 1;
769-
770-
pool = squooshImagePoolCreate(threads);
771-
772-
// workarounds for https://github.com/GoogleChromeLabs/squoosh/issues/1152
773-
// eslint-disable-next-line no-warning-comments
774-
// @ts-ignore - navigator property deletion for squoosh compatibility
775-
// eslint-disable-next-line n/no-unsupported-features/node-builtins
776-
delete globalThis.navigator;
777-
}
776+
// workarounds for https://github.com/GoogleChromeLabs/squoosh/issues/1152
777+
// eslint-disable-next-line no-warning-comments
778+
// @ts-ignore - navigator property deletion for squoosh compatibility
779+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
780+
delete globalThis.navigator;
778781
}
779782

780783
/**
781784
* Tears down the squoosh image pool
782785
*/
783786
async function squooshImagePoolTeardown() {
784-
if (pool) {
785-
await /** @type {{close(): Promise<void>}} */ (pool).close();
786-
787-
pool = undefined;
788-
}
787+
// No shared pool to teardown
789788
}
790789

791790
/**
@@ -797,11 +796,7 @@ async function squooshImagePoolTeardown() {
797796
async function squooshGenerate(original, minifyOptions) {
798797
const squoosh = require("@squoosh/lib");
799798

800-
const isReusePool = Boolean(pool);
801-
const imagePool =
802-
/** @type {{ingestImage(data: Uint8Array): {preprocess(options: Record<string, unknown>): Promise<void>, encode(options: Record<string, unknown>): Promise<void>, encodedWith: Record<string, {binary: Uint8Array, extension: string}>, decoded: {bitmap: {width: number, height: number}}}, close(): Promise<void>}} */ (
803-
pool || squooshImagePoolCreate()
804-
);
799+
const imagePool = squooshImagePoolCreate();
805800
const image = imagePool.ingestImage(new Uint8Array(original.data));
806801

807802
const squooshOptions = /** @type {SquooshOptions} */ (minifyOptions ?? {});
@@ -826,11 +821,9 @@ async function squooshGenerate(original, minifyOptions) {
826821
const { encodeOptions } = squooshOptions;
827822

828823
try {
829-
await image.encode(/** @type {Record<string, unknown>} */ (encodeOptions));
824+
await image.encode(/** @type {SquooshEncodeOptions} */ (encodeOptions));
830825
} catch (error) {
831-
if (!isReusePool) {
832-
await imagePool.close();
833-
}
826+
await imagePool.close();
834827

835828
const originalError =
836829
error instanceof Error ? error : new Error(/** @type {string} */ (error));
@@ -842,31 +835,21 @@ async function squooshGenerate(original, minifyOptions) {
842835
return null;
843836
}
844837

845-
if (!isReusePool) {
838+
if (Object.keys(image.encodedWith).length === 0) {
846839
await imagePool.close();
847-
}
848840

849-
if (Object.keys(image.encodedWith).length === 0) {
850-
original.errors.push(
851-
new Error(
852-
`No result from 'squoosh' for '${original.filename}', please configure the 'encodeOptions' option to generate images`,
853-
),
841+
const originalError = new Error(
842+
`Error with '${original.filename}': No encoded images found`,
854843
);
855844

845+
original.errors.push(originalError);
856846
return null;
857847
}
858848

859-
if (Object.keys(image.encodedWith).length > 1) {
860-
original.errors.push(
861-
new Error(
862-
`Multiple values for the 'encodeOptions' option is not supported for '${original.filename}', specify only one codec for the generator`,
863-
),
864-
);
849+
await imagePool.close();
865850

866-
return null;
867-
}
868-
869-
const { binary, extension } = await Object.values(image.encodedWith)[0];
851+
const { binary, extension } =
852+
await image.encodedWith[Object.keys(image.encodedWith)[0]];
870853
const { width, height } = (await image.decoded).bitmap;
871854

872855
const filename = replaceFileExtension(original.filename, extension);
@@ -923,11 +906,7 @@ async function squooshMinify(original, options) {
923906
return null;
924907
}
925908

926-
const isReusePool = Boolean(pool);
927-
const imagePool =
928-
/** @type {{ingestImage(data: Uint8Array): {preprocess(options: Record<string, unknown>): Promise<void>, encode(options: Record<string, unknown>): Promise<void>, encodedWith: Record<string, {binary: Uint8Array, extension: string}>, decoded: {bitmap: {width: number, height: number}}}, close(): Promise<void>}} */ (
929-
pool || squooshImagePoolCreate()
930-
);
909+
const imagePool = pool || squooshImagePoolCreate();
931910
const image = imagePool.ingestImage(new Uint8Array(original.data));
932911
const squooshOptions = /** @type {SquooshOptions} */ (options ?? {});
933912

@@ -950,20 +929,18 @@ async function squooshMinify(original, options) {
950929

951930
const { encodeOptions = {} } = squooshOptions;
952931

953-
if (!(/** @type {Record<string, unknown>} */ (encodeOptions)[targetCodec])) {
954-
/** @type {Record<string, unknown>} */ (encodeOptions)[targetCodec] = {};
932+
if (!(/** @type {SquooshEncodeOptions} */ (encodeOptions)[targetCodec])) {
933+
/** @type {SquooshEncodeOptions} */ (encodeOptions)[targetCodec] = {};
955934
}
956935

957936
try {
958937
await image.encode({
959-
[targetCodec]: /** @type {Record<string, unknown>} */ (encodeOptions)[
938+
[targetCodec]: (encodeOptions)[
960939
targetCodec
961940
],
962941
});
963942
} catch (error) {
964-
if (!isReusePool) {
965-
await imagePool.close();
966-
}
943+
await imagePool.close();
967944

968945
const originalError =
969946
error instanceof Error ? error : new Error(/** @type {string} */ (error));
@@ -975,13 +952,11 @@ async function squooshMinify(original, options) {
975952
return null;
976953
}
977954

978-
if (!isReusePool) {
979-
await imagePool.close();
980-
}
981-
982955
const { binary } = await image.encodedWith[targets[ext]];
983956
const { width, height } = (await image.decoded).bitmap;
984957

958+
await imagePool.close();
959+
985960
return {
986961
filename: original.filename,
987962
data: Buffer.from(binary),

0 commit comments

Comments
 (0)