forked from webpack/tapable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tapable.d.ts
362 lines (311 loc) · 9.51 KB
/
tapable.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
type FixedSizeArray<T extends number, U> = T extends 0
? void[]
: ReadonlyArray<U> & {
0: U
length: T
}
type Measure<T extends number> = T extends 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
? T
: never
type Append<T extends any[], U> = {
0: [U]
1: [T[0], U]
2: [T[0], T[1], U]
3: [T[0], T[1], T[2], U]
4: [T[0], T[1], T[2], T[3], U]
5: [T[0], T[1], T[2], T[3], T[4], U]
6: [T[0], T[1], T[2], T[3], T[4], T[5], U]
7: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], U]
8: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], U]
}[Measure<T['length']>]
type AsArray<T> = T extends any[] ? T : [T]
declare class UnsetAdditionalOptions {
_UnsetAdditionalOptions: true
}
type IfSet<X> = X extends UnsetAdditionalOptions ? {} : X
type Callback<E, T> = (error: E | null, result?: T) => void
type InnerCallback<E, T> = (error?: E | null | false, result?: T) => void
type FullTap = Tap & {
type: 'sync' | 'async' | 'promise'
fn: Function
}
type Tap = TapOptions & {
name: string
}
type TapOptions = {
before?: string
stage?: number
}
interface HookInterceptor<T, R, AdditionalOptions = UnsetAdditionalOptions> {
name?: string
tap?: (tap: FullTap & IfSet<AdditionalOptions>) => void
call?: (...args: any[]) => void
loop?: (...args: any[]) => void
error?: (err: Error) => void
result?: (result: R) => void
done?: () => void
register?: (
tap: FullTap & IfSet<AdditionalOptions>,
) => FullTap & IfSet<AdditionalOptions>
}
type ArgumentNames<T extends any[]> = FixedSizeArray<T['length'], string>
declare class Hook<T, R, AdditionalOptions = UnsetAdditionalOptions> {
constructor(args?: ArgumentNames<AsArray<T>>, name?: string)
name: string | undefined
taps: FullTap[]
intercept(interceptor: HookInterceptor<T, R, AdditionalOptions>): void
isUsed(): boolean
callAsync(...args: Append<AsArray<T>, Callback<Error, R>>): void
promise(...args: AsArray<T>): Promise<R>
tap(
options: string | (Tap & IfSet<AdditionalOptions>),
fn: (...args: AsArray<T>) => R,
): void
withOptions(
options: TapOptions & IfSet<AdditionalOptions>,
): Omit<this, 'call' | 'callAsync' | 'promise'>
}
export class SyncHook<
T,
R = void,
AdditionalOptions = UnsetAdditionalOptions,
> extends Hook<T, R, AdditionalOptions> {
call(...args: AsArray<T>): R
}
export class SyncBailHook<
T,
R,
AdditionalOptions = UnsetAdditionalOptions,
> extends SyncHook<T, R, AdditionalOptions> {}
export class SyncLoopHook<
T,
AdditionalOptions = UnsetAdditionalOptions,
> extends SyncHook<T, void, AdditionalOptions> {}
export class SyncWaterfallHook<
T,
AdditionalOptions = UnsetAdditionalOptions,
> extends SyncHook<T, AsArray<T>[0], AdditionalOptions> {}
declare class AsyncHook<
T,
R,
AdditionalOptions = UnsetAdditionalOptions,
> extends Hook<T, R, AdditionalOptions> {
tapAsync(
options: string | (Tap & IfSet<AdditionalOptions>),
fn: (...args: Append<AsArray<T>, InnerCallback<Error, R>>) => void,
): void
tapPromise(
options: string | (Tap & IfSet<AdditionalOptions>),
fn: (...args: AsArray<T>) => Promise<R>,
): void
}
export class AsyncParallelHook<
T,
AdditionalOptions = UnsetAdditionalOptions,
> extends AsyncHook<T, void, AdditionalOptions> {}
export class AsyncParallelBailHook<
T,
R,
AdditionalOptions = UnsetAdditionalOptions,
> extends AsyncHook<T, R, AdditionalOptions> {}
export class AsyncSeriesHook<
T,
AdditionalOptions = UnsetAdditionalOptions,
> extends AsyncHook<T, void, AdditionalOptions> {}
export class AsyncSeriesBailHook<
T,
R,
AdditionalOptions = UnsetAdditionalOptions,
> extends AsyncHook<T, R, AdditionalOptions> {}
export class AsyncSeriesLoopHook<
T,
AdditionalOptions = UnsetAdditionalOptions,
> extends AsyncHook<T, void, AdditionalOptions> {}
export class AsyncSeriesWaterfallHook<
T,
AdditionalOptions = UnsetAdditionalOptions,
> extends AsyncHook<T, AsArray<T>[0], AdditionalOptions> {}
type HookFactory<H> = (key: any, hook?: H) => H
interface HookMapInterceptor<H> {
factory?: HookFactory<H>
}
export class HookMap<H> {
constructor(factory: HookFactory<H>, name?: string)
name: string | undefined
get(key: any): H | undefined
for(key: any): H
intercept(interceptor: HookMapInterceptor<H>): void
}
export class MultiHook<H> {
constructor(hooks: H[], name?: string)
name: string | undefined
tap(options: string | Tap, fn?: Function): void
tapAsync(options: string | Tap, fn?: Function): void
tapPromise(options: string | Tap, fn?: Function): void
}
import * as util from 'util'
const deprecateContext = util.deprecate(
() => {},
'Hook.context is deprecated and will be removed',
)
type CallDelegate = (...args: any[]) => any
const CALL_DELEGATE: CallDelegate = function (this: Hook, ...args: any[]) {
this.call = this._createCall('sync')
return this.call(...args)
}
const CALL_ASYNC_DELEGATE: CallDelegate = function (
this: Hook,
...args: any[]
) {
this.callAsync = this._createCall('async')
return this.callAsync(...args)
}
const PROMISE_DELEGATE: CallDelegate = function (this: Hook, ...args: any[]) {
this.promise = this._createCall('promise')
return this.promise(...args)
}
interface TapOptions {
name?: string
before?: string | string[]
stage?: number
type?: string
fn?: Function
context?: any
}
interface Interceptor {
register?: (options: TapOptions) => TapOptions
}
class Hook {
private _args: any[]
public name?: string;
public taps: TapOptions[] = [];
public interceptors: Interceptor[] = [];
private _call: CallDelegate = CALL_DELEGATE;
public call: CallDelegate = CALL_DELEGATE;
private _callAsync: CallDelegate = CALL_ASYNC_DELEGATE;
public callAsync: CallDelegate = CALL_ASYNC_DELEGATE;
private _promise: CallDelegate = PROMISE_DELEGATE;
public promise: CallDelegate = PROMISE_DELEGATE;
private _x?: any;
constructor(args: any[] = [], name?: string) {
this._args = args;
this.name = name;
this.compile = this.compile;
this.tap = this.tap;
this.tapAsync = this.tapAsync;
this.tapPromise = this.tapPromise;
}
compile(options: {
taps: TapOptions[];
interceptors: Interceptor[];
args: any[];
type: string;
}): never {
throw new Error('Abstract: should be overridden');
}
private _createCall(type: string) {
return this.compile({
taps: this.taps,
interceptors: this.interceptors,
args: this._args,
type: type,
});
}
private _tap(type: string, options: string | TapOptions, fn: Function) {
let _options: TapOptions = typeof options === 'string' ? { name: options.trim() } : options;
if (!_options || typeof _options !== 'object' || _options === null) {
throw new Error('Invalid tap options');
}
if (!_options.name || _options.name === '') {
throw new Error('Missing name for tap');
}
if (_options.context !== undefined) {
deprecateContext();
}
_options = { ..._options, type, fn };
_options = this._runRegisterInterceptors(_options);
this._insert(_options);
}
tap(options: string | TapOptions, fn: Function) {
this._tap('sync', options, fn);
}
tapAsync(options: string | TapOptions, fn: Function) {
this._tap('async', options, fn);
}
tapPromise(options: string | TapOptions, fn: Function) {
this._tap('promise', options, fn);
}
private _runRegisterInterceptors(options: TapOptions): TapOptions {
for (const interceptor of this.interceptors) {
if (interceptor.register) {
const newOptions = interceptor.register(options);
if (newOptions !== undefined) {
options = newOptions;
}
}
}
return options;
}
withOptions(options: TapOptions) {
const mergeOptions = (opt: string | TapOptions) =>
typeof opt === 'string' ? { ...options, name: opt } : { ...options, ...opt };
return {
name: this.name,
tap: (opt: string | TapOptions, fn: Function) => this.tap(mergeOptions(opt), fn),
tapAsync: (opt: string | TapOptions, fn: Function) => this.tapAsync(mergeOptions(opt), fn),
tapPromise: (opt: string | TapOptions, fn: Function) => this.tapPromise(mergeOptions(opt), fn),
intercept: (interceptor: Interceptor) => this.intercept(interceptor),
isUsed: () => this.isUsed(),
withOptions: (opt: string | TapOptions) => this.withOptions(mergeOptions(opt)),
};
}
isUsed() {
return this.taps.length > 0 || this.interceptors.length > 0;
}
intercept(interceptor: Interceptor) {
this._resetCompilation();
this.interceptors.push({ ...interceptor });
if (interceptor.register) {
for (let i = 0; i < this.taps.length; i++) {
this.taps[i] = interceptor.register(this.taps[i])!;
}
}
}
private _resetCompilation() {
this.call = this._call;
this.callAsync = this._callAsync;
this.promise = this._promise;
}
private _insert(item: TapOptions) {
this._resetCompilation();
let before: Set<string> | undefined;
if (typeof item.before === 'string') {
before = new Set([item.before]);
} else if (Array.isArray(item.before)) {
before = new Set(item.before);
}
const stage = item.stage || 0;
let i = this.taps.length;
while (i > 0) {
i--;
const x = this.taps[i];
this.taps[i + 1] = x;
const xStage = x.stage || 0;
if (before?.has(x.name!)) {
before.delete(x.name!);
continue;
}
if (before && before.size > 0) {
continue;
}
if (xStage > stage) {
continue;
}
i++;
break;
}
this.taps[i] = item;
}
}
Object.setPrototypeOf(Hook.prototype, null)
export default Hook