index.d.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* (c) 2015 Ari Porad (@ariporad) <http://ariporad.com>. License: ariporad.mit-license.org */
  2. /**
  3. * The hook. Accepts the code of the module and the filename.
  4. */
  5. declare type Hook = (code: string, filename: string) => string;
  6. /**
  7. * A matcher function, will be called with path to a file.
  8. *
  9. * Should return truthy if the file should be hooked, falsy otherwise.
  10. */
  11. declare type Matcher = (path: string) => boolean;
  12. /**
  13. * Reverts the hook when called.
  14. */
  15. declare type RevertFunction = () => void;
  16. interface Options {
  17. /**
  18. * The extensions to hook. Should start with '.' (ex. ['.js']).
  19. *
  20. * Takes precedence over `exts`, `extension` and `ext`.
  21. *
  22. * @alias exts
  23. * @alias extension
  24. * @alias ext
  25. * @default ['.js']
  26. */
  27. extensions?: ReadonlyArray<string> | string;
  28. /**
  29. * The extensions to hook. Should start with '.' (ex. ['.js']).
  30. *
  31. * Takes precedence over `extension` and `ext`.
  32. *
  33. * @alias extension
  34. * @alias ext
  35. * @default ['.js']
  36. */
  37. exts?: ReadonlyArray<string> | string;
  38. /**
  39. * The extensions to hook. Should start with '.' (ex. ['.js']).
  40. *
  41. * Takes precedence over `ext`.
  42. *
  43. * @alias ext
  44. * @default ['.js']
  45. */
  46. extension?: ReadonlyArray<string> | string;
  47. /**
  48. * The extensions to hook. Should start with '.' (ex. ['.js']).
  49. *
  50. * @default ['.js']
  51. */
  52. ext?: ReadonlyArray<string> | string;
  53. /**
  54. * A matcher function, will be called with path to a file.
  55. *
  56. * Should return truthy if the file should be hooked, falsy otherwise.
  57. */
  58. matcher?: Matcher | null;
  59. /**
  60. * Auto-ignore node_modules. Independent of any matcher.
  61. *
  62. * @default true
  63. */
  64. ignoreNodeModules?: boolean;
  65. }
  66. /**
  67. * Add a require hook.
  68. *
  69. * @param hook The hook. Accepts the code of the module and the filename. Required.
  70. * @returns The `revert` function. Reverts the hook when called.
  71. */
  72. export declare function addHook(hook: Hook, opts?: Options): RevertFunction;
  73. export {};