UNPKG

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