Metro Cache Key Utility

0.84.3 · active · verified Sun Apr 19

The `metro-cache-key` package is an internal utility within the Metro bundler ecosystem, primarily responsible for generating consistent and correct cache keys. These keys are crucial for optimizing build performance by enabling Metro to effectively cache various components, such as transformer outputs and Babel configurations, in React Native projects. The package is currently at version 0.84.3. As an integral part of the larger Metro monorepo, its release schedule is synchronized with Metro's development, typically involving frequent minor updates and occasional hotfixes. A key differentiator is its deep integration and specialized optimization for Metro's internal caching strategy, addressing complex factors like user-defined Babel configs to prevent stale caches and ensure build correctness.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how a cache key might be generated for a simulated Metro project configuration using a conceptual `createCacheKey` function, highlighting its role in identifying unique build contexts for effective caching.

import { createCacheKey } from 'metro-cache-key';
import crypto from 'crypto';

// Simulate a basic configuration object for which a cache key is needed
// In a real Metro context, this config would be more complex and passed by Metro itself.
const projectConfig = {
  root: '/path/to/my/project',
  transformerPath: 'metro-react-native-babel-transformer',
  resolverPath: 'metro-resolver',
  babelConfig: {
    presets: ['module:metro-react-native-babel-preset'],
    plugins: ['react-native-reanimated/plugin'],
  },
  platform: 'ios',
  dev: true,
  // Other factors like Node.js version, Metro version, etc., might also be included
  environmentHash: process.env.NODE_VERSION || 'unknown_node_version',
};

// This `generateSimpleCacheKey` function is illustrative. The actual `createCacheKey`
// from the package would handle complex serialization and hashing internally.
function generateSimpleCacheKey(config: any): string {
  const configString = JSON.stringify(config, Object.keys(config).sort());
  return crypto.createHash('md5').update(configString).digest('hex');
}

// Example usage, conceptually aligning with how `metro-cache-key` might be used
// The actual `createCacheKey` from the package would be called directly.
const cacheKey = createCacheKey ? createCacheKey(projectConfig) : generateSimpleCacheKey(projectConfig);
console.log(`Generated cache key: ${cacheKey}`);

// If a critical input changes, the cache key should change, invalidating previous caches.
const anotherConfig = { ...projectConfig, dev: false };
const anotherCacheKey = createCacheKey ? createCacheKey(anotherConfig) : generateSimpleCacheKey(anotherConfig);
console.log(`Another cache key (dev: false): ${anotherCacheKey}`);

if (cacheKey === anotherCacheKey) {
  console.warn('Warning: Cache keys should differ if configuration changes!');
}

view raw JSON →