{"library":"metro-cache-key","title":"Metro Cache Key Utility","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install metro-cache-key"],"cli":null},"imports":["import { createCacheKey } from 'metro-cache-key';","import type { CacheKeyConfig } from 'metro-cache-key';","import * as CacheKey from 'metro-cache-key';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { createCacheKey } from 'metro-cache-key';\nimport crypto from 'crypto';\n\n// Simulate a basic configuration object for which a cache key is needed\n// In a real Metro context, this config would be more complex and passed by Metro itself.\nconst projectConfig = {\n  root: '/path/to/my/project',\n  transformerPath: 'metro-react-native-babel-transformer',\n  resolverPath: 'metro-resolver',\n  babelConfig: {\n    presets: ['module:metro-react-native-babel-preset'],\n    plugins: ['react-native-reanimated/plugin'],\n  },\n  platform: 'ios',\n  dev: true,\n  // Other factors like Node.js version, Metro version, etc., might also be included\n  environmentHash: process.env.NODE_VERSION || 'unknown_node_version',\n};\n\n// This `generateSimpleCacheKey` function is illustrative. The actual `createCacheKey`\n// from the package would handle complex serialization and hashing internally.\nfunction generateSimpleCacheKey(config: any): string {\n  const configString = JSON.stringify(config, Object.keys(config).sort());\n  return crypto.createHash('md5').update(configString).digest('hex');\n}\n\n// Example usage, conceptually aligning with how `metro-cache-key` might be used\n// The actual `createCacheKey` from the package would be called directly.\nconst cacheKey = createCacheKey ? createCacheKey(projectConfig) : generateSimpleCacheKey(projectConfig);\nconsole.log(`Generated cache key: ${cacheKey}`);\n\n// If a critical input changes, the cache key should change, invalidating previous caches.\nconst anotherConfig = { ...projectConfig, dev: false };\nconst anotherCacheKey = createCacheKey ? createCacheKey(anotherConfig) : generateSimpleCacheKey(anotherConfig);\nconsole.log(`Another cache key (dev: false): ${anotherCacheKey}`);\n\nif (cacheKey === anotherCacheKey) {\n  console.warn('Warning: Cache keys should differ if configuration changes!');\n}\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}