Metro Bundler Documentation
repository·main·Indexed 26 days ago
https://github.com/react/metroA high-performance JavaScript bundler designed specifically for React Native, optimized for fast reload cycles and scalability. The documentation covers core API functions like runBuild and runServer, configuration via metro-config, resolution logic with metro-resolver, and various bundling formats including Plain, Indexed RAM, and File RAM bundles. It also details caching strategies using FileStore and HttpStore, as well as the experimental metro-file-map for file system crawling.
What's inside Metro
- Metro Config is the configuration resolver and transformer for the Metro bundler. It provides the necessary tools to define how Metro resolves files and transforms them during the bundling process.
Overview of [Experimental] Metro File Map
mainMetro File Map is an experimental package used for file system crawling, watching, and mapping within the Metro bundler ecosystem. It is originally a fork of
jest-haste-map.Warning: This package is currently considered experimental. The API is treated as internal, meaning it is subject to change without semver-breaking guarantees. If you intend to use
metro-file-mapAPIs directly in your own project, it is recommended to raise an issue to discuss your use case with the maintainers first.Overview of Metro bundler
mainMetro is the JavaScript bundler designed specifically for React Native. It is optimized for high performance with sub-second reload cycles, fast startup times, and quick bundling speeds. It is built to be scalable, supporting applications with thousands of modules, and provides out-of-the-box integration for React Native projects.Use ob1 for type-checked 0- and 1-based offsets
mainob1 is a small library designed to handle 0-based and 1-based offsets using type-safe mechanisms. It helps prevent common off-by-one errors by distinguishing between these two indexing types at the type level.Understand Metro's Resolution Types
mainMetro's resolver can return one of three resolution types for a given module name and platform:
- Source file: The request is resolved to an absolute path representing a physical file on disk.
- Asset files: The request is resolved to one or more absolute paths representing physical files on disk (e.g., images).
- Empty module: The request is resolved to a built-in empty module, as specified by the
resolver.emptyModulePathconfiguration.
Understand Metro bundle formats
mainMetro supports three different bundling formats depending on the target environment. Note that in all formats, modules are assigned numeric IDs, which means dynamic
requirecalls are not supported. Requires are transformed into references to these numeric IDs.Supported formats:
- Plain bundle: A standard JavaScript bundle where all files are wrapped in function calls and added to a global file. Ideal for environments expecting a pure JS bundle, such as a web browser. Triggering a build is typically done by requiring the entry point with the
.bundleextension. - Indexed RAM bundle: A binary format optimized for environments that load all code into memory at once (e.g., iOS). It uses an offset table for constant-time module loading.
- File RAM bundle: A format where each module is stored as an individual file. This is optimized for environments like Android where accessing specific files within a zipped package is faster than unzipping a large single binary.
- Plain bundle: A standard JavaScript bundle where all files are wrapped in function calls and added to a global file. Ideal for environments expecting a pure JS bundle, such as a web browser. Triggering a build is typically done by requiring the entry point with the
Use metro-resolver for Metro resolution logic
mainThemetro-resolverpackage provides the resolution logic used by the Metro bundler. It is responsible for determining how module paths are resolved to actual files on disk.Understand Metro module resolution
mainModule resolution is the process Metro uses at build time to translate module names (e.g.,import {View} from 'react-native') into specific file paths (e.g.,node_modules/react-native/index.js). Metro uses a version of Node's module resolution algorithm but includes several specialized features for React Native environments.Understand the Metro bundling process
mainMetro is a JavaScript bundler that converts an entry file and its dependencies into a single JavaScript file. The bundling process consists of three distinct stages:
- Resolution: Metro builds a dependency graph starting from the entry point. It uses a resolver to locate required files.
- Transformation: Modules are transpiled into a format compatible with the target platform (e.g., React Native). This stage runs in parallel across available CPU cores.
- Serialization: Once transformed, a serializer combines the modules into one or more bundles (single JavaScript files).
Use ES Modules syntax (`import` and `export`)
mainTo use ES Modules (
importandexport) in Metro projects, it is recommended to use@babel/plugin-transform-modules-commonjs.Note: If you are using
@react-native/babel-presetin a React Native project,importandexportare supported automatically.Configure the Metro configuration structure
mainMetro configuration is organized into modules. A standard
metro.config.mtsfile uses theMetroConfigtype and contains top-level objects forresolver,transformer,serializer,server, andwatcher.// metro.config.mts import type {MetroConfig} from 'metro-config'; const config: MetroConfig = { /* general options */ resolver: { /* resolver options */ }, transformer: { /* transformer options */ }, serializer: { /* serializer options */ }, server: { /* server options */ }, watcher: { /* watcher options */ watchman: { /* Watchman-specific options */ } } }; export default config;Use conditional exports to target React Native and Web
mainMetro supports the `