Cache key utility that generates MD5 hashes based on file contents for Metro bundler.
npx @tessl/cli install tessl/npm-metro-cache-key@0.83.0Metro Cache Key is a simple cache key generation utility for the Metro bundler that creates MD5 hashes based on file contents. It provides a single function to generate deterministic hash keys from arrays of file paths, enabling efficient cache invalidation when source files change in Metro's JavaScript bundling pipeline.
npm install metro-cache-keyCommonJS:
const { getCacheKey } = require("metro-cache-key");Alternative CommonJS:
const metroCache = require("metro-cache-key");
const getCacheKey = metroCache.getCacheKey;ESM (if using Node.js with type: "module"):
import { getCacheKey } from "metro-cache-key";const { getCacheKey } = require("metro-cache-key");
// Generate cache key from a single file
const cacheKey1 = getCacheKey(["/path/to/file.js"]);
console.log(cacheKey1); // "651e28171df9ff5d72a4115295dfce6b"
// Generate cache key from multiple files
const cacheKey2 = getCacheKey([
"/path/to/file1.js",
"/path/to/file2.js",
"/path/to/config.json"
]);
console.log(cacheKey2); // "40457a98d325b546bed62a34c7d7cf96"
// Cache keys are deterministic - same files produce same key
const duplicateKey = getCacheKey(["/path/to/file.js"]);
console.log(cacheKey1 === duplicateKey); // trueGenerates an MD5 hash cache key from an array of file paths by reading file contents and combining them with MD5 hashing.
/**
* Generates an MD5 hash cache key from an array of file paths
* @param files - Array of file paths to generate cache key from
* @returns MD5 hash in hexadecimal format
*/
function getCacheKey(files: Array<string>): string;Parameters:
files (Array<string>): Array of absolute file paths. Each file will be read synchronously and its contents included in the hash calculation.Returns:
string: MD5 hash in hexadecimal format (32 characters)Behavior:
fs.readFileSync()\0)crypto.createHash('md5') for hashingError Handling:
ENOENT: no such file or directory, EACCES: permission denied)Usage Examples:
const { getCacheKey } = require("metro-cache-key");
// Single file
const singleFileKey = getCacheKey(["/app/src/index.js"]);
// Multiple files (common Metro bundler usage)
const bundleKey = getCacheKey([
"/app/src/index.js",
"/app/src/components/App.js",
"/app/package.json",
"/app/metro.config.js"
]);
// Cache validation pattern
function isCacheValid(cacheKey, filePaths) {
const currentKey = getCacheKey(filePaths);
return currentKey === cacheKey;
}// Flow type annotations (internal - not exported)
type FilePath = string;
type CacheKey = string;
getCacheKey(files: Array<FilePath>): CacheKey;Note: This package uses Flow type annotations internally but does not export TypeScript definitions. The types shown above are for reference only and represent the internal Flow types used in the source code.
The function uses a reduction pattern to build the hash:
crypto.createHash('md5')\0)fs.readFileSync(file).digest('hex')The null byte separator ensures that different files with concatenated contents don't produce hash collisions.
This utility is designed specifically for Metro bundler's caching system:
The deterministic nature ensures cache invalidation works correctly when source files change, while identical content always produces the same cache key for optimal cache hits.