tapable.d.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. type FixedSizeArray<T extends number, U> = T extends 0
  2. ? void[]
  3. : ReadonlyArray<U> & {
  4. 0: U;
  5. length: T;
  6. };
  7. type Measure<T extends number> = T extends 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
  8. ? T
  9. : never;
  10. type Append<T extends any[], U> = {
  11. 0: [U];
  12. 1: [T[0], U];
  13. 2: [T[0], T[1], U];
  14. 3: [T[0], T[1], T[2], U];
  15. 4: [T[0], T[1], T[2], T[3], U];
  16. 5: [T[0], T[1], T[2], T[3], T[4], U];
  17. 6: [T[0], T[1], T[2], T[3], T[4], T[5], U];
  18. 7: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], U];
  19. 8: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], U];
  20. }[Measure<T["length"]>];
  21. type AsArray<T> = T extends any[] ? T : [T];
  22. declare class UnsetAdditionalOptions {
  23. _UnsetAdditionalOptions: true
  24. }
  25. type IfSet<X> = X extends UnsetAdditionalOptions ? {} : X;
  26. type Callback<E, T> = (error: E | null, result?: T) => void;
  27. type InnerCallback<E, T> = (error?: E | null | false, result?: T) => void;
  28. type FullTap = Tap & {
  29. type: "sync" | "async" | "promise",
  30. fn: Function
  31. }
  32. type Tap = TapOptions & {
  33. name: string;
  34. };
  35. type TapOptions = {
  36. before?: string;
  37. stage?: number;
  38. };
  39. interface HookInterceptor<T, R, AdditionalOptions = UnsetAdditionalOptions> {
  40. name?: string;
  41. tap?: (tap: FullTap & IfSet<AdditionalOptions>) => void;
  42. call?: (...args: any[]) => void;
  43. loop?: (...args: any[]) => void;
  44. error?: (err: Error) => void;
  45. result?: (result: R) => void;
  46. done?: () => void;
  47. register?: (tap: FullTap & IfSet<AdditionalOptions>) => FullTap & IfSet<AdditionalOptions>;
  48. }
  49. type ArgumentNames<T extends any[]> = FixedSizeArray<T["length"], string>;
  50. declare class Hook<T, R, AdditionalOptions = UnsetAdditionalOptions> {
  51. constructor(args?: ArgumentNames<AsArray<T>>, name?: string);
  52. name: string | undefined;
  53. taps: FullTap[];
  54. intercept(interceptor: HookInterceptor<T, R, AdditionalOptions>): void;
  55. isUsed(): boolean;
  56. callAsync(...args: Append<AsArray<T>, Callback<Error, R>>): void;
  57. promise(...args: AsArray<T>): Promise<R>;
  58. tap(options: string | Tap & IfSet<AdditionalOptions>, fn: (...args: AsArray<T>) => R): void;
  59. withOptions(options: TapOptions & IfSet<AdditionalOptions>): Omit<this, "call" | "callAsync" | "promise">;
  60. }
  61. export class SyncHook<T, R = void, AdditionalOptions = UnsetAdditionalOptions> extends Hook<T, R, AdditionalOptions> {
  62. call(...args: AsArray<T>): R;
  63. }
  64. export class SyncBailHook<T, R, AdditionalOptions = UnsetAdditionalOptions> extends SyncHook<T, R, AdditionalOptions> {}
  65. export class SyncLoopHook<T, AdditionalOptions = UnsetAdditionalOptions> extends SyncHook<T, void, AdditionalOptions> {}
  66. export class SyncWaterfallHook<T, AdditionalOptions = UnsetAdditionalOptions> extends SyncHook<T, AsArray<T>[0], AdditionalOptions> {}
  67. declare class AsyncHook<T, R, AdditionalOptions = UnsetAdditionalOptions> extends Hook<T, R, AdditionalOptions> {
  68. tapAsync(
  69. options: string | Tap & IfSet<AdditionalOptions>,
  70. fn: (...args: Append<AsArray<T>, InnerCallback<Error, R>>) => void
  71. ): void;
  72. tapPromise(
  73. options: string | Tap & IfSet<AdditionalOptions>,
  74. fn: (...args: AsArray<T>) => Promise<R>
  75. ): void;
  76. }
  77. export class AsyncParallelHook<T, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, void, AdditionalOptions> {}
  78. export class AsyncParallelBailHook<T, R, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, R, AdditionalOptions> {}
  79. export class AsyncSeriesHook<T, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, void, AdditionalOptions> {}
  80. export class AsyncSeriesBailHook<T, R, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, R, AdditionalOptions> {}
  81. export class AsyncSeriesLoopHook<T, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, void, AdditionalOptions> {}
  82. export class AsyncSeriesWaterfallHook<T, AdditionalOptions = UnsetAdditionalOptions> extends AsyncHook<T, AsArray<T>[0], AdditionalOptions> {}
  83. type HookFactory<H> = (key: any, hook?: H) => H;
  84. interface HookMapInterceptor<H> {
  85. factory?: HookFactory<H>;
  86. }
  87. export class HookMap<H> {
  88. constructor(factory: HookFactory<H>, name?: string);
  89. name: string | undefined;
  90. get(key: any): H | undefined;
  91. for(key: any): H;
  92. intercept(interceptor: HookMapInterceptor<H>): void;
  93. }
  94. export class MultiHook<H> {
  95. constructor(hooks: H[], name?: string);
  96. name: string | undefined;
  97. tap(options: string | Tap, fn?: Function): void;
  98. tapAsync(options: string | Tap, fn?: Function): void;
  99. tapPromise(options: string | Tap, fn?: Function): void;
  100. }