Top Related Projects
The React Framework
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
Next generation frontend tooling. It's fast!
The zero configuration build tool for the web. 📦🚀
Next-generation ES module bundler
Quick Overview
Metro is a JavaScript bundler developed by Facebook for React Native applications. It focuses on fast startup times, quick rebuilds, and a simple configuration process, making it an essential tool for React Native developers.
Pros
- Fast bundling and rebuilding times
- Optimized for React Native development
- Supports Hot Module Replacement (HMR)
- Easy to configure and use
Cons
- Primarily designed for React Native, limiting its use in other JavaScript projects
- Less extensive ecosystem compared to other bundlers like webpack
- May require additional setup for advanced features or custom configurations
- Limited documentation for edge cases and advanced usage
Code Examples
- Basic Metro configuration:
// metro.config.js
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
- Custom asset resolver:
// metro.config.js
const { getDefaultConfig } = require('metro-config');
module.exports = (async () => {
const {
resolver: { sourceExts, assetExts },
} = await getDefaultConfig();
return {
transformer: {
babelTransformerPath: require.resolve('react-native-svg-transformer'),
},
resolver: {
assetExts: assetExts.filter(ext => ext !== 'svg'),
sourceExts: [...sourceExts, 'svg'],
},
};
})();
- Custom transformer:
// custom-transformer.js
const upstreamTransformer = require('metro-react-native-babel-transformer');
module.exports.transform = async ({ src, filename, options }) => {
if (filename.endsWith('.custom')) {
return {
code: `module.exports = ${JSON.stringify(src)};`,
};
}
return upstreamTransformer.transform({ src, filename, options });
};
Getting Started
To use Metro in your React Native project:
- Install Metro:
npm install --save-dev metro metro-core
- Create a
metro.config.js
file in your project root:
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
- Run Metro:
npx react-native start
This will start the Metro bundler for your React Native application.
Competitor Comparisons
The React Framework
Pros of Next.js
- Full-stack React framework with built-in server-side rendering and routing
- Extensive ecosystem with plugins and integrations for various use cases
- Automatic code splitting and optimized performance out of the box
Cons of Next.js
- Steeper learning curve for developers new to server-side rendering concepts
- Less flexibility in project structure compared to Metro's minimal approach
- Potentially higher initial bundle size due to included features
Code Comparison
Next.js (pages/index.js):
export default function Home() {
return <h1>Welcome to Next.js!</h1>
}
Metro (index.js):
import React from 'react';
import {AppRegistry} from 'react-native';
const App = () => <h1>Welcome to Metro!</h1>;
AppRegistry.registerComponent('MyApp', () => App);
Key Differences
- Next.js is a full-featured React framework, while Metro is a JavaScript bundler primarily used in React Native projects
- Next.js focuses on web applications with server-side rendering, while Metro is designed for bundling JavaScript in mobile app development
- Next.js provides a more opinionated structure and built-in features, whereas Metro offers a more flexible and lightweight approach to bundling
Use Cases
- Choose Next.js for web applications requiring server-side rendering and a comprehensive framework
- Opt for Metro when developing React Native mobile applications or need a lightweight bundler for JavaScript projects
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
Pros of webpack
- More flexible and configurable, supporting a wider range of project types and build scenarios
- Larger ecosystem with extensive plugins and loaders available
- Better suited for complex web applications and non-React Native projects
Cons of webpack
- Steeper learning curve and more complex configuration
- Slower build times for large projects compared to Metro
- Less optimized for React Native development out of the box
Code comparison
Metro configuration:
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
webpack configuration:
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{ test: /\.js$/, use: 'babel-loader' },
],
},
};
Metro is designed specifically for React Native, offering simpler configuration and faster builds for mobile apps. webpack, on the other hand, provides more flexibility and extensive customization options, making it suitable for a broader range of web development projects. The code examples illustrate the difference in configuration complexity between the two bundlers.
Next generation frontend tooling. It's fast!
Pros of Vite
- Faster build times and hot module replacement (HMR) due to native ES modules
- Broader ecosystem support, including Vue, React, and other frameworks
- Simpler configuration and easier setup for new projects
Cons of Vite
- Less optimized for React Native development compared to Metro
- May require additional plugins or configuration for certain React Native features
- Potentially less stable for large-scale mobile app development
Code Comparison
Metro configuration:
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
},
};
Vite configuration:
export default {
plugins: [react()],
build: {
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
},
},
},
};
Summary
While Metro is specifically designed for React Native development, Vite offers faster build times and broader ecosystem support. However, Vite may require additional setup for React Native projects and might not be as optimized for mobile app development. The choice between the two depends on the specific project requirements and development focus.
The zero configuration build tool for the web. 📦🚀
Pros of Parcel
- Zero configuration out of the box, making it easier for beginners
- Supports a wide range of file types and assets without additional plugins
- Faster build times due to multicore processing and caching
Cons of Parcel
- Less customizable than Metro for complex projects
- May have larger bundle sizes for certain applications
- Not specifically optimized for React Native development
Code Comparison
Metro:
const Metro = require('metro');
Metro.loadConfig().then(config => {
Metro.runBuild(config, {
entry: 'index.js',
output: 'bundle.js'
});
});
Parcel:
const Bundler = require('parcel-bundler');
const Path = require('path');
const entryFiles = Path.join(__dirname, './index.html');
const bundler = new Bundler(entryFiles);
bundler.bundle();
Metro is designed specifically for React Native, offering optimized bundling for mobile applications. It provides more control over the bundling process and is tightly integrated with the React Native ecosystem.
Parcel, on the other hand, is a more general-purpose bundler that focuses on simplicity and ease of use. It's great for quick prototyping and smaller projects where minimal configuration is desired.
Both tools have their strengths, and the choice between them often depends on the specific requirements of the project and the developer's familiarity with each tool.
Next-generation ES module bundler
Pros of Rollup
- More flexible and configurable for various project types
- Better tree-shaking capabilities, resulting in smaller bundle sizes
- Supports ES modules natively, making it ideal for modern JavaScript projects
Cons of Rollup
- Slower build times for large projects compared to Metro
- Less optimized for React Native development
- May require additional plugins for certain features that Metro includes out-of-the-box
Code Comparison
Metro configuration:
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
Rollup configuration:
export default {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
plugins: []
};
Metro is specifically designed for React Native projects, offering a more streamlined setup for mobile app development. It includes features like Hot Module Replacement and a custom resolver out-of-the-box. Rollup, on the other hand, is a more general-purpose bundler that excels in creating smaller, more efficient bundles for web applications and libraries. The choice between the two depends on the specific project requirements and target platform.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Metro
ð The JavaScript bundler for React Native.
- ð Fast: We aim for sub-second reload cycles, fast startup and quick bundling speeds.
- âï¸ Scalable: Works with thousands of modules in a single application.
- âï¸ Integrated: Supports every React Native project out of the box.
Installation
Metro is included with React Native â see the React Native docs to quickly get started â±ï¸.
To add Metro to an existing project, see our Getting Started guide.
Documentation
All available documentation, including on configuring Metro, can be found on the Metro website.
Source code for documentation is located in this repository under docs/
.
Contributing
Metro was previously part of the react-native repository. In this standalone repository it is easier for the team working on Metro to respond to issues and pull requests. See react-native#13976 for the initial announcement.
Code of Conduct
Meta has adopted a Code of Conduct that we expect project participants to adhere to. Please read the full text so that you can understand what actions will and will not be tolerated.
Contributing guide
Read our contributing guide to learn about our development process, how to propose bug fixes and improvements, and how to build and test your changes to Metro.
Discussions
Larger discussions and proposals concerning React Native and Metro are discussed in @react-native-community/discussions-and-proposals.
License
Metro is MIT licensed, as found in the LICENSE file.
Top Related Projects
The React Framework
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
Next generation frontend tooling. It's fast!
The zero configuration build tool for the web. 📦🚀
Next-generation ES module bundler
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot