{"version":3,"file":"cache.js","names":[],"sources":["esm/cache.js"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport {\n  existsSync,\n  mkdirSync,\n  readFileSync,\n  rmSync,\n  statSync,\n  writeFileSync\n} from \"node:fs\";\nimport { join } from \"node:path\";\nimport { configuration, isNativeWorkletsEnabled } from \"./configure\";\nconst stats = {\n  hits: 0,\n  misses: 0,\n  writes: 0,\n  errors: 0\n};\nfunction getCacheDir() {\n  const cacheDir = join(process.cwd(), \"node_modules\", \".vxrn\", \"compiler-cache\");\n  if (!existsSync(cacheDir)) {\n    mkdirSync(cacheDir, { recursive: true });\n  }\n  return cacheDir;\n}\nfunction getConfigFingerprint(userBabelConfigPath) {\n  return createHash(\"sha1\").update(\n    JSON.stringify({\n      compiler: configuration.enableCompiler,\n      reanimated: configuration.enableReanimated,\n      nativeWorklets: isNativeWorkletsEnabled(),\n      nativewind: configuration.enableNativewind,\n      nativeCSS: configuration.enableNativeCSS,\n      // transform output depends on the user babel config when one applies,\n      // so its identity joins the fingerprint. adding, removing, or editing\n      // babel.config.* must miss entries written without that change.\n      babelConfig: getBabelConfigIdentity(userBabelConfigPath),\n      // bump when the transform engine changes, so entries written by a\n      // previous engine aren't served for the same source\n      engine: \"oxc-worklets-array-closure-hermes-async\"\n    })\n  ).digest(\"hex\").slice(0, 8);\n}\nfunction getBabelConfigIdentity(configPath) {\n  if (!configPath) return null;\n  try {\n    const mtime = statSync(configPath).mtimeMs;\n    const hash = createHash(\"sha1\").update(readFileSync(configPath)).digest(\"hex\").slice(0, 16);\n    return `${configPath}:${mtime}:${hash}`;\n  } catch {\n    return configPath;\n  }\n}\nfunction getCacheKey(filePath, environment, userBabelConfigPath) {\n  const hash = createHash(\"sha1\").update(`${environment}:${filePath}:${getConfigFingerprint(userBabelConfigPath)}`).digest(\"hex\");\n  return hash;\n}\nfunction getContentHash(code) {\n  return createHash(\"sha1\").update(code).digest(\"hex\").slice(0, 16);\n}\nfunction getCachedTransform(filePath, code, environment, userBabelConfigPath) {\n  try {\n    const cleanPath = filePath.startsWith(\"\\0\") ? filePath.slice(1) : filePath;\n    const cacheDir = getCacheDir();\n    const cacheKey = getCacheKey(cleanPath, environment, userBabelConfigPath);\n    const cachePath = join(cacheDir, `${cacheKey}.json`);\n    if (!existsSync(cachePath)) {\n      stats.misses++;\n      return null;\n    }\n    const cached = JSON.parse(readFileSync(cachePath, \"utf-8\"));\n    const currentMtime = statSync(cleanPath).mtimeMs;\n    if (cached.mtime !== currentMtime) {\n      stats.misses++;\n      return null;\n    }\n    const currentHash = getContentHash(code);\n    if (cached.hash !== currentHash) {\n      stats.misses++;\n      return null;\n    }\n    stats.hits++;\n    return { code: cached.code, map: cached.map };\n  } catch (err) {\n    stats.misses++;\n    return null;\n  }\n}\nfunction setCachedTransform(filePath, code, result, environment, userBabelConfigPath) {\n  try {\n    const cleanPath = filePath.startsWith(\"\\0\") ? filePath.slice(1) : filePath;\n    const cacheDir = getCacheDir();\n    const cacheKey = getCacheKey(cleanPath, environment, userBabelConfigPath);\n    const cachePath = join(cacheDir, `${cacheKey}.json`);\n    const mtime = statSync(cleanPath).mtimeMs;\n    const hash = getContentHash(code);\n    const entry = {\n      mtime,\n      hash,\n      code: result.code,\n      map: result.map\n    };\n    writeFileSync(cachePath, JSON.stringify(entry), \"utf-8\");\n    stats.writes++;\n  } catch (err) {\n    console.warn(`[cache] Failed to write cache for ${filePath}:`, err);\n  }\n}\nfunction clearTransformCache() {\n  rmSync(getCacheDir(), { recursive: true, force: true });\n}\nfunction getCacheStats() {\n  return { ...stats };\n}\nfunction logCacheStats() {\n  if (!process.env.DEBUG_COMPILER_PERF) {\n    return;\n  }\n  const total = stats.hits + stats.misses;\n  if (total === 0) return;\n  const hitRate = (stats.hits / total * 100).toFixed(1);\n  console.info(\n    `\n\\u{1F4BE} [Cache Stats] ${stats.hits} hits / ${stats.misses} misses (${hitRate}% hit rate), ${stats.writes} writes`\n  );\n}\nexport {\n  clearTransformCache,\n  getCacheStats,\n  getCach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