Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Dec 13, 2021
1 parent bb9d661 commit 76009b7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
17 changes: 12 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import schema from "./options.json";

/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compiler} Compiler */
/** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */
/** @typedef {import("webpack").Compilation} Compilation */
/** @typedef {import("webpack").sources.Source} Source */
/** @typedef {import("webpack").Asset} Asset */
/** @typedef {import("webpack").WebpackError} WebpackError */

/**
* @template T
* @typedef {T | { valueOf(): T }} WithImplicitCoercion
*/

/** @typedef {RegExp | string} Rule */

/** @typedef {Rule[] | Rule} Rules */
Expand All @@ -41,7 +47,7 @@ import schema from "./options.json";
* @callback AlgorithmFunction
* @param {Buffer} input
* @param {CompressionOptions<T>} options
* @param {(error: Error, result: string | Buffer) => void} callback
* @param {(error: Error | null | undefined, result: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer> | Uint8Array | ReadonlyArray<number> | WithImplicitCoercion<Uint8Array | ReadonlyArray<number> | string> | WithImplicitCoercion<string> | { [Symbol.toPrimitive](hint: 'string'): string }) => void} callback
*/

/**
Expand Down Expand Up @@ -81,6 +87,7 @@ import schema from "./options.json";

/**
* @template [T=ZlibOptions]
* @implements WebpackPluginInstance
*/
class CompressionPlugin {
/**
Expand Down Expand Up @@ -195,11 +202,11 @@ class CompressionPlugin {
}

if (!Buffer.isBuffer(result)) {
// eslint-disable-next-line no-param-reassign
result = Buffer.from(result);
// @ts-ignore
resolve(Buffer.from(result));
} else {
resolve(result);
}

resolve(result);
}
);
});
Expand Down
40 changes: 37 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
export default CompressionPlugin;
export type Schema = import("schema-utils/declarations/validate").Schema;
export type Compiler = import("webpack").Compiler;
export type WebpackPluginInstance = import("webpack").WebpackPluginInstance;
export type Compilation = import("webpack").Compilation;
export type Source = import("webpack").sources.Source;
export type Asset = import("webpack").Asset;
export type WebpackError = import("webpack").WebpackError;
export type WithImplicitCoercion<T> =
| T
| {
valueOf(): T;
};
export type Rule = RegExp | string;
export type Rules = Rule[] | Rule;
export type CustomOptions = {
Expand All @@ -16,7 +22,27 @@ export type CompressionOptions<T> = InferDefaultType<T>;
export type AlgorithmFunction<T> = (
input: Buffer,
options: CompressionOptions<T>,
callback: (error: Error, result: string | Buffer) => void
callback: (
error: Error | null | undefined,
result:
| string
| ArrayBuffer
| SharedArrayBuffer
| Uint8Array
| readonly number[]
| {
valueOf(): ArrayBuffer | SharedArrayBuffer;
}
| {
valueOf(): string | Uint8Array | readonly number[];
}
| {
valueOf(): string;
}
| {
[Symbol.toPrimitive](hint: "string"): string;
}
) => void
) => any;
export type PathData = {
[key: string]: any;
Expand Down Expand Up @@ -44,10 +70,15 @@ export type InternalPluginOptions<T> = BasePluginOptions<T> & {
export type ZlibOptions = import("zlib").ZlibOptions;
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compiler} Compiler */
/** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */
/** @typedef {import("webpack").Compilation} Compilation */
/** @typedef {import("webpack").sources.Source} Source */
/** @typedef {import("webpack").Asset} Asset */
/** @typedef {import("webpack").WebpackError} WebpackError */
/**
* @template T
* @typedef {T | { valueOf(): T }} WithImplicitCoercion
*/
/** @typedef {RegExp | string} Rule */
/** @typedef {Rule[] | Rule} Rules */
/**
Expand All @@ -66,7 +97,7 @@ export type ZlibOptions = import("zlib").ZlibOptions;
* @callback AlgorithmFunction
* @param {Buffer} input
* @param {CompressionOptions<T>} options
* @param {(error: Error, result: string | Buffer) => void} callback
* @param {(error: Error | null | undefined, result: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer> | Uint8Array | ReadonlyArray<number> | WithImplicitCoercion<Uint8Array | ReadonlyArray<number> | string> | WithImplicitCoercion<string> | { [Symbol.toPrimitive](hint: 'string'): string }) => void} callback
*/
/**
* @typedef {{[key: string]: any}} PathData
Expand Down Expand Up @@ -99,8 +130,11 @@ export type ZlibOptions = import("zlib").ZlibOptions;
*/
/**
* @template [T=ZlibOptions]
* @implements WebpackPluginInstance
*/
declare class CompressionPlugin<T = import("zlib").ZlibOptions> {
declare class CompressionPlugin<T = import("zlib").ZlibOptions>
implements WebpackPluginInstance
{
/**
* @param {BasePluginOptions<T>} [options]
*/
Expand Down

0 comments on commit 76009b7

Please sign in to comment.