index.d.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. /// <reference types="node" />
  8. import EventEmitter from 'events';
  9. import { Config } from '@jest/types';
  10. import H from './constants';
  11. import HasteFS from './HasteFS';
  12. import HasteModuleMap, { SerializableModuleMap as HasteSerializableModuleMap } from './ModuleMap';
  13. import { ChangeEvent, HasteMap as InternalHasteMapObject, HasteRegExp, InternalHasteMap, Mapper } from './types';
  14. declare type HType = typeof H;
  15. declare type Options = {
  16. cacheDirectory?: string;
  17. computeDependencies?: boolean;
  18. computeSha1?: boolean;
  19. console?: Console;
  20. dependencyExtractor?: string;
  21. extensions: Array<string>;
  22. forceNodeFilesystemAPI?: boolean;
  23. hasteImplModulePath?: string;
  24. ignorePattern?: HasteRegExp;
  25. mapper?: Mapper;
  26. maxWorkers: number;
  27. mocksPattern?: string;
  28. name: string;
  29. platforms: Array<string>;
  30. providesModuleNodeModules?: Array<string>;
  31. resetCache?: boolean;
  32. retainAllFiles: boolean;
  33. rootDir: string;
  34. roots: Array<string>;
  35. skipPackageJson?: boolean;
  36. throwOnModuleCollision?: boolean;
  37. useWatchman?: boolean;
  38. watch?: boolean;
  39. };
  40. declare namespace HasteMap {
  41. type ModuleMap = HasteModuleMap;
  42. type SerializableModuleMap = HasteSerializableModuleMap;
  43. type FS = HasteFS;
  44. type HasteMapObject = InternalHasteMapObject;
  45. type HasteChangeEvent = ChangeEvent;
  46. }
  47. /**
  48. * HasteMap is a JavaScript implementation of Facebook's haste module system.
  49. *
  50. * This implementation is inspired by https://github.com/facebook/node-haste
  51. * and was built with for high-performance in large code repositories with
  52. * hundreds of thousands of files. This implementation is scalable and provides
  53. * predictable performance.
  54. *
  55. * Because the haste map creation and synchronization is critical to startup
  56. * performance and most tasks are blocked by I/O this class makes heavy use of
  57. * synchronous operations. It uses worker processes for parallelizing file
  58. * access and metadata extraction.
  59. *
  60. * The data structures created by `jest-haste-map` can be used directly from the
  61. * cache without further processing. The metadata objects in the `files` and
  62. * `map` objects contain cross-references: a metadata object from one can look
  63. * up the corresponding metadata object in the other map. Note that in most
  64. * projects, the number of files will be greater than the number of haste
  65. * modules one module can refer to many files based on platform extensions.
  66. *
  67. * type HasteMap = {
  68. * clocks: WatchmanClocks,
  69. * files: {[filepath: string]: FileMetaData},
  70. * map: {[id: string]: ModuleMapItem},
  71. * mocks: {[id: string]: string},
  72. * }
  73. *
  74. * // Watchman clocks are used for query synchronization and file system deltas.
  75. * type WatchmanClocks = {[filepath: string]: string};
  76. *
  77. * type FileMetaData = {
  78. * id: ?string, // used to look up module metadata objects in `map`.
  79. * mtime: number, // check for outdated files.
  80. * size: number, // size of the file in bytes.
  81. * visited: boolean, // whether the file has been parsed or not.
  82. * dependencies: Array<string>, // all relative dependencies of this file.
  83. * sha1: ?string, // SHA-1 of the file, if requested via options.
  84. * };
  85. *
  86. * // Modules can be targeted to a specific platform based on the file name.
  87. * // Example: platform.ios.js and Platform.android.js will both map to the same
  88. * // `Platform` module. The platform should be specified during resolution.
  89. * type ModuleMapItem = {[platform: string]: ModuleMetaData};
  90. *
  91. * //
  92. * type ModuleMetaData = {
  93. * path: string, // the path to look up the file object in `files`.
  94. * type: string, // the module type (either `package` or `module`).
  95. * };
  96. *
  97. * Note that the data structures described above are conceptual only. The actual
  98. * implementation uses arrays and constant keys for metadata storage. Instead of
  99. * `{id: 'flatMap', mtime: 3421, size: 42, visited: true, dependencies: []}` the real
  100. * representation is similar to `['flatMap', 3421, 42, 1, []]` to save storage space
  101. * and reduce parse and write time of a big JSON blob.
  102. *
  103. * The HasteMap is created as follows:
  104. * 1. read data from the cache or create an empty structure.
  105. *
  106. * 2. crawl the file system.
  107. * * empty cache: crawl the entire file system.
  108. * * cache available:
  109. * * if watchman is available: get file system delta changes.
  110. * * if watchman is unavailable: crawl the entire file system.
  111. * * build metadata objects for every file. This builds the `files` part of
  112. * the `HasteMap`.
  113. *
  114. * 3. parse and extract metadata from changed files.
  115. * * this is done in parallel over worker processes to improve performance.
  116. * * the worst case is to parse all files.
  117. * * the best case is no file system access and retrieving all data from
  118. * the cache.
  119. * * the average case is a small number of changed files.
  120. *
  121. * 4. serialize the new `HasteMap` in a cache file.
  122. * Worker processes can directly access the cache through `HasteMap.read()`.
  123. *
  124. */
  125. declare class HasteMap extends EventEmitter {
  126. private _buildPromise;
  127. private _cachePath;
  128. private _changeInterval?;
  129. private _console;
  130. private _options;
  131. private _watchers;
  132. private _whitelist;
  133. private _worker;
  134. constructor(options: Options);
  135. static getCacheFilePath(tmpdir: Config.Path, name: string, ...extra: Array<string>): string;
  136. getCacheFilePath(): string;
  137. build(): Promise<InternalHasteMapObject>;
  138. /**
  139. * 1. read data from the cache or create an empty structure.
  140. */
  141. read(): InternalHasteMap;
  142. readModuleMap(): HasteModuleMap;
  143. /**
  144. * 2. crawl the file system.
  145. */
  146. private _buildFileMap;
  147. /**
  148. * 3. parse and extract metadata from changed files.
  149. */
  150. private _processFile;
  151. private _buildHasteMap;
  152. private _cleanup;
  153. /**
  154. * 4. serialize the new `HasteMap` in a cache file.
  155. */
  156. private _persist;
  157. /**
  158. * Creates workers or parses files and extracts metadata in-process.
  159. */
  160. private _getWorker;
  161. private _crawl;
  162. /**
  163. * Watch mode
  164. */
  165. private _watch;
  166. /**
  167. * This function should be called when the file under `filePath` is removed
  168. * or changed. When that happens, we want to figure out if that file was
  169. * part of a group of files that had the same ID. If it was, we want to
  170. * remove it from the group. Furthermore, if there is only one file
  171. * remaining in the group, then we want to restore that single file as the
  172. * correct resolution for its ID, and cleanup the duplicates index.
  173. */
  174. private _recoverDuplicates;
  175. end(): Promise<void>;
  176. /**
  177. * Helpers
  178. */
  179. private _ignore;
  180. private _isNodeModulesDir;
  181. private _createEmptyMap;
  182. static H: HType;
  183. static DuplicateError: typeof DuplicateError;
  184. static ModuleMap: typeof HasteModuleMap;
  185. }
  186. declare class DuplicateError extends Error {
  187. mockPath1: string;
  188. mockPath2: string;
  189. constructor(mockPath1: string, mockPath2: string);
  190. }
  191. export = HasteMap;
  192. //# sourceMappingURL=index.d.ts.map