Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 19, 2024
1 parent b7f8570 commit 72aa77f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
/** @typedef {import("webpack").Asset} Asset */
/** @typedef {import("webpack").AssetInfo} AssetInfo */
/** @typedef {import("webpack").sources.Source} Source */
/** @typedef {import("webpack").Module} Module */
/** @typedef {import("./utils.js").imageminMinify} ImageminMinifyFunction */
/** @typedef {import("./utils.js").squooshMinify} SquooshMinifyFunction */

Expand Down Expand Up @@ -484,8 +485,14 @@ class ImageMinimizerPlugin {
compilation.hooks.assetPath.tap(
{ name: pluginName },
(filename, data, info) => {
// @ts-ignore
const newInfo = data?.module ? IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS.get(data.module) : undefined;
const newInfo =
/** @type {{ module: Module }} */
(data)?.module
? IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS.get(
/** @type {{ module: Module }} */
(data).module,
)
: undefined;

if (info && newInfo) {
Object.assign(info, newInfo);
Expand Down
9 changes: 7 additions & 2 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const path = require("path");

const worker = require("./worker");
const schema = require("./loader-options.json");
const { isAbsoluteURL, IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS } = require("./utils.js");
const {
isAbsoluteURL,
IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS,
} = require("./utils.js");

/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compilation} Compilation */
Expand Down Expand Up @@ -97,7 +100,9 @@ function processSizeQuery(transformers, widthQuery, heightQuery, unitQuery) {
*/
async function loader(content) {
// Avoid optimize twice
const imageMinimizerPluginInfo = this._module ? IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS.get(this._module) : undefined;
const imageMinimizerPluginInfo = this._module
? IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS.get(this._module)
: undefined;

Check warning on line 105 in src/loader.js

View check run for this annotation

Codecov / codecov/patch

src/loader.js#L105

Added line #L105 was not covered by tests

if (
imageMinimizerPluginInfo?.minimized ||
Expand Down
3 changes: 3 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const path = require("path");
/** @typedef {import("./index").SquooshOptions} SquooshOptions */
/** @typedef {import("imagemin").Options} ImageminOptions */
/** @typedef {import("webpack").WebpackError} WebpackError */
/** @typedef {import("webpack").Module} Module */
/** @typedef {import("webpack").AssetInfo} AssetInfo */

/**
* @template T
Expand Down Expand Up @@ -1289,6 +1291,7 @@ async function svgoMinify(original, minimizerOptions) {
};
}

/** @type {WeakMap<Module, AssetInfo>} */
const IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS = new WeakMap();

module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ declare namespace ImageMinimizerPlugin {
Asset,
AssetInfo,
Source,
Module,
ImageminMinifyFunction,
SquooshMinifyFunction,
Rule,
Expand Down Expand Up @@ -95,6 +96,7 @@ type WebpackError = import("webpack").WebpackError;
type Asset = import("webpack").Asset;
type AssetInfo = import("webpack").AssetInfo;
type Source = import("webpack").sources.Source;
type Module = import("webpack").Module;
type ImageminMinifyFunction = typeof imageminMinify;
type SquooshMinifyFunction = typeof squooshMinify;
type Rule = RegExp | string;
Expand Down
7 changes: 6 additions & 1 deletion types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export type WorkerResult = import("./index").WorkerResult;
export type SquooshOptions = import("./index").SquooshOptions;
export type ImageminOptions = import("imagemin").Options;
export type WebpackError = import("webpack").WebpackError;
export type Module = import("webpack").Module;
export type AssetInfo = import("webpack").AssetInfo;
export type Task<T> = () => Promise<T>;
export type SvgoLib = typeof import("svgo");
export type SvgoOptions = {
Expand Down Expand Up @@ -58,6 +60,8 @@ export function isAbsoluteURL(url: string): boolean;
/** @typedef {import("./index").SquooshOptions} SquooshOptions */
/** @typedef {import("imagemin").Options} ImageminOptions */
/** @typedef {import("webpack").WebpackError} WebpackError */
/** @typedef {import("webpack").Module} Module */
/** @typedef {import("webpack").AssetInfo} AssetInfo */
/**
* @template T
* @typedef {() => Promise<T>} Task
Expand Down Expand Up @@ -167,7 +171,8 @@ export function svgoMinify<T>(
original: WorkerResult,
minimizerOptions: T,
): Promise<WorkerResult | null>;
export const IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS: WeakMap<WeakKey, any>;
/** @type {WeakMap<Module, AssetInfo>} */
export const IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS: WeakMap<Module, AssetInfo>;
declare function squooshImagePoolSetup(): void;
declare function squooshImagePoolTeardown(): Promise<void>;
export {};

0 comments on commit 72aa77f

Please sign in to comment.