Transform worker for Metro bundler that handles JavaScript, TypeScript, JSX, JSON, and asset transformations through a comprehensive Babel-based pipeline.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Metro Transform Worker provides comprehensive cache key generation for efficient incremental builds and cache invalidation across the transformation pipeline.
Generates deterministic cache keys based on transformer configuration, file dependencies, and transformation settings to enable efficient caching.
/**
* Generate cache key for transformer configuration
* @param config - Transformer configuration object
* @returns String cache key that changes when configuration affects transformation output
*/
function getCacheKey(config: JsTransformerConfig): string;Usage Examples:
const { getCacheKey } = require("metro-transform-worker");
const config = {
babelTransformerPath: require.resolve("@react-native/metro-babel-transformer"),
minifierPath: require.resolve("metro-minify-terser"),
assetRegistryPath: "react-native/Libraries/Image/AssetRegistry",
assetPlugins: [],
enableBabelRCLookup: true,
enableBabelRuntime: false,
globalPrefix: "",
hermesParser: false,
minifierConfig: {
output: { ascii_only: true },
compress: { reduce_funcs: false },
},
optimizationSizeLimit: 150000,
publicPath: "/assets",
allowOptionalDependencies: "ignore",
unstable_disableModuleWrapping: false,
unstable_compactOutput: false,
unstable_allowRequireContext: false,
};
// Generate cache key for configuration
const cacheKey = getCacheKey(config);
console.log(cacheKey); // "abc123def456...ghi789$hjk012$lmn345"
// Cache keys change when configuration changes
const modifiedConfig = { ...config, dev: true };
const newCacheKey = getCacheKey(modifiedConfig);
console.log(cacheKey !== newCacheKey); // trueThe cache key is composed of several components that ensure invalidation when transformation behavior changes:
// Cache key format: "files$config$babel"
interface CacheKeyComponents {
/** Hash of all file dependencies that affect transformation */
filesKey: string;
/** Hash of configuration object (excluding file paths) */
configHash: string;
/** Babel transformer's cache key (if available) */
babelKey: string;
}The final cache key is constructed by joining these components with $ separators:
const cacheKey = [filesKey, configHash, babelKey].join("$");The cache system tracks dependencies across multiple layers:
const coreFiles = [
__filename, // metro-transform-worker/src/index.js
require.resolve(babelTransformerPath),
require.resolve(minifierPath),
require.resolve("./utils/getMinifier"),
require.resolve("./utils/assetTransformer"),
];const metroFiles = [
require.resolve("metro/private/ModuleGraph/worker/generateImportNames"),
require.resolve("metro/private/ModuleGraph/worker/JsFileWrapping"),
];const pluginFiles = metroTransformPlugins.getTransformPluginCacheKeyFiles();The configuration hash excludes file paths (which are already covered by file dependency tracking) and focuses on options that affect transformation behavior:
interface HashedConfigSubset {
// Excludes: babelTransformerPath, minifierPath
assetPlugins: ReadonlyArray<string>;
assetRegistryPath: string;
asyncRequireModulePath: string;
dynamicDepsInPackages: DynamicRequiresBehavior;
enableBabelRCLookup: boolean;
enableBabelRuntime: boolean | string;
globalPrefix: string;
hermesParser: boolean;
minifierConfig: MinifierConfig;
optimizationSizeLimit: number;
publicPath: string;
allowOptionalDependencies: AllowOptionalDependencies;
unstable_dependencyMapReservedName?: string;
unstable_disableModuleWrapping: boolean;
unstable_disableNormalizePseudoGlobals: boolean;
unstable_compactOutput: boolean;
unstable_allowRequireContext: boolean;
unstable_memoizeInlineRequires?: boolean;
unstable_nonMemoizedInlineRequires?: ReadonlyArray<string>;
unstable_renameRequire?: boolean;
}The cache key changes automatically when any of these conditions occur:
Metro Transform Worker's cache keys integrate with Metro's broader caching system:
// Metro uses the cache key for:
// 1. Transformation result caching
// 2. Source map caching
// 3. Dependency graph caching
// 4. Asset processing caching
const metroConfig = {
transformer: {
getTransformCacheKeyFn: () => getCacheKey(transformerConfig),
// ... other transformer options
},
};When experiencing unexpected cache behavior:
// Enable Metro cache debugging
process.env.DEBUG = "Metro:Cache";
// Check cache key components
const cacheKey = getCacheKey(config);
const [filesKey, configHash, babelKey] = cacheKey.split("$");
console.log("Files key:", filesKey);
console.log("Config hash:", configHash);
console.log("Babel key:", babelKey);
// Verify configuration serialization
console.log("Config subset:", JSON.stringify(configSubset, null, 2));