A module used by Jest to create a fast lookup of files in a project
npx @tessl/cli install tessl/npm-jest-haste-map@30.1.0jest-haste-map is a high-performance file system mapping and watching library designed for efficient project file discovery and change tracking. It serves as a core component of the Jest testing framework, providing fast file lookups, parallel crawling capabilities, and cached file system operations that significantly improve performance in large JavaScript/TypeScript projects.
npm install jest-haste-mapimport HasteMap, { ModuleMap } from "jest-haste-map";For CommonJS:
const HasteMap = require("jest-haste-map").default;
const { ModuleMap } = require("jest-haste-map");import HasteMap from "jest-haste-map";
import os from "os";
// Create a haste map for your project
const hasteMap = await HasteMap.create({
id: "myproject",
extensions: ["js", "ts", "jsx", "tsx"],
maxWorkers: os.availableParallelism(),
platforms: [], // For React Native support
roots: ["/path/to/project"],
retainAllFiles: true,
rootDir: "/path/to/project",
});
// Build the file system map
const { hasteFS, moduleMap } = await hasteMap.build();
// Use the virtual file system
const allFiles = hasteFS.getAllFiles();
const dependencies = hasteFS.getDependencies("/path/to/file.js");
// Resolve modules
const modulePath = moduleMap.getModule("MyComponent");jest-haste-map is built around several key components:
Core file system crawling and mapping functionality for creating fast lookups of project files. Supports parallel processing and intelligent caching.
class HasteMap extends EventEmitter implements IHasteMap {
static create(options: Options): Promise<IHasteMap>;
build(): Promise<{hasteFS: IHasteFS; moduleMap: IModuleMap}>;
end(): Promise<void>;
}
interface Options {
id: string;
extensions: Array<string>;
maxWorkers: number;
platforms: Array<string>;
retainAllFiles: boolean;
rootDir: string;
roots: Array<string>;
cacheDirectory?: string;
computeDependencies?: boolean;
computeSha1?: boolean;
console?: Console;
dependencyExtractor?: string | null;
enableSymlinks?: boolean;
forceNodeFilesystemAPI?: boolean;
hasteImplModulePath?: string;
hasteMapModulePath?: string;
ignorePattern?: HasteRegExp;
mocksPattern?: string;
resetCache?: boolean;
skipPackageJson?: boolean;
throwOnModuleCollision?: boolean;
useWatchman?: boolean;
watch?: boolean;
workerThreads?: boolean;
}Fast file system operations through a virtual interface that provides cached access to file metadata, dependencies, and content information.
interface IHasteFS {
exists(path: string): boolean;
getAllFiles(): Array<string>;
getAbsoluteFileIterator(): Iterable<string>;
getDependencies(file: string): Array<string> | null;
getSize(path: string): number | null;
getSha1(file: string): string | null;
matchFiles(pattern: RegExp | string): Array<string>;
matchFilesWithGlob(globs: ReadonlyArray<string>, root: string | null): Set<string>;
getModuleName(file: string): string | null;
}Module resolution system that maps module names to file paths with support for platform-specific extensions, package.json resolution, and mock files.
interface IModuleMap {
getModule(
name: string,
platform?: string | null,
supportsNativePlatform?: boolean | null,
type?: HTypeValue | null
): string | null;
getPackage(
name: string,
platform: string | null | undefined,
_supportsNativePlatform: boolean | null
): string | null;
getMockModule(name: string): string | undefined;
getRawModuleMap(): RawModuleMap;
toJSON(): SerializableModuleMap;
}
const ModuleMap = {
create: (rootPath: string) => IModuleMap;
};Real-time file system monitoring for interactive development tools, providing change events and automatic map updates.
interface IHasteMap {
on(eventType: 'change', handler: (event: ChangeEvent) => void): void;
}
interface ChangeEvent {
eventsQueue: EventsQueue;
hasteFS: IHasteFS;
moduleMap: IModuleMap;
}type HasteRegExp = RegExp | ((str: string) => boolean);
interface HasteMapStatic {
getCacheFilePath(tmpdir: string, name: string, ...extra: Array<string>): string;
getModuleMapFromJSON(json: SerializableModuleMap): IModuleMap;
}
interface EventsQueue extends Array<{
filePath: string;
stat: Stats | undefined;
type: string;
}> {}
interface SerializableModuleMap {
duplicates: ReadonlyArray<[string, [string, [string, [string, number]]]]>;
map: ReadonlyArray<[string, ModuleMapItem]>;
mocks: ReadonlyArray<[string, string]>;
rootDir: string;
}
type ModuleMapItem = {[platform: string]: ModuleMetaData};
type ModuleMetaData = [path: string, type: number];
type DuplicatesSet = Map<string, number>;
type DuplicatesIndex = Map<string, Map<string, DuplicatesSet>>;
class DuplicateHasteCandidatesError extends Error {
hasteName: string;
platform: string | null;
supportsNativePlatform: boolean;
duplicatesSet: DuplicatesSet;
constructor(
name: string,
platform: string,
supportsNativePlatform: boolean,
duplicatesSet: DuplicatesSet
);
}