Internal Babel registration utility for Metro bundler development and monorepo workflows.
npx @tessl/cli install tessl/npm-metro-babel-register@0.83.0Metro Babel Register provides internal Babel registration and configuration specifically designed for Metro monorepo development workflows. It offers a standardized way to configure Babel transformations for Metro's internal build system, including support for TypeScript, Flow, and modern JavaScript features. This is a private monorepo package used internally by Metro and is not published as a standalone npm package.
const { register, config, buildRegExps, unstable_registerForMetroMonorepo } = require("metro-babel-register");For individual imports:
const { register } = require("metro-babel-register");Note: This package is internal to the Metro monorepo and must be required from within Metro's source tree.
const { register } = require("metro-babel-register");
// Register Babel for specific directories
register(['/path/to/src', '/path/to/lib']);
// Register with early plugins
register(['/path/to/src'], {
earlyPlugins: [
['@babel/plugin-proposal-optional-chaining']
]
});Metro Babel Register is built around several key components:
register() function that configures @babel/register with Metro-specific settingsconfig() function that creates Babel configuration objects with Metro presets and pluginsbuildRegExps() for creating file path matching patternsRegister Babel with Metro-specific configuration for automatic code compilation.
/**
* Registers Babel with Metro-specific configuration for compilation
* @param onlyList - Array of RegExp or string patterns for files to transform
* @param opts - Optional configuration object
*/
function register(
onlyList: Array<RegExp | string>,
opts?: {
earlyPlugins?: Array<any>;
}
): void;Generate Babel configuration objects with Metro-specific plugins and presets.
/**
* Returns Babel configuration object for Metro projects
* @param onlyList - Array of RegExp or string patterns for files to transform
* @param options - Optional configuration options
* @returns Babel configuration object
*/
function config(
onlyList: Array<RegExp | string>,
options?: {
lazy?: boolean;
earlyPlugins?: Array<any>;
}
): BabelCoreOptions;
interface BabelCoreOptions {
babelrc: boolean;
compact: boolean;
configFile: boolean;
browserslistConfigFile: boolean;
ignore: Array<RegExp>;
only: Array<RegExp | string>;
plugins: Array<any>;
presets: Array<any>;
retainLines: boolean;
sourceMaps: string;
overrides: Array<{
test: RegExp;
plugins?: Array<any>;
presets?: Array<any>;
}>;
}Build regular expressions for file path matching with absolute paths.
/**
* Builds regular expressions for path matching with absolute paths
* @param basePath - Base directory path
* @param dirPaths - Array of directory paths or RegExp patterns
* @returns Array of compiled RegExp patterns
*/
function buildRegExps(
basePath: string,
dirPaths: Array<RegExp | string>
): Array<RegExp>;Internal registration function for Metro's own development workflow.
/**
* Registers Babel specifically for Metro monorepo development
* This is an unstable API for internal Metro development use
*/
function unstable_registerForMetroMonorepo(): void;The configuration automatically handles these file extensions (in processing order):
.ts - TypeScript files.tsx - TypeScript JSX files.es6 - ES6 modules.es - ES modules.jsx - React JSX files.js - JavaScript files.mjs - ES modulesNote: Extensions are added to Babel's register configuration in addition to Babel's default extensions.
Core Plugins:
@babel/plugin-proposal-export-namespace-from - Export namespace syntax@babel/plugin-transform-modules-commonjs - CommonJS module transformationFlow Support (for .js files):
babel-plugin-syntax-hermes-parser - Hermes parser syntax supportbabel-plugin-transform-flow-enums - Flow enum transformations@babel/plugin-transform-flow-strip-types - Flow type strippingTypeScript Support (for .ts/.tsx files):
@babel/preset-typescript - TypeScript compilationbabel-plugin-replace-ts-export-assignment - Export assignment handlingbabel-plugin-metro-replace-ts-require-assignment - Custom Metro TypeScript pluginMetro Babel Register includes a custom Babel plugin for TypeScript compatibility:
/**
* Custom Babel plugin that transforms TypeScript import assignments
* Converts `import thing = require('thing')` to `import thing from 'thing'`
* @param options - Object containing template helper function
* @param options.template - Babel template helper for creating AST nodes
* @returns Babel plugin object
*/
function babelPluginMetroReplaceTsRequireAssignment({ template }): {
name: 'metro-replace-ts-require-assignment';
visitor: {
TSImportEqualsDeclaration(path: any): void;
};
};The package includes several safety mechanisms:
isRegisteredForMetroMonorepo flagNODE_ENV=production)FBSOURCE_ENV=1const { register } = require("metro-babel-register");
// Register for source and lib directories
register([
'./src',
'./lib',
/packages\/.*\/src/
]);const { register, config } = require("metro-babel-register");
// Get configuration without registering
const babelConfig = config(['./src'], {
lazy: true,
earlyPlugins: [
['@babel/plugin-proposal-decorators', { legacy: true }]
]
});
// Use custom configuration
require('@babel/register')(babelConfig);const { buildRegExps } = require("metro-babel-register");
// Build absolute path patterns
const patterns = buildRegExps('/home/user/project', [
'src',
'lib',
/packages\/.*\/src/
]);
console.log(patterns);
// [/^\/home\/user\/project\/src/, /^\/home\/user\/project\/lib/, /^\/home\/user\/project\/packages\/.*\/src/]const { unstable_registerForMetroMonorepo } = require("metro-babel-register");
// For Metro development (internal use)
unstable_registerForMetroMonorepo();