A module used by Jest to create a fast lookup of files in a project
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Module resolution system that maps module names to file paths with support for platform-specific extensions, package.json resolution, and mock files. The ModuleMap provides efficient lookups for module names, packages, and mocks with full platform support.
Core module resolution functionality for finding files by module name with platform support.
/**
* Resolve a module name to its file path
* @param name - Module name to resolve
* @param platform - Target platform (e.g., 'ios', 'android', 'web')
* @param supportsNativePlatform - Whether to support React Native platform resolution
* @param type - Module type filter (MODULE or PACKAGE)
* @returns Absolute path to module file, or null if not found
*/
getModule(
name: string,
platform?: string | null,
supportsNativePlatform?: boolean | null,
type?: HTypeValue | null
): string | null;
/**
* Resolve a package name to its package.json file path
* @param name - Package name to resolve
* @param platform - Target platform for platform-specific packages
* @param _supportsNativePlatform - Platform support flag (unused in current implementation)
* @returns Absolute path to package.json, or null if not found
*/
getPackage(
name: string,
platform: string | null | undefined,
_supportsNativePlatform: boolean | null
): string | null;
/**
* Get the file path for a mock module
* @param name - Module name to find mock for
* @returns Absolute path to mock file, or undefined if no mock exists
*/
getMockModule(name: string): string | undefined;Methods for accessing raw module map data and serializing for caching.
/**
* Get the raw internal module map data structure
* @returns Raw module map with all internal data
*/
getRawModuleMap(): RawModuleMap;
/**
* Serialize the module map to JSON for caching or transmission
* @returns Serializable representation of the module map
*/
toJSON(): SerializableModuleMap;Factory methods for creating ModuleMap instances.
/**
* Create a new empty ModuleMap
* @param rootDir - Root directory for the module map
* @returns New ModuleMap instance
*/
static create(rootDir: string): ModuleMap;
/**
* Create ModuleMap from serialized JSON data
* @param serializableModuleMap - Previously serialized module map data
* @returns ModuleMap instance restored from JSON
*/
static fromJSON(serializableModuleMap: SerializableModuleMap): ModuleMap;Convenient export for creating ModuleMap instances.
/**
* Exported ModuleMap utility object with create method
*/
const ModuleMap = {
/**
* Create new ModuleMap instance
* @param rootPath - Root directory path
* @returns IModuleMap interface implementation
*/
create: (rootPath: string) => IModuleMap;
};Usage Examples:
import HasteMap, { ModuleMap } from "jest-haste-map";
// Build haste map and get module map
const hasteMap = await HasteMap.create({
id: "my-app",
extensions: ["js", "ts", "jsx", "tsx"],
maxWorkers: 4,
platforms: ["ios", "android", "web"],
roots: ["/src"],
retainAllFiles: true,
rootDir: "/project/root",
});
const {moduleMap} = await hasteMap.build();
// Resolve module by name
const buttonPath = moduleMap.getModule("Button");
if (buttonPath) {
console.log("Button component found at:", buttonPath);
}
// Platform-specific resolution
const iosButtonPath = moduleMap.getModule("Button", "ios", true);
const androidButtonPath = moduleMap.getModule("Button", "android", true);
const webButtonPath = moduleMap.getModule("Button", "web", true);
console.log("Platform-specific Button components:");
console.log("iOS:", iosButtonPath);
console.log("Android:", androidButtonPath);
console.log("Web:", webButtonPath);
// Package resolution
const reactPackage = moduleMap.getPackage("react", null, false);
if (reactPackage) {
console.log("React package.json found at:", reactPackage);
}
// Mock resolution
const buttonMock = moduleMap.getMockModule("Button");
if (buttonMock) {
console.log("Button mock found at:", buttonMock);
}
// Create standalone module map
const standaloneMap = ModuleMap.create("/different/project");
// Serialize for caching
const serialized = moduleMap.toJSON();
console.log("Serialized module map:", serialized);
// Restore from serialized data
const restoredMap = ModuleMap.fromJSON(serialized);
// Access raw data for advanced use cases
const rawData = moduleMap.getRawModuleMap();
console.log("Module count:", rawData.map.size);
console.log("Mock count:", rawData.mocks.size);
console.log("Duplicates:", rawData.duplicates.size);Core data structures used by the module resolution system.
/**
* Raw internal module map data structure
*/
interface RawModuleMap {
/** Root directory for resolving relative paths */
rootDir: string;
/** Map of duplicate module names to their conflict information */
duplicates: DuplicatesIndex;
/** Map of module names to platform-specific file metadata */
map: ModuleMapData;
/** Map of mock names to file paths */
mocks: MockData;
}
/**
* Platform-specific module metadata mapping
*/
type ModuleMapItem = {[platform: string]: ModuleMetaData};
/**
* Module metadata stored as array for efficiency
* [path, type]
*/
type ModuleMetaData = [path: string, type: number];
/**
* Serializable representation for caching and transmission
*/
interface SerializableModuleMap {
/** Serialized duplicates data */
duplicates: ReadonlyArray<[string, [string, [string, [string, number]]]]>;
/** Serialized module map data */
map: ReadonlyArray<[string, ModuleMapItem]>;
/** Serialized mocks data */
mocks: ReadonlyArray<[string, string]>;
/** Root directory path */
rootDir: string;
}
/**
* Module type constants
*/
interface HType {
MODULE: 0; // Regular module file
PACKAGE: 1; // package.json file
GENERIC_PLATFORM: 'g'; // Generic platform identifier
NATIVE_PLATFORM: 'native'; // React Native platform identifier
}
type HTypeValue = HType[keyof HType];
/**
* Type definitions for internal data structures
*/
type ModuleMapData = Map<string, ModuleMapItem>;
type MockData = Map<string, string>;
type DuplicatesIndex = Map<string, Map<string, DuplicatesSet>>;
type DuplicatesSet = Map<string, number>;Error classes for handling module resolution conflicts.
/**
* Error thrown when multiple files claim the same module name
*/
class DuplicateHasteCandidatesError extends Error {
/** The conflicting module name */
hasteName: string;
/** The platform where the conflict occurred */
platform: string | null;
/** Whether native platform support was enabled */
supportsNativePlatform: boolean;
/** Set of conflicting file paths and their types */
duplicatesSet: DuplicatesSet;
constructor(
name: string,
platform: string,
supportsNativePlatform: boolean,
duplicatesSet: DuplicatesSet
);
}Platform-specific module resolution follows a priority order:
Button.ios.js for platform "ios"Button.native.js if supportsNativePlatform is trueButton.js as fallback// Example of platform resolution priority
const moduleMap = await hasteMap.build().then(result => result.moduleMap);
// This will look for files in order:
// 1. Button.ios.js (or Button.ios.ts, etc.)
// 2. Button.native.js (if supportsNativePlatform is true)
// 3. Button.js (generic fallback)
const iosButton = moduleMap.getModule("Button", "ios", true);
// Force specific type resolution
const packageOnly = moduleMap.getModule("some-package", null, false, 1); // PACKAGE type only
const moduleOnly = moduleMap.getModule("SomeModule", null, false, 0); // MODULE type onlyModuleMap works seamlessly with HasteFS for complete project introspection:
const {hasteFS, moduleMap} = await hasteMap.build();
// Find module and check if it exists
const utilsPath = moduleMap.getModule("utils");
if (utilsPath && hasteFS.exists(utilsPath)) {
const dependencies = hasteFS.getDependencies(utilsPath);
const size = hasteFS.getSize(utilsPath);
console.log(`Utils module: ${utilsPath} (${size} bytes, ${dependencies?.length || 0} deps)`);
}
// Find all modules of a certain type
const rawMap = moduleMap.getRawModuleMap();
for (const [moduleName, platformMap] of rawMap.map) {
for (const [platform, [path, type]] of Object.entries(platformMap)) {
if (type === 0) { // MODULE type
console.log(`Module ${moduleName} (${platform}): ${path}`);
}
}
}