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 is the core transformation engine for Meta's Metro JavaScript bundler. It handles the conversion of JavaScript, TypeScript, JSX, JSON, and asset files through a comprehensive Babel-based pipeline, providing dependency analysis, code optimization, minification, and source map generation for React Native and web applications.
npm install metro-transform-workerconst { transform, getCacheKey } = require("metro-transform-worker");For ESM:
import { transform, getCacheKey } from "metro-transform-worker";const { transform } = require("metro-transform-worker");
const fs = require("fs");
// Basic file transformation
const config = {
babelTransformerPath: require.resolve("@react-native/metro-babel-transformer"),
minifierPath: require.resolve("metro-minify-terser"),
assetRegistryPath: "react-native/Libraries/Image/AssetRegistry",
assetPlugins: [],
// ... other configuration options
};
const options = {
dev: true,
hot: false,
inlinePlatform: false,
inlineRequires: false,
minify: false,
platform: "ios",
type: "module",
unstable_transformProfile: "default",
};
// Transform a JavaScript file
const fileData = fs.readFileSync("./src/MyComponent.js");
const result = await transform(
config,
"/path/to/project/root",
"./src/MyComponent.js",
fileData,
options
);
console.log(result.output[0].data.code); // Transformed code
console.log(result.dependencies); // Module dependenciesMetro Transform Worker is built around several key components:
Primary transformation function that processes files through the complete Metro pipeline, handling JavaScript/TypeScript/JSX transformations, dependency analysis, and code optimization.
/**
* Transform files through Metro's complete transformation pipeline
* @param config - Transformer configuration object
* @param projectRoot - Absolute path to project root directory
* @param filename - Relative path to file being transformed
* @param data - File content as Buffer
* @param options - Transformation options
* @returns Promise resolving to transformation result with dependencies and output
*/
function transform(
config: JsTransformerConfig,
projectRoot: string,
filename: string,
data: Buffer,
options: JsTransformOptions
): Promise<TransformResponse>;
interface TransformResponse {
dependencies: TransformResultDependency[];
output: (JsOutput | BytecodeOutput)[];
}
interface JsOutput {
data: {
code: string;
lineCount: number;
map: MetroSourceMapSegmentTuple[];
functionMap: FBSourceFunctionMap | null;
};
type: JSFileType;
}Cache key generation for efficient incremental builds and cache invalidation.
/**
* Generate cache key for transformer configuration
* @param config - Transformer configuration object
* @returns String cache key based on configuration and dependencies
*/
function getCacheKey(config: JsTransformerConfig): string;Comprehensive configuration system for customizing transformation behavior, Babel integration, and optimization settings.
interface JsTransformerConfig {
assetPlugins: ReadonlyArray<string>;
assetRegistryPath: string;
asyncRequireModulePath: string;
babelTransformerPath: string;
dynamicDepsInPackages: DynamicRequiresBehavior;
enableBabelRCLookup: boolean;
enableBabelRuntime: boolean | string;
globalPrefix: string;
hermesParser: boolean;
minifierConfig: MinifierConfig;
minifierPath: string;
optimizationSizeLimit: number;
publicPath: string;
allowOptionalDependencies: AllowOptionalDependencies;
unstable_collectDependenciesPath: string;
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;
}
interface JsTransformOptions {
customTransformOptions?: CustomTransformOptions;
dev: boolean;
experimentalImportSupport?: boolean;
hot: boolean;
inlinePlatform: boolean;
inlineRequires: boolean;
minify: boolean;
nonInlinedRequires?: ReadonlyArray<string>;
platform?: string;
runtimeBytecodeVersion?: number;
type: Type;
unstable_disableES6Transforms?: boolean;
unstable_memoizeInlineRequires?: boolean;
unstable_nonMemoizedInlineRequires?: ReadonlyArray<string>;
unstable_staticHermesOptimizedRequire?: boolean;
unstable_transformProfile: TransformProfile;
}Detailed error handling with context and debugging information for transformation failures.
class InvalidRequireCallError extends Error {
innerError: InternalInvalidRequireCallError;
filename: string;
constructor(innerError: InternalInvalidRequireCallError, filename: string);
}type Type = "script" | "module" | "asset";
type JSFileType = "js/script" | "js/module" | "js/module/asset";
type BytecodeFileType = "bytecode/module" | "bytecode/module/asset" | "bytecode/script";
type DynamicRequiresBehavior = "reject" | "throwAtRuntime";
interface MinifierOptions {
code: string;
map?: BasicSourceMap;
filename: string;
reserved: ReadonlyArray<string>;
config: MinifierConfig;
}
interface MinifierResult {
code: string;
map?: BasicSourceMap;
}
type Minifier = (options: MinifierOptions) => MinifierResult | Promise<MinifierResult>;
type TransformProfile = "default" | "hermes-stable" | "hermes-canary";interface TransformResultDependency {
name: string;
type: string;
// Additional Metro-specific dependency metadata
}
type MetroSourceMapSegmentTuple = [number, number, number?, number?, string?];
interface FBSourceFunctionMap {
names: ReadonlyArray<string>;
mappings: string;
}
interface BytecodeOutput {
data: unknown;
type: BytecodeFileType;
}