Skip to content
Merged
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
40 changes: 11 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ const {
} = require("./utils.js");
const worker = require("./worker");

/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("schema-utils").Schema} Schema */
/** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */
/** @typedef {import("webpack").Compiler} Compiler */
/** @typedef {import("webpack").Compilation} Compilation */
/** @typedef {import("webpack").WebpackError} WebpackError */
/** @typedef {import("webpack").Asset} Asset */
/** @typedef {import("webpack").AssetInfo} AssetInfo */
/** @typedef {import("webpack").sources.Source} Source */
/** @typedef {import("webpack").Module} Module */
/** @typedef {import("webpack").TemplatePath} TemplatePath */
/** @typedef {import("webpack").PathData} PathData */
/** @typedef {import("./utils.js").imageminMinify} ImageminMinifyFunction */
/** @typedef {import("./utils.js").squooshMinify} SquooshMinifyFunction */

Expand All @@ -41,24 +42,15 @@ const worker = require("./worker");
* @returns {boolean}
*/

// eslint-disable-next-line jsdoc/no-restricted-syntax
/**
* @typedef {object} ImageminOptions
* @property {Array<string | [string, Record<string, any>?] | import("imagemin").Plugin>} plugins plugins
*/

// eslint-disable-next-line jsdoc/no-restricted-syntax
/**
* @typedef {{ [key: string]: any }} SquooshOptions
*/
/** @typedef {typeof import("./worker").isFilenameProcessed} IsFilenameProcessed */

/**
* @typedef {object} WorkerResult
* @property {string} filename filename
* @property {Buffer} data data buffer
* @property {Array<Error>} warnings warnings
* @property {Array<Error>} errors errors
* @property {AssetInfo} info asset info
* @property {AssetInfo & { [worker.isFilenameProcessed]?: boolean }} info asset info
*/

/**
Expand Down Expand Up @@ -114,11 +106,6 @@ const worker = require("./worker");
* @typedef {BasicTransformerImplementation<T> & BasicTransformerHelpers} TransformerFunction
*/

/**
* @typedef {object} PathData
* @property {string=} filename filename
*/

/**
* @callback FilenameFn
* @param {PathData} pathData path data
Expand Down Expand Up @@ -155,7 +142,7 @@ const worker = require("./worker");
* @property {Buffer} input input buffer
* @property {Transformer<T> | Transformer<T>[]} transformer transformer
* @property {string=} severityError severity error setting
* @property {(filenameTemplate: string, options: { filename: string }) => string=} generateFilename filename generator function
* @property {(filename: TemplatePath, data: PathData) => string} generateFilename filename generator function
*/

/**
Expand All @@ -169,7 +156,7 @@ const worker = require("./worker");
* @property {Rule=} test test to match files against
* @property {Rule=} include files to include
* @property {Rule=} exclude files to exclude
* @property {T extends any[] ? { [P in keyof T]: Minimizer<T[P]> } : Minimizer<T> | Minimizer<T>[]=} minimizer allows to setup the minimizer
* @property {T extends any[] ? { [P in keyof T]: Minimizer<T[P]> } : Minimizer<T> | Minimizer<T>[]=} minimizer allows to set the minimizer
* @property {G extends any[] ? { [P in keyof G]: Generator<G[P]> } : Generator<G>[]=} generator allows to set the generator
* @property {boolean=} loader automatically adding `image-loader`.
* @property {number=} concurrency maximum number of concurrency optimization processes in one time
Expand Down Expand Up @@ -328,6 +315,7 @@ class ImageMinimizerPlugin {

// In some cases cpus() returns undefined
// https://github.com/nodejs/node/issues/19022
// TODO fix me
const limit = Math.max(
1,
this.options.concurrency ?? os.cpus()?.length ?? 1,
Expand Down Expand Up @@ -372,15 +360,9 @@ class ImageMinimizerPlugin {
});
}

compilation.warnings = [
...compilation.warnings,
.../** @type {[WebpackError]} */ (output.warnings),
];
compilation.warnings = [...compilation.warnings, ...output.warnings];

compilation.errors = [
...compilation.errors,
.../** @type {[WebpackError]} */ (output.errors),
];
compilation.errors = [...compilation.errors, ...output.errors];

if (compilation.getAsset(output.filename)) {
compilation.updateAsset(
Expand Down Expand Up @@ -459,7 +441,7 @@ class ImageMinimizerPlugin {
}

/**
* @param {import("webpack").Compiler} compiler compiler
* @param {Compiler} compiler compiler
*/
apply(compiler) {
const pluginName = this.constructor.name;
Expand Down
8 changes: 4 additions & 4 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ function processSizeQuery(transformers, widthQuery, heightQuery, unitQuery) {
return transformers.map((transformer) => {
const minimizer = { ...transformer };

const minimizerOptions =
/** @type { import("./index").BasicTransformerOptions<T> & { resize?: import("./index").ResizeOptions }} */
// @ts-expect-error
{ ...minimizer.options };
const minimizerOptions = {
.../** @type {{ options: import("./index").BasicTransformerOptions<T> & { resize?: import("./index").ResizeOptions }}} */
(minimizer).options,
};

minimizerOptions.resize = { ...minimizerOptions?.resize };
minimizer.options = minimizerOptions;
Expand Down
Loading
Loading