UNPKG

9.58 kBTypeScriptView Raw
1export = ImageMinimizerPlugin;
2/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
3/** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */
4/** @typedef {import("webpack").Compiler} Compiler */
5/** @typedef {import("webpack").Compilation} Compilation */
6/** @typedef {import("webpack").WebpackError} WebpackError */
7/** @typedef {import("webpack").Asset} Asset */
8/** @typedef {import("webpack").AssetInfo} AssetInfo */
9/** @typedef {import("webpack").sources.Source} Source */
10/** @typedef {import("./utils.js").imageminMinify} ImageminMinifyFunction */
11/** @typedef {import("./utils.js").squooshMinify} SquooshMinifyFunction */
12/** @typedef {RegExp | string} Rule */
13/** @typedef {Rule[] | Rule} Rules */
14/**
15 * @callback FilterFn
16 * @param {Buffer} source `Buffer` of source file.
17 * @param {string} sourcePath Absolute path to source.
18 * @returns {boolean}
19 */
20/**
21 * @typedef {Object} ImageminOptions
22 * @property {Array<string | [string, Record<string, any>?] | import("imagemin").Plugin>} plugins
23 */
24/**
25 * @typedef {Object.<string, any>} SquooshOptions
26 */
27/**
28 * @typedef {Object} WorkerResult
29 * @property {string} filename
30 * @property {Buffer} data
31 * @property {Array<Error>} warnings
32 * @property {Array<Error>} errors
33 * @property {AssetInfo} info
34 */
35/**
36 * @template T
37 * @typedef {Object} Task
38 * @property {string} name
39 * @property {AssetInfo} info
40 * @property {Source} inputSource
41 * @property {WorkerResult & { source?: Source } | undefined} output
42 * @property {ReturnType<ReturnType<Compilation["getCache"]>["getItemCache"]>} cacheItem
43 * @property {Transformer<T> | Transformer<T>[]} transformer
44 */
45/**
46 * @typedef {{ [key: string]: any }} CustomOptions
47 */
48/**
49 * @template T
50 * @typedef {T extends infer U ? U : CustomOptions} InferDefaultType
51 */
52/**
53 * @template T
54 * @typedef {InferDefaultType<T> | undefined} BasicTransformerOptions
55 */
56/**
57 * @template T
58 * @callback BasicTransformerImplementation
59 * @param {WorkerResult} original
60 * @param {BasicTransformerOptions<T>} [options]
61 * @returns {Promise<WorkerResult>}
62 */
63/**
64 * @typedef {object} BasicTransformerHelpers
65 * @property {() => {}} [setup]
66 * @property {() => {}} [teardown]
67 */
68/**
69 * @template T
70 * @typedef {BasicTransformerImplementation<T> & BasicTransformerHelpers} TransformerFunction
71 */
72/**
73 * @typedef {Object} PathData
74 * @property {string} [filename]
75 */
76/**
77 * @callback FilenameFn
78 * @param {PathData} pathData
79 * @param {AssetInfo} [assetInfo]
80 * @returns {string}
81 */
82/**
83 * @template T
84 * @typedef {Object} Transformer
85 * @property {TransformerFunction<T>} implementation
86 * @property {BasicTransformerOptions<T>} [options]
87 * @property {FilterFn} [filter]
88 * @property {string | FilenameFn} [filename]
89 * @property {string} [preset]
90 * @property {"import" | "asset"} [type]
91 */
92/**
93 * @template T
94 * @typedef {Omit<Transformer<T>, "preset" | "type">} Minimizer
95 */
96/**
97 * @template T
98 * @typedef {Transformer<T>} Generator
99 */
100/**
101 * @template T
102 * @typedef {Object} InternalWorkerOptions
103 * @property {string} filename
104 * @property {Buffer} input
105 * @property {Transformer<T> | Transformer<T>[]} transformer
106 * @property {string} [severityError]
107 * @property {Function} [generateFilename]
108 */
109/**
110 * @template T
111 * @typedef {import("./loader").LoaderOptions<T>} InternalLoaderOptions
112 */
113/**
114 * @template T, G
115 * @typedef {Object} PluginOptions
116 * @property {Rules} [test] Test to match files against.
117 * @property {Rules} [include] Files to include.
118 * @property {Rules} [exclude] Files to exclude.
119 * @property {T extends any[] ? { [P in keyof T]: Minimizer<T[P]> } : Minimizer<T> | Minimizer<T>[]} [minimizer] Allows to setup the minimizer.
120 * @property {G extends any[] ? { [P in keyof G]: Generator<G[P]> } : Generator<G>[]} [generator] Allows to set the generator.
121 * @property {boolean} [loader] Automatically adding `imagemin-loader`.
122 * @property {number} [concurrency] Maximum number of concurrency optimization processes in one time.
123 * @property {string} [severityError] Allows to choose how errors are displayed.
124 * @property {boolean} [deleteOriginalAssets] Allows to remove original assets. Useful for converting to a `webp` and remove original assets.
125 */
126/**
127 * @template T, [G=T]
128 * @extends {WebpackPluginInstance}
129 */
130declare class ImageMinimizerPlugin<T, G = T> {
131 /**
132 * @param {PluginOptions<T, G>} [options={}] Plugin options.
133 */
134 constructor(options?: PluginOptions<T, G> | undefined);
135 /**
136 * @private
137 */
138 private options;
139 /**
140 * @private
141 * @param {Compiler} compiler
142 * @param {Compilation} compilation
143 * @param {Record<string, Source>} assets
144 * @returns {Promise<void>}
145 */
146 private optimize;
147 /**
148 * @private
149 */
150 private setupAll;
151 /**
152 * @private
153 */
154 private teardownAll;
155 /**
156 * @param {import("webpack").Compiler} compiler
157 */
158 apply(compiler: import("webpack").Compiler): void;
159}
160declare namespace ImageMinimizerPlugin {
161 export {
162 loader,
163 imageminNormalizeConfig,
164 imageminMinify,
165 imageminGenerate,
166 squooshMinify,
167 squooshGenerate,
168 Schema,
169 WebpackPluginInstance,
170 Compiler,
171 Compilation,
172 WebpackError,
173 Asset,
174 AssetInfo,
175 Source,
176 ImageminMinifyFunction,
177 SquooshMinifyFunction,
178 Rule,
179 Rules,
180 FilterFn,
181 ImageminOptions,
182 SquooshOptions,
183 WorkerResult,
184 Task,
185 CustomOptions,
186 InferDefaultType,
187 BasicTransformerOptions,
188 BasicTransformerImplementation,
189 BasicTransformerHelpers,
190 TransformerFunction,
191 PathData,
192 FilenameFn,
193 Transformer,
194 Minimizer,
195 Generator,
196 InternalWorkerOptions,
197 InternalLoaderOptions,
198 PluginOptions,
199 };
200}
201type PluginOptions<T, G> = {
202 /**
203 * Test to match files against.
204 */
205 test?: Rules | undefined;
206 /**
207 * Files to include.
208 */
209 include?: Rules | undefined;
210 /**
211 * Files to exclude.
212 */
213 exclude?: Rules | undefined;
214 /**
215 * Allows to setup the minimizer.
216 */
217 minimizer?:
218 | (T extends any[]
219 ? { [P in keyof T]: Minimizer<T[P]> }
220 : Minimizer<T> | Minimizer<T>[])
221 | undefined;
222 /**
223 * Allows to set the generator.
224 */
225 generator?:
226 | (G extends any[]
227 ? { [P_1 in keyof G]: Generator<G[P_1]> }
228 : Generator<G>[])
229 | undefined;
230 /**
231 * Automatically adding `imagemin-loader`.
232 */
233 loader?: boolean | undefined;
234 /**
235 * Maximum number of concurrency optimization processes in one time.
236 */
237 concurrency?: number | undefined;
238 /**
239 * Allows to choose how errors are displayed.
240 */
241 severityError?: string | undefined;
242 /**
243 * Allows to remove original assets. Useful for converting to a `webp` and remove original assets.
244 */
245 deleteOriginalAssets?: boolean | undefined;
246};
247declare var loader: string;
248import { imageminNormalizeConfig } from "./utils.js";
249import { imageminMinify } from "./utils.js";
250import { imageminGenerate } from "./utils.js";
251import { squooshMinify } from "./utils.js";
252import { squooshGenerate } from "./utils.js";
253type Schema = import("schema-utils/declarations/validate").Schema;
254type WebpackPluginInstance = import("webpack").WebpackPluginInstance;
255type Compiler = import("webpack").Compiler;
256type Compilation = import("webpack").Compilation;
257type WebpackError = import("webpack").WebpackError;
258type Asset = import("webpack").Asset;
259type AssetInfo = import("webpack").AssetInfo;
260type Source = import("webpack").sources.Source;
261type ImageminMinifyFunction = typeof imageminMinify;
262type SquooshMinifyFunction = typeof squooshMinify;
263type Rule = RegExp | string;
264type Rules = Rule[] | Rule;
265type FilterFn = (source: Buffer, sourcePath: string) => boolean;
266type ImageminOptions = {
267 plugins: Array<
268 string | [string, Record<string, any>?] | import("imagemin").Plugin
269 >;
270};
271type SquooshOptions = {
272 [x: string]: any;
273};
274type WorkerResult = {
275 filename: string;
276 data: Buffer;
277 warnings: Array<Error>;
278 errors: Array<Error>;
279 info: AssetInfo;
280};
281type Task<T> = {
282 name: string;
283 info: AssetInfo;
284 inputSource: Source;
285 output:
286 | (WorkerResult & {
287 source?: Source;
288 })
289 | undefined;
290 cacheItem: ReturnType<ReturnType<Compilation["getCache"]>["getItemCache"]>;
291 transformer: Transformer<T> | Transformer<T>[];
292};
293type CustomOptions = {
294 [key: string]: any;
295};
296type InferDefaultType<T> = T extends infer U ? U : CustomOptions;
297type BasicTransformerOptions<T> = InferDefaultType<T> | undefined;
298type BasicTransformerImplementation<T> = (
299 original: WorkerResult,
300 options?: BasicTransformerOptions<T>
301) => Promise<WorkerResult>;
302type BasicTransformerHelpers = {
303 setup?: (() => {}) | undefined;
304 teardown?: (() => {}) | undefined;
305};
306type TransformerFunction<T> = BasicTransformerImplementation<T> &
307 BasicTransformerHelpers;
308type PathData = {
309 filename?: string | undefined;
310};
311type FilenameFn = (
312 pathData: PathData,
313 assetInfo?: import("webpack").AssetInfo | undefined
314) => string;
315type Transformer<T> = {
316 implementation: TransformerFunction<T>;
317 options?: BasicTransformerOptions<T>;
318 filter?: FilterFn | undefined;
319 filename?: string | FilenameFn | undefined;
320 preset?: string | undefined;
321 type?: "import" | "asset" | undefined;
322};
323type Minimizer<T> = Omit<Transformer<T>, "preset" | "type">;
324type Generator<T> = Transformer<T>;
325type InternalWorkerOptions<T> = {
326 filename: string;
327 input: Buffer;
328 transformer: Transformer<T> | Transformer<T>[];
329 severityError?: string | undefined;
330 generateFilename?: Function | undefined;
331};
332type InternalLoaderOptions<T> = import("./loader").LoaderOptions<T>;