Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 13, 2021
1 parent 76009b7 commit 2af311b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
19 changes: 11 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,24 @@ import schema from "./options.json";
* @property {Rules} [test]
* @property {Rules} [include]
* @property {Rules} [exclude]
* @property {string | AlgorithmFunction<T>} [algorithm]
* @property {CompressionOptions<T>} [compressionOptions]
* @property {number} [threshold]
* @property {number} [minRatio]
* @property {DeleteOriginalAssets} [deleteOriginalAssets]
* @property {Filename} [filename]
*/

/**
* @typedef {import("zlib").ZlibOptions} ZlibOptions
*/

/**
* @template T
* @typedef {BasePluginOptions<T> & { compressionOptions: CompressionOptions<T>, threshold: number, minRatio: number, deleteOriginalAssets: DeleteOriginalAssets, filename: Filename }} InternalPluginOptions
* @typedef {T extends ZlibOptions ? { algorithm?: string | AlgorithmFunction<T> | undefined, compressionOptions?: CompressionOptions<T> | undefined } : { algorithm: string | AlgorithmFunction<T>, compressionOptions?: CompressionOptions<T> | undefined }} DefinedDefaultAlgorithmAndOptions
*/

/**
* @typedef {import("zlib").ZlibOptions} ZlibOptions
* @template T
* @typedef {BasePluginOptions<T> & { algorithm: string | AlgorithmFunction<T>, compressionOptions: CompressionOptions<T>, threshold: number, minRatio: number, deleteOriginalAssets: DeleteOriginalAssets, filename: Filename }} InternalPluginOptions
*/

/**
Expand All @@ -91,10 +94,10 @@ import schema from "./options.json";
*/
class CompressionPlugin {
/**
* @param {BasePluginOptions<T>} [options]
* @param {BasePluginOptions<T> & DefinedDefaultAlgorithmAndOptions<T>} [options]
*/
constructor(options = {}) {
validate(/** @type {Schema} */ (schema), options, {
constructor(options) {
validate(/** @type {Schema} */ (schema), options || {}, {
name: "Compression Plugin",
baseDataPath: "options",
});
Expand All @@ -109,7 +112,7 @@ class CompressionPlugin {
threshold = 0,
minRatio = 0.8,
deleteOriginalAssets = false,
} = options;
} = options || {};

/**
* @private
Expand Down
32 changes: 23 additions & 9 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,29 @@ export type BasePluginOptions<T> = {
test?: Rules | undefined;
include?: Rules | undefined;
exclude?: Rules | undefined;
algorithm?: string | AlgorithmFunction<T> | undefined;
compressionOptions?: InferDefaultType<T> | undefined;
threshold?: number | undefined;
minRatio?: number | undefined;
deleteOriginalAssets?: DeleteOriginalAssets | undefined;
filename?: Filename | undefined;
};
export type ZlibOptions = import("zlib").ZlibOptions;
export type DefinedDefaultAlgorithmAndOptions<T> = T extends ZlibOptions
? {
algorithm?: string | AlgorithmFunction<T> | undefined;
compressionOptions?: CompressionOptions<T> | undefined;
}
: {
algorithm: string | AlgorithmFunction<T>;
compressionOptions?: CompressionOptions<T> | undefined;
};
export type InternalPluginOptions<T> = BasePluginOptions<T> & {
algorithm: string | AlgorithmFunction<T>;
compressionOptions: CompressionOptions<T>;
threshold: number;
minRatio: number;
deleteOriginalAssets: DeleteOriginalAssets;
filename: Filename;
};
export type ZlibOptions = import("zlib").ZlibOptions;
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compiler} Compiler */
/** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */
Expand Down Expand Up @@ -114,19 +122,21 @@ export type ZlibOptions = import("zlib").ZlibOptions;
* @property {Rules} [test]
* @property {Rules} [include]
* @property {Rules} [exclude]
* @property {string | AlgorithmFunction<T>} [algorithm]
* @property {CompressionOptions<T>} [compressionOptions]
* @property {number} [threshold]
* @property {number} [minRatio]
* @property {DeleteOriginalAssets} [deleteOriginalAssets]
* @property {Filename} [filename]
*/
/**
* @typedef {import("zlib").ZlibOptions} ZlibOptions
*/
/**
* @template T
* @typedef {BasePluginOptions<T> & { compressionOptions: CompressionOptions<T>, threshold: number, minRatio: number, deleteOriginalAssets: DeleteOriginalAssets, filename: Filename }} InternalPluginOptions
* @typedef {T extends ZlibOptions ? { algorithm?: string | AlgorithmFunction<T> | undefined, compressionOptions?: CompressionOptions<T> | undefined } : { algorithm: string | AlgorithmFunction<T>, compressionOptions?: CompressionOptions<T> | undefined }} DefinedDefaultAlgorithmAndOptions
*/
/**
* @typedef {import("zlib").ZlibOptions} ZlibOptions
* @template T
* @typedef {BasePluginOptions<T> & { algorithm: string | AlgorithmFunction<T>, compressionOptions: CompressionOptions<T>, threshold: number, minRatio: number, deleteOriginalAssets: DeleteOriginalAssets, filename: Filename }} InternalPluginOptions
*/
/**
* @template [T=ZlibOptions]
Expand All @@ -136,9 +146,13 @@ declare class CompressionPlugin<T = import("zlib").ZlibOptions>
implements WebpackPluginInstance
{
/**
* @param {BasePluginOptions<T>} [options]
* @param {BasePluginOptions<T> & DefinedDefaultAlgorithmAndOptions<T>} [options]
*/
constructor(options?: BasePluginOptions<T> | undefined);
constructor(
options?:
| (BasePluginOptions<T> & DefinedDefaultAlgorithmAndOptions<T>)
| undefined
);
/**
* @private
* @type {InternalPluginOptions<T>}
Expand Down

0 comments on commit 2af311b

Please sign in to comment.