Skip to content

Commit 76009b7

Browse files
fix: types
1 parent bb9d661 commit 76009b7

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@ import schema from "./options.json";
1313

1414
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
1515
/** @typedef {import("webpack").Compiler} Compiler */
16+
/** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */
1617
/** @typedef {import("webpack").Compilation} Compilation */
1718
/** @typedef {import("webpack").sources.Source} Source */
1819
/** @typedef {import("webpack").Asset} Asset */
1920
/** @typedef {import("webpack").WebpackError} WebpackError */
2021

22+
/**
23+
* @template T
24+
* @typedef {T | { valueOf(): T }} WithImplicitCoercion
25+
*/
26+
2127
/** @typedef {RegExp | string} Rule */
2228

2329
/** @typedef {Rule[] | Rule} Rules */
@@ -41,7 +47,7 @@ import schema from "./options.json";
4147
* @callback AlgorithmFunction
4248
* @param {Buffer} input
4349
* @param {CompressionOptions<T>} options
44-
* @param {(error: Error, result: string | Buffer) => void} callback
50+
* @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
4551
*/
4652

4753
/**
@@ -81,6 +87,7 @@ import schema from "./options.json";
8187

8288
/**
8389
* @template [T=ZlibOptions]
90+
* @implements WebpackPluginInstance
8491
*/
8592
class CompressionPlugin {
8693
/**
@@ -195,11 +202,11 @@ class CompressionPlugin {
195202
}
196203

197204
if (!Buffer.isBuffer(result)) {
198-
// eslint-disable-next-line no-param-reassign
199-
result = Buffer.from(result);
205+
// @ts-ignore
206+
resolve(Buffer.from(result));
207+
} else {
208+
resolve(result);
200209
}
201-
202-
resolve(result);
203210
}
204211
);
205212
});

types/index.d.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
export default CompressionPlugin;
33
export type Schema = import("schema-utils/declarations/validate").Schema;
44
export type Compiler = import("webpack").Compiler;
5+
export type WebpackPluginInstance = import("webpack").WebpackPluginInstance;
56
export type Compilation = import("webpack").Compilation;
67
export type Source = import("webpack").sources.Source;
78
export type Asset = import("webpack").Asset;
89
export type WebpackError = import("webpack").WebpackError;
10+
export type WithImplicitCoercion<T> =
11+
| T
12+
| {
13+
valueOf(): T;
14+
};
915
export type Rule = RegExp | string;
1016
export type Rules = Rule[] | Rule;
1117
export type CustomOptions = {
@@ -16,7 +22,27 @@ export type CompressionOptions<T> = InferDefaultType<T>;
1622
export type AlgorithmFunction<T> = (
1723
input: Buffer,
1824
options: CompressionOptions<T>,
19-
callback: (error: Error, result: string | Buffer) => void
25+
callback: (
26+
error: Error | null | undefined,
27+
result:
28+
| string
29+
| ArrayBuffer
30+
| SharedArrayBuffer
31+
| Uint8Array
32+
| readonly number[]
33+
| {
34+
valueOf(): ArrayBuffer | SharedArrayBuffer;
35+
}
36+
| {
37+
valueOf(): string | Uint8Array | readonly number[];
38+
}
39+
| {
40+
valueOf(): string;
41+
}
42+
| {
43+
[Symbol.toPrimitive](hint: "string"): string;
44+
}
45+
) => void
2046
) => any;
2147
export type PathData = {
2248
[key: string]: any;
@@ -44,10 +70,15 @@ export type InternalPluginOptions<T> = BasePluginOptions<T> & {
4470
export type ZlibOptions = import("zlib").ZlibOptions;
4571
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
4672
/** @typedef {import("webpack").Compiler} Compiler */
73+
/** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */
4774
/** @typedef {import("webpack").Compilation} Compilation */
4875
/** @typedef {import("webpack").sources.Source} Source */
4976
/** @typedef {import("webpack").Asset} Asset */
5077
/** @typedef {import("webpack").WebpackError} WebpackError */
78+
/**
79+
* @template T
80+
* @typedef {T | { valueOf(): T }} WithImplicitCoercion
81+
*/
5182
/** @typedef {RegExp | string} Rule */
5283
/** @typedef {Rule[] | Rule} Rules */
5384
/**
@@ -66,7 +97,7 @@ export type ZlibOptions = import("zlib").ZlibOptions;
6697
* @callback AlgorithmFunction
6798
* @param {Buffer} input
6899
* @param {CompressionOptions<T>} options
69-
* @param {(error: Error, result: string | Buffer) => void} callback
100+
* @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
70101
*/
71102
/**
72103
* @typedef {{[key: string]: any}} PathData
@@ -99,8 +130,11 @@ export type ZlibOptions = import("zlib").ZlibOptions;
99130
*/
100131
/**
101132
* @template [T=ZlibOptions]
133+
* @implements WebpackPluginInstance
102134
*/
103-
declare class CompressionPlugin<T = import("zlib").ZlibOptions> {
135+
declare class CompressionPlugin<T = import("zlib").ZlibOptions>
136+
implements WebpackPluginInstance
137+
{
104138
/**
105139
* @param {BasePluginOptions<T>} [options]
106140
*/

0 commit comments

Comments
 (0)