diff --git a/src/index.js b/src/index.js index 554dd78..5b1c3ac 100644 --- a/src/index.js +++ b/src/index.js @@ -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 */ @@ -41,7 +47,7 @@ import schema from "./options.json"; * @callback AlgorithmFunction * @param {Buffer} input * @param {CompressionOptions} options - * @param {(error: Error, result: string | Buffer) => void} callback + * @param {(error: Error | null | undefined, result: WithImplicitCoercion | Uint8Array | ReadonlyArray | WithImplicitCoercion | string> | WithImplicitCoercion | { [Symbol.toPrimitive](hint: 'string'): string }) => void} callback */ /** @@ -81,6 +87,7 @@ import schema from "./options.json"; /** * @template [T=ZlibOptions] + * @implements WebpackPluginInstance */ class CompressionPlugin { /** @@ -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); } ); }); diff --git a/types/index.d.ts b/types/index.d.ts index 7f960cd..686ece8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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 + | { + valueOf(): T; + }; export type Rule = RegExp | string; export type Rules = Rule[] | Rule; export type CustomOptions = { @@ -16,7 +22,27 @@ export type CompressionOptions = InferDefaultType; export type AlgorithmFunction = ( input: Buffer, options: CompressionOptions, - 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; @@ -44,10 +70,15 @@ export type InternalPluginOptions = BasePluginOptions & { 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 */ /** @@ -66,7 +97,7 @@ export type ZlibOptions = import("zlib").ZlibOptions; * @callback AlgorithmFunction * @param {Buffer} input * @param {CompressionOptions} options - * @param {(error: Error, result: string | Buffer) => void} callback + * @param {(error: Error | null | undefined, result: WithImplicitCoercion | Uint8Array | ReadonlyArray | WithImplicitCoercion | string> | WithImplicitCoercion | { [Symbol.toPrimitive](hint: 'string'): string }) => void} callback */ /** * @typedef {{[key: string]: any}} PathData @@ -99,8 +130,11 @@ export type ZlibOptions = import("zlib").ZlibOptions; */ /** * @template [T=ZlibOptions] + * @implements WebpackPluginInstance */ -declare class CompressionPlugin { +declare class CompressionPlugin + implements WebpackPluginInstance +{ /** * @param {BasePluginOptions} [options] */